CodexBloom - Programming Q&A Platform

Terraform scenarios to create IAM role with scenarios 'The role name already exists' despite not being listed

👀 Views: 5 💬 Answers: 1 📅 Created: 2025-05-31
terraform aws iam HCL

I'm getting frustrated with I'm sure I'm missing something obvious here, but I'm using Terraform v1.4.0 to create an IAM role in AWS with the following configuration: ```hcl resource "aws_iam_role" "my_role" { name = "my_unique_role_name" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Effect = "Allow" Principal = { Service = "ec2.amazonaws.com" } } ] }) } ``` When I run `terraform apply`, I get the behavior message: ``` behavior: behavior creating IAM Role my_unique_role_name: EntityAlreadyExists: Role name my_unique_role_name already exists ``` I’ve checked the IAM console and verified that there's no role with that name..... To troubleshoot, I tried running `terraform state list` and `terraform state show aws_iam_role.my_role`, but nothing indicates that the role is being tracked by Terraform. I also attempted to run `terraform import aws_iam_role.my_role <role-arn>` using the ARN of a role I created manually before, but it still gives me the same behavior when I try to create it again. Could there be a delay in the IAM service or some caching scenario that's causing Terraform to think the role already exists? Are there any best practices for ensuring that role names are truly unique before creation? Any insight would be greatly appreciated. I'm working on a service that needs to handle this. I'd really appreciate any guidance on this. Any help would be greatly appreciated! For context: I'm using Hcl on Windows 10. I'd really appreciate any guidance on this. I'd be grateful for any help.