title: Terraform - EntityAlreadyExists Error (409) when a module's resource already exists
body: <p>I'm writing a <a href="/questions/tagged/terraform" class="post-tag" title="show questions tagged &#39;terraform&#39;" rel="tag">terraform</a> module which should be reused across different environments.</p>
  
  <p>In order to make things simple, here's a basic example of calling a module from one of the environments root module: </p>
  
  <pre><code>##QA-resources.tf
  
  module "some_module" {
    source = "./path/to/module" 
  }
  
  some_variable = ${module.some_module.some_output}
  </code></pre>
  
  <p>The problem is that when a module was already created Terraform throws an error of:</p>
  
  <blockquote>
    <p>Error creating [resource-type] [resource-name]: EntityAlreadyExists: [resource-type] with [resource-name] already exists.
            status code: 409, request id: ...</p>
  </blockquote>
  
  <p>This is happening when the module was created under the scope of external <code>terraform.tfstate</code> and one of the resources has a unique field like 'Name'.</p>
  
  <p>In my case, it happened while trying to use an IAM module which already created an role with that specific name, but it can happen in many other cases (I don't want the discussion to be specific to my use case).  </p>
  
  <p><strong>I would expect that if one of the module's resources exist, no failure will occur and the module's outputs would be available to the <code>root</code> module</strong>.</p>
  
  <p>Any suggestions how to manage this (maybe using specific command or a flag)?</p>
  
  <hr>
  
  <p>A few related threads I found:</p>
  
  <p><a href="https://github.com/hashicorp/terraform/issues/8184" rel="nofollow noreferrer">Terraform doesn't reuse an AWS Role it just created and fails?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/57903408/what-is-the-best-way-to-solve-entityalreadyexists-error-in-terraform">what is the best way to solve EntityAlreadyExists error in terraform?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/58860929/terraform-error-entityalreadyexists-role-with-name-iam-for-lambda-already-exist">Terraform error EntityAlreadyExists: Role with name iam_for_lambda already exists</a></p>
  
  <hr>
  
  <h2>Edit</h2>
  
  <p>For @Martin Atkins request here's the resource which caused the error.</p>
  
  <p>It is a basic role for an AWS EKS cluster which have 2 policies attached (passed via <code>var.policies</code>):</p>
  
  <pre><code>resource "aws_iam_role" "k8s_role" {
    name = "k8s-role"
  
    assume_role_policy = &lt;&lt;EOF
  {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "sts:AssumeRole",
        "Principal": {
          "Service": "eks.amazonaws.com"
        },
        "Effect": "Allow",
        "Sid": ""
      }
    ]
  }
  EOF
  }
  
  
  resource "aws_iam_role_policy_attachment" "role-policy-attach" {
    role  = "${aws_iam_role.k8s_role.name}"
    count = "${length(var.policies)}"
    policy_arn = "${element(var.policies, count.index)}"
  }
  </code></pre>
  
  <p>This role was wrapped as a module and was passed to the root module.<br>
  The error mentioned above in blockquotes occurred because the role already exist while the root module tried to create it.</p>