Wednesday, February 27, 2019

HowTo fix Terraform - IAM resource instance profile already exists error


Terraform - Apply raises IAM instance profile role already exists error






While working with Terraform IAM roles, not quite often but sometimes we come across the below error


Error creating IAM instance profile my-iam-profile already exists: my-iam-profile already exists (EntityAlreadyExists)

In this post, let us see how to resolve this.

When I first encountered this error, during 'Terraform Apply' like anyone else, I went and searched for whether this IAM instance profile was actually accounted in 'Terraform Plan' or not. To much of my surprise, it had considered this resource for creation and had reported during pan 

Next door to knock for was the IAM console(as I had access for my account(s)) and could not see them either

Issue seems to be that, for some reasons unknown, Terraform does not destroy them in the first place and yet identifies and reports these resources for creation during 'Plan' phase. 

Resolution: 

  1. Delete the IAM instance profile roles in context from AWS CLI 
  2. Or another way is that add in CLI command just followed to the destroy phase but the issue here is that we do not know when these instance profiles are deleted and when they aren't deleted :-(
This is how I cleared the error and here it is:

First see whether the instance profiles still exists or not from CLI interface

aws iam list-instance-profiles

Then remove them manually as below



In case, if the resource instance profile doesnt exist we will get the error as below



Now run the Terraform Plan, Terraform Apply and see the resources getting created as expected

5 comments: