3 min read | by Jordi Prats
Terraform keeps a list of managed objects on it's state, if for some reason we no longer want terraform to manage them we can remove them from the code base. Doing so we will see how terraform will try to delete them:
$ terraform plan
(...)
Plan: 0 to add, 0 to change, 10 to destroy.
(...)
To be able to terraform to forget about these resources we can remove them from them terraform state using terraform state rm:
$ terraform state rm aws_iam_instance_profile.instance_profile
Acquiring state lock. This may take a few moments...
Removed aws_iam_instance_profile.instance_profile
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
We can also use the terraform plan output for getting the list of objects to delete:
$ terraform plan -no-color | grep "will be destroyed" | awk '{ print $2 }'
aws_iam_policy.policy1
aws_iam_policy.policy2
aws_iam_policy.policy3
aws_iam_role.demo_role
aws_iam_role_policy.inline_policy1
aws_iam_role_policy.inline_policy2
aws_iam_role_policy_attachment.policy1
aws_iam_role_policy_attachment.policy2
aws_iam_role_policy_attachment.policy3
To be able to tell terraform to remove them from the state using a bash for loop:
$ for i in $(terraform plan -no-color | grep "will be destroyed" | awk '{ print $2 }'); do terraform state rm $i; done
Acquiring state lock. This may take a few moments...
Removed aws_iam_policy.policy1
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Acquiring state lock. This may take a few moments...
Removed aws_iam_policy.policy2
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Acquiring state lock. This may take a few moments...
Removed aws_iam_policy.policy3
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Acquiring state lock. This may take a few moments...
Removed aws_iam_role.demo_role
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Acquiring state lock. This may take a few moments...
Removed aws_iam_role_policy.inline_policy1
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Acquiring state lock. This may take a few moments...
Removed aws_iam_role_policy.inline_policy2
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Acquiring state lock. This may take a few moments...
Removed aws_iam_role_policy_attachment.policy1
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Acquiring state lock. This may take a few moments...
Removed aws_iam_role_policy_attachment.policy2
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Acquiring state lock. This may take a few moments...
Removed aws_iam_role_policy_attachment.policy3
Successfully removed 1 resource instance(s).
Releasing state lock. This may take a few moments...
Posted on 30/04/2021