DEV Community

Cover image for 31.Delete IAM Group Using Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

31.Delete IAM Group Using Terraform

Lab Information

The Nautilus DevOps team is currently engaged in a cleanup process, focusing on removing unnecessary data and services from their AWS account. As part of the migration process, several resources were created for one-time use only, necessitating a cleanup effort to optimize their AWS environment.

Delete an IAM group named iamgroup_james using terraform. Make sure to keep the provisioning code, as we might need to provision this instance again later.

The Terraform working directory is /home/bob/terraform.

Note: Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.

Lab Solutions

Step 1: Check Main Terraform Configuration

# main.tf

resource "aws_iam_group" "this" {
  name = "iamgroup_james"
}

Enter fullscreen mode Exit fullscreen mode

Check IAM Group

aws iam get-group --group-name iamgroup_james
Enter fullscreen mode Exit fullscreen mode

Output

bob@iac-server ~/terraform via πŸ’  default ➜  aws iam get-group --group-name iamgroup_james
{
    "Users": [],
    "Group": {
        "Path": "/",
        "GroupName": "iamgroup_james",
        "GroupId": "qy0dq13t40i9exy3xtsb",
        "Arn": "arn:aws:iam::000000000000:group/iamgroup_james",
        "CreateDate": "2025-11-08T09:39:53.135474Z"
    }
}
Enter fullscreen mode Exit fullscreen mode

Step2: To deploy this configuration

Navigate to the Terraform directory:

cd /home/bob/terraform
Enter fullscreen mode Exit fullscreen mode

Initialize Terraform:

terraform init
Enter fullscreen mode Exit fullscreen mode

Output

bob@iac-server ~/terraform via πŸ’  default ➜  terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "5.91.0"...
- Installing hashicorp/aws v5.91.0...
- Installed hashicorp/aws v5.91.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Enter fullscreen mode Exit fullscreen mode

Destroy the configuration:

terraform destroy -target=aws_iam_group.this
Enter fullscreen mode Exit fullscreen mode

Then type yes when prompted to confirm the creation of the snapshot.

Output

bob@iac-server ~/terraform via πŸ’  default ➜  terraform destroy -target=aws_iam_group.this
aws_iam_group.this: Refreshing state... [id=iamgroup_james]

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # aws_iam_group.this will be destroyed
  - resource "aws_iam_group" "this" {
      - arn       = "arn:aws:iam::000000000000:group/iamgroup_james" -> null
      - id        = "iamgroup_james" -> null
      - name      = "iamgroup_james" -> null
      - path      = "/" -> null
      - unique_id = "qy0dq13t40i9exy3xtsb" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.
β•·
β”‚ Warning: Resource targeting is in effect
β”‚ 
β”‚ You are creating a plan with the -target option, which means that the result of this
β”‚ plan may not represent all of the changes requested by the current configuration.
β”‚ 
β”‚ The -target option is not for routine use, and is provided only for exceptional
β”‚ situations such as recovering from errors or mistakes, or when Terraform specifically
β”‚ suggests to use it as part of an error message.
β•΅

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_iam_group.this: Destroying... [id=iamgroup_james]
aws_iam_group.this: Destruction complete after 0s
β•·
β”‚ Warning: Applied changes may be incomplete
β”‚ 
β”‚ The plan was created with the -target option in effect, so some changes requested in
β”‚ the configuration may have been ignored and the output values may not be fully updated.
β”‚ Run the following command to verify that no other changes are pending:
β”‚     terraform plan
β”‚ 
β”‚ Note that the -target option is not suitable for routine use, and is provided only for
β”‚ exceptional situations such as recovering from errors or mistakes, or when Terraform
β”‚ specifically suggests to use it as part of an error message.
β•΅

Destroy complete! Resources: 1 destroyed.
Enter fullscreen mode Exit fullscreen mode

Resources & Next Steps
πŸ“¦ Full Code Repository: KodeKloud Learning Labs
πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions
πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you

Credits
β€’ All labs are from: KodeKloud
β€’ I sincerely appreciate your provision of these valuable resources.

Top comments (0)