terraform-provider-aws icon indicating copy to clipboard operation
terraform-provider-aws copied to clipboard

[Bug]: creating `aws_ecs_cluster` imports existing cluster automatically if it already exists

Open mattthaber opened this issue 6 months ago • 3 comments

Terraform Core Version

1.9.3

AWS Provider Version

4.51.0

Affected Resource(s)

  • aws_ecs_cluster

Expected Behavior

  • in two seperate workspaces run the below code twice (exact same cluster/name)
  # aws_ecs_cluster.main will be created
  + resource "aws_ecs_cluster" "main" {
      + arn                = (known after apply)
      + capacity_providers = (known after apply)
      + id                 = (known after apply)
      + name               = "stg-hogwarts"
      + tags_all           = {
          + "Environment" = "stg"
          + "Region"      = "us-west-2"
          + "Repo"        = "template-terraform"
          + "Team"        = "DevOps"
          + "Tenant"      = "multitenant"
        }

      + default_capacity_provider_strategy (known after apply)

      + setting {
          + name  = "containerInsights"
          + value = "disabled"
        }
    }

  • when creating an ecs_cluster that has a name that already exists, it should fail and say something like "cluster already exists"

Actual Behavior

  • instead of failing and saying cluster already exists, it auto imports the existing cluster the exact same way an import{} block would work.

whats weird , the aws cli returns 200 and shows in the response 0 active services but then in the subsequent ecsDescribeCluster picks up the existing services that have already been deployed.

I can only assume the root problem here is the underlying aws api, but it is really bad IMO that you can accidently import an existing aws resource without using an import{} block. This can lead to incredibly bad scenerios where two workspaces are unknowingly managing the same resource and can cause major drift changes

2024-08-14T21:17:41.416-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: {"capacityProviders":["FARGATE_SPOT","FARGATE"],"cluster":"stg-hogwarts","defaultCapacityProviderStrategy":[{"base":0,"capacityProvider":"FARGATE","weight":0},{"base":0,"capacityProvider":"FARGATE_SPOT","weight":1}]}
2024-08-14T21:17:41.416-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: -----------------------------------------------------
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: [DEBUG] [aws-sdk-go] DEBUG: Response ecs/PutClusterCapacityProviders Details:
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: ---[ RESPONSE ]--------------------------------------
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: HTTP/1.1 200 OK
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: Content-Length: 569
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: Content-Type: application/x-amz-json-1.1
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: Date: Thu, 15 Aug 2024 01:17:40 GMT
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: X-Amzn-Requestid: 99f9723a-791f-4796-9495-27fed38e1f81
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: -----------------------------------------------------
2024-08-14T21:17:41.533-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: [DEBUG] [aws-sdk-go] {"cluster":{"activeServicesCount":0,"attachments":[],"attachmentsStatus":"UPDATE_COMPLETE","capacityProviders":["FARGATE","FARGATE_SPOT"],"clusterArn":"arn:aws:ecs:us-west-2:xxxxx:cluster/stg-hogwarts","clusterName":"stg-hogwarts","defaultCapacityProviderStrategy":[{"base":0,"capacityProvider":"FARGATE","weight":0},{"base":0,"capacityProvider":"FARGATE_SPOT","weight":1}],"pendingTasksCount":0,"registeredContainerInstancesCount":0,"runningTasksCount":0,"settings":[{"name":"containerInsights","value":"disabled"}],"statistics":[],"status":"ACTIVE","tags":[]}}



