hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.82k stars 9.17k forks source link

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

Closed mattthaber closed 1 month ago

mattthaber commented 2 months ago

Terraform Core Version

1.9.3

AWS Provider Version

4.51.0

Affected Resource(s)

Expected Behavior

  # 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"
        }
    }

Actual Behavior

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

 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

github-actions[bot] commented 2 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

mattthaber commented 2 months ago

the root cause of this seems to be calling aws ecs create-cluster multiple times with same parameters doesnt error, after the initial creation , it just returns the cluster instead of erroring

aws ecs create-cluster --cluster-name test-cluster
aws ecs create-cluster --cluster-name test-cluster
justinretzolk commented 2 months ago

Hey @mattthaber 👋 Thank you for taking the time to raise this! I noticed you're using a fairly old version of the provider. While this issue awaits prioritization, it may be worth testing with a more recent version to see if the behavior has already been fixed.

github-actions[bot] commented 1 month ago

[!WARNING] This issue has been closed, meaning that any additional comments are hard for our team to see. Please assume that the maintainers will not see them.

Ongoing conversations amongst community members are welcome, however, the issue will be locked after 30 days. Moving conversations to another venue, such as the AWS Provider forum, is recommended. If you have additional concerns, please open a new issue, referencing this one where needed.

github-actions[bot] commented 2 days ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.