title: Terraform Error creating Lambda function: ResourceConflictException with the resource just created by Terraform apply
body: <p>I am deploying a Lambda function in each AWS Region of our account and encountering weird issue where the Apply is failing with the following error message for some of the AWS Regions</p>
  <p><strong>Error while Terraform Apply</strong></p>
  <pre><code>Error: Error creating Lambda function: ResourceConflictException: Function already exist: log-forwarder
  {
    RespMetadata: {
      StatusCode: 409,
      RequestID: &quot;8cfd7260-7c4a-42d2-98c6-6619c7b2804f&quot;
    },
    Message_: &quot;Function already exist: log-forwarder&quot;,
    Type: &quot;User&quot;
  }
  </code></pre>
  <p>The above Lambda function has just been created by same Terraform Apply that is failing.</p>
  <p>The terraform plan and init doesn't throw any errors about having TF config issues.</p>
  <p>Both plan and init runs successfully.</p>
  <p>Below is my directory structure</p>
  <pre><code>.
  ├── log_forwarder.tf
  ├── log_forwarder_lambdas
  │   └── main.tf
  └── providers.tf
  </code></pre>
  <p>Below is my <code>providers.tf</code> file</p>
  <pre><code>provider &quot;aws&quot; {
    region  = &quot;us-east-1&quot;
    version = &quot;3.9.0&quot;
  }
  provider &quot;aws&quot; {
    alias   = &quot;us-east-2&quot;
    region  = &quot;us-east-2&quot;
    version = &quot;3.9.0&quot;
  }
  provider &quot;aws&quot; {
    alias   = &quot;us-west-2&quot;
    region  = &quot;us-west-2&quot;
    version = &quot;3.9.0&quot;
  }  
  provider &quot;aws&quot; {
    alias   = &quot;us-west-1&quot;
    region  = &quot;us-west-1&quot;
    version = &quot;3.9.0&quot;
  }
  provider &quot;aws&quot; {
    alias   = &quot;ca-central-1&quot;
    region  = &quot;ca-central-1&quot;
    version = &quot;3.9.0&quot;
  }
  
  
  ... with all the AWS Regions.
  </code></pre>
  <p>Below is the tf config of <code>log_forwarder.tf</code></p>
  <pre><code>terraform {
    required_version = &quot;0.12.25&quot;
  
    backend &quot;s3&quot; {
      All the backend Config
    }
  }
  
  
  resource &quot;aws_iam_role&quot; &quot;log_forwarder&quot; {
    name = &quot;LogForwarder&quot;
  
    assume_role_policy = &lt;&lt;EOF
  {
    &quot;Version&quot;: &quot;2012-10-17&quot;,
    &quot;Statement&quot;: [
      {
        &quot;Sid&quot;: &quot;&quot;,
        &quot;Effect&quot;: &quot;Allow&quot;,
        &quot;Principal&quot;: {
          &quot;Service&quot;: [&quot;lambda.amazonaws.com&quot;]
        },
        &quot;Action&quot;: &quot;sts:AssumeRole&quot;
      }
    ]
  }
  EOF
  }
  
  resource &quot;aws_iam_role_policy&quot; &quot;log_forwarder&quot; {
    name = &quot;LogForwarder&quot;
    role = aws_iam_role.log_forwarder.id
  
    policy = &lt;&lt;EOF
  {
    &quot;Version&quot;: &quot;2012-10-17&quot;,
    &quot;Statement&quot;: [
      {
        &quot;Effect&quot;: &quot;Allow&quot;,
        &quot;Action&quot;: [
          &quot;lambda:ListTags&quot;,
          &quot;logs:CreateLogGroup&quot;,
          &quot;logs:CreateLogStream&quot;,
          &quot;logs:PutLogEvents&quot;
        ],
        &quot;Resource&quot;: [
          &quot;arn:aws:logs:*&quot;,
          &quot;arn:aws:lambda:*&quot;
        ]
      },
      {
        &quot;Effect&quot;: &quot;Allow&quot;,
        &quot;Action&quot;: [
          &quot;lambda:InvokeFunction&quot;
        ],
        &quot;Resource&quot;: &quot;*&quot;
      },
          {
      &quot;Sid&quot;: &quot;AWSDatadogPermissionsForCloudtrail&quot;,
      &quot;Effect&quot;: &quot;Allow&quot;,
      &quot;Action&quot;: [&quot;s3:ListBucket&quot;, &quot;s3:GetBucketLocation&quot;, &quot;s3:GetObject&quot;, &quot;s3:ListObjects&quot;],
      &quot;Resource&quot;: [
          &quot;arn:aws:s3:::BucketName&quot;,
          &quot;arn:aws:s3:::BucketName/*&quot;
      ]
  }
  
    ]
  }
  EOF
  }
  
  
  module &quot;DDLogForwarderUSEast1&quot; {
    source                = &quot;./log_forwarder_lambdas&quot;
    dd_log_forwarder_role = aws_iam_role.log_forwarder.arn
    region                = &quot;us-east-1&quot;
  
  }
  
  module &quot;DDLogForwarderUSEast2&quot; {
    source                = &quot;./log_forwarder_lambdas&quot;
    dd_log_forwarder_role = aws_iam_role.log_forwarder.arn
    providers             = { aws = aws.us-east-2 }
    region                = &quot;us-east-2&quot;
  }
  
  module &quot;DDLogForwarderUSWest1&quot; {
    source                = &quot;./log_forwarder_lambdas&quot;
    dd_log_forwarder_role = aws_iam_role.log_forwarder.arn
    providers             = { aws = aws.us-west-1 }
    region                = &quot;us-west-1&quot;
  }
  
  module &quot;DDLogForwarderUSWest2&quot; {
    source                = &quot;./log_forwarder_lambdas&quot;
    dd_log_forwarder_role = aws_iam_role.log_forwarder.arn
    region                = &quot;us-west-2&quot;
    providers             = { aws = aws.us-west-2 }
  }
  
  module &quot;DDLogForwarderAPEast1&quot; {
    source                = &quot;./log_forwarder_lambdas&quot;
    dd_log_forwarder_role = aws_iam_role.log_forwarder.arn
    providers             = { aws = aws.ap-east-1 }
    region                = &quot;ap-east-1&quot;
  }
  
  module &quot;DDLogForwarderAPSouth1&quot; {
    source                = &quot;./log_forwarder_lambdas&quot;
    dd_log_forwarder_role = aws_iam_role.log_forwarder.arn
    region                = &quot;ap-south-1&quot;
    providers             = { aws = aws.ap-south-1 }
  }
  
  ... All AWS Regions with different providers 
  </code></pre>
  <p>TF Config of <code>log_forwarder_lambdas/main.tf</code></p>
  <pre><code>variable &quot;region&quot; {}
  
  variable &quot;account_id&quot; {
    default = &quot;AWS Account Id&quot;
  }
  
  variable &quot;dd_log_forwarder_role&quot; {}
  
  variable &quot;exclude_at_match&quot; {
    default = &quot;([A-Z]* RequestId: .*)&quot;
  }
  
  data &quot;aws_s3_bucket&quot; &quot;cloudtrail_bucket&quot; {
    count  = var.region == &quot;us-west-2&quot; ? 1 : 0
    bucket = &quot;BucketName&quot;
  }
  
  
  resource &quot;aws_lambda_function&quot; &quot;log_forwarder&quot; {
    filename      = &quot;${path.cwd}/log_forwarder_lambdas/aws-dd-forwarder-3.16.3.zip&quot;
    function_name = &quot;log-forwarder&quot;
    role          = var.dd_log_forwarder_role
    description   = &quot;Gathers logs from targetted Cloudwatch Log Groups and sends them to DataDog&quot;
    handler       = &quot;lambda_function.lambda_handler&quot;
    runtime       = &quot;python3.7&quot;
    timeout       = 600
    memory_size   = 1024
    layers        = [&quot;arn:aws:lambda:${var.region}:464622532012:layer:Datadog-Python37:11&quot;]
  
  
    environment {
      variables = {
        DD_ENHANCED_METRICS = false
        EXCLUDE_AT_MATCH    = var.exclude_at_match
      }
    }
  }
  
  resource &quot;aws_cloudwatch_log_group&quot; &quot;log_forwarder&quot; {
    name              = &quot;/aws/lambda/${aws_lambda_function.log_forwarder.function_name}&quot;
    retention_in_days = 90
  }
  
  resource &quot;aws_lambda_permission&quot; &quot;cloudtrail_bucket&quot; {
    count         = var.region == &quot;us-west-2&quot; ? 1 : 0
    statement_id  = &quot;AllowExecutionFromS3Bucket&quot;
    action        = &quot;lambda:InvokeFunction&quot;
    function_name = aws_lambda_function.log_forwarder.arn
    principal     = &quot;s3.amazonaws.com&quot;
    source_arn    = element(data.aws_s3_bucket.cloudtrail_bucket.*.arn, count.index)
  }
  
  resource &quot;aws_s3_bucket_notification&quot; &quot;cloudtrail_bucket_notification&quot; {
    count  = var.region == &quot;us-west-2&quot; ? 1 : 0
    bucket = element(data.aws_s3_bucket.cloudtrail_bucket.*.id, count.index)
  
  
    lambda_function {
      lambda_function_arn = aws_lambda_function.log_forwarder.arn
      events              = [&quot;s3:ObjectCreated:*&quot;]
    }
  
    depends_on = [aws_lambda_permission.cloudtrail_bucket, aws_cloudwatch_log_group.log_forwarder]
  }
  </code></pre>
  <p>I am using TF 0.12.25 in this case.</p>
  <p>The things I have tried so far.</p>
  <ol>
  <li>Remove the .terraform folder from the root module every time I run the Terraform init/plan/apply cycle</li>
  <li>I have tried to refactor code as much as possible.</li>
  <li>I am running the TF Plan/Apply cycle locally without any CI.</li>
  </ol>