2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: [DEBUG] [aws-sdk-go] DEBUG: Response ecs/DescribeClusters Details:
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: ---[ RESPONSE ]--------------------------------------
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: HTTP/1.1 200 OK
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: Content-Length: 724
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: Content-Type: application/x-amz-json-1.1
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: Date: Thu, 15 Aug 2024 01:17:51 GMT
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: X-Amzn-Requestid: 5ba3eeee-dbc7-4816-9ac4-7f17da23760c
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: -----------------------------------------------------
2024-08-14T21:17:51.940-0400 [DEBUG] provider.terraform-provider-aws_v4.51.0_x5: [DEBUG] [aws-sdk-go] {"clusters":[{"activeServicesCount":65,"capacityProviders":["FARGATE","FARGATE_SPOT"],"clusterArn":"arn:aws:ecs:us-west-2:xxxxx:cluster/stg-hogwarts","clusterName":"stg-hogwarts","defaultCapacityProviderStrategy":[{"base":0,"capacityProvider":"FARGATE","weight":0},{"base":0,"capacityProvider":"FARGATE_SPOT","weight":1}],"pendingTasksCount":0,"registeredContainerInstancesCount":0,"runningTasksCount":60,"settings":[{"name":"containerInsights","value":"disabled"}],"statistics":[],"status":"ACTIVE","tags":[{"key":"Environment","value":"stg"},{"key":"Region","value":"us-west-2"},{"key":"Team","value":"DevOps"},{"key":"Tenant","value":"multitenant"},{"key":"Repo","value":"terraform-applications"}]}],"failures":[]}

Relevant Error/Panic Output Snippet

Terraform Configuration Files

resource "aws_ecs_cluster" "main" {
  name = "stg-hogwarts"

  setting {
    name  = "containerInsights"
    value = "disabled"
  }
}

Steps to Reproduce

  • launch the same ecs cluster in two separate terraform workspaces but in the same aws account/region.
  • the second will claim it "created" the cluster, but instead is actually "importing" and existing cluster
 master [!?] terraform workspace select stg
Switched to workspace "stg".
 master [!?] terraform apply
aws_ecs_cluster_capacity_providers.default: Refreshing state... [id=stg-hogwarts]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_ecs_cluster.main will be created
  + resource "aws_ecs_cluster" "main" {
      + arn                = (known after apply)
      + capacity_providers = (known after apply)
      + id                 = (known after apply)
      + name               = "dev-test"
      + tags_all           = {
          + "Environment" = "stg"
          + "Region"      = "us-west-2"
          + "Repo"        = "template-terraform"
          + "Team"        = "DevOps"
          + "Tenant"      = "multitenant"
        }

      + default_capacity_provider_strategy (known after apply)

      + setting {
          + name  = "containerInsights"
          + value = "disabled"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions in workspace "stg"?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_ecs_cluster.main: Creating...
aws_ecs_cluster.main: Still creating... [10s elapsed]
aws_ecs_cluster.main: Creation complete after 11s [id=arn:aws:ecs:us-west-2:123456789:cluster/dev-test]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

template-terraform ❯ on  master [!?] terraform workspace select dev
Switched to workspace "dev".
template-terraform ❯ on  master [!?] terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_ecs_cluster.main will be created
  + resource "aws_ecs_cluster" "main" {
      + arn                = (known after apply)
      + capacity_providers = (known after apply)
      + id                 = (known after apply)
      + name               = "dev-test"
      + tags_all           = {
          + "Environment" = "stg"
          + "Region"      = "us-west-2"
          + "Repo"        = "template-terraform"
          + "Team"        = "DevOps"
          + "Tenant"      = "multitenant"
        }

      + default_capacity_provider_strategy (known after apply)

      + setting {
          + name  = "containerInsights"
          + value = "disabled"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions in workspace "dev"?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_ecs_cluster.main: Creating...
aws_ecs_cluster.main: Still creating... [10s elapsed]
aws_ecs_cluster.main: Creation complete after 11s [id=arn:aws:ecs:us-west-2:123456789:cluster/dev-test]

here you can see me switching workspaces and creating the same resource. both resources claim they created the EXACT SAME arn for ecs-cluster, which seems... wrong because the 2nd workspace apply did not create it

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

mattthaber avatar Aug 15 '24 01:08 mattthaber