2
votes

I got a duplicate existing resource error when deploy to AWS via Terraform.

Error: Error creating IAM Role SecuritySearchAPITaskRole: EntityAlreadyExists: Role with name SecuritySearchAPITaskRole already exists.
    status code: 409, request id: cf5ae1f4-de6a-11e9-a7b1-d3cdff4db013

  on deploy/modules/ecs-fargate-service/iam.tf line 1, in resource "aws_iam_role" "task":
   1: resource "aws_iam_role" "task" {

Based on above error, it is an existing IAM Role with the name SecuritySearchAPITaskRole. I think the solution is to import this resource to my local terraform state but how I can find out the resource ID I need to use. I am able to find this ROLE on AWS IAM console but it doesn't seem to have an ID. I also tried to run terraform plan which gives me:

  + resource "aws_iam_role" "task" {
      + arn                   = (known after apply)
      + assume_role_policy    = jsonencode(
            {
              + Statement = [
                  + {
                      + Action    = "sts:AssumeRole"
                      + Effect    = "Allow"
                      + Principal = {
                          + Service = "ecs-tasks.amazonaws.com"
                        }
                    },
                ]
              + Version   = "2012-10-17"
            }
        )
      + create_date           = (known after apply)
      + description           = "Task role for the SecuritySearchAPI service"
      + force_detach_policies = false
      + id                    = (known after apply)
      + max_session_duration  = 3600
      + name                  = "SecuritySearchAPITaskRole"
      + path                  = "/"
      + tags                  = {
          + "Application"            = "Security Search"
          + "Client"                 = "IRESS"
          + "DataClassification"     = "NoData"
          + "Name"                   = "SecuritySearchAPI Task Role"
          + "Owner"                  = "platform"
          + "Product"                = "SharedServices"
          + "Schedule"               = "False"
          + "Service"                = "Search"
          + "TaggingStandardVersion" = "3"
        }
      + unique_id             = (known after apply)
    }

And you can see the id =known after apply` is not created. How can I find the ID for IAM role?

1

1 Answers

2
votes

Ok, I found out this doc https://www.terraform.io/docs/providers/aws/r/iam_role.html#import, I can use role name as the ID in terraform import command.