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.62k stars 9.01k forks source link

[Bug]: Replacement of aws_cognito_user_pool_domain fails #28124

Open De117 opened 1 year ago

De117 commented 1 year ago

Terraform Core Version

1.3.1

AWS Provider Version

4.44.0

Affected Resource(s)

aws_cognito_user_pool_domain

Expected Behavior

The Cognito user pool domain, after being marked for destruction, should be cleanly replaced.

Actual Behavior

The old Cognito user pool domain is destroyed, but the new one is not created. Instead, deployment breaks with an error.

Rerunning terraform apply many minutes later succeeds.

Relevant Error/Panic Output Snippet

...

Terraform will perform the following actions:

  # aws_cognito_user_pool_domain.main is tainted, so must be replaced
-/+ resource "aws_cognito_user_pool_domain" "main" {
      ~ aws_account_id              = "123456789012" -> (known after apply)
      ~ cloudfront_distribution_arn = "d8u9rcqe4ple9.cloudfront.net" -> (known after apply)
      ~ id                          = "terraform-bug.yourdomain.here" -> (known after apply)
      ~ s3_bucket                   = "aws-cognito-prod-dub-assets" -> (known after apply)
      ~ version                     = "20221201170922" -> (known after apply)
        # (3 unchanged attributes hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.
aws_cognito_user_pool_domain.main: Destroying... [id=terraform-bug.yourdomain.here]
aws_cognito_user_pool_domain.main: Destruction complete after 2s
aws_cognito_user_pool_domain.main: Creating...
╷
│ Error: Error creating Cognito User Pool Domain: InvalidParameterException: One or more of the CNAMEs you provided are already associated with a different resource. (Service: AmazonCloudFront; Status Code: 409; Error Code: CNAMEAlreadyExists; Request ID: a2e4593f-5933-4461-ba2e-0107d603f3b8; Proxy: null)
│
│   with aws_cognito_user_pool_domain.main,
│   on test.tf line 67, in resource "aws_cognito_user_pool_domain" "main":
│   67: resource "aws_cognito_user_pool_domain" "main" {
│
╵

Terraform Configuration Files

# Replace "..." with your profile and hosted zone ID
provider "aws" {
  region  = "eu-west-1"
  profile = "..."
}

provider "aws" {
  alias   = "north_virginia"
  region  = "us-east-1"
  profile = "..."
}

locals {
  hosted_zone_id = "..."
}

#################### DEPENDENCIES ####################

# Cognito needs a parent domain with an A record.
resource "aws_route53_record" "parent" {
  zone_id = local.hosted_zone_id
  name    = ""
  type    = "A"
  ttl     = 60
  records = ["1.1.1.1"]
}

resource "aws_route53_record" "main" {
  zone_id = local.hosted_zone_id
  name    = "terraform-bug"
  type    = "CNAME"
  ttl     = 60
  records = ["www.example.com"]
}

resource "aws_acm_certificate" "main" {
  provider = aws.north_virginia

  validation_method = "DNS"
  domain_name = aws_route53_record.main.fqdn
}

resource "aws_route53_record" "domain_for_certificate_validation" {
  provider = aws.north_virginia

  zone_id = local.hosted_zone_id
  type = tolist(aws_acm_certificate.main.domain_validation_options).0.resource_record_type
  name = tolist(aws_acm_certificate.main.domain_validation_options).0.resource_record_name
  records = [tolist(aws_acm_certificate.main.domain_validation_options).0.resource_record_value]
  ttl = "60"
}

resource "aws_acm_certificate_validation" "main" {
  provider = aws.north_virginia

  certificate_arn = aws_acm_certificate.main.arn
  validation_record_fqdns = [aws_route53_record.domain_for_certificate_validation.fqdn]
}

#################### RELEVANT RESOURCES ####################

resource "aws_cognito_user_pool" "main" {
  name = "demo"
}

resource "aws_cognito_user_pool_domain" "main" {
  depends_on = [aws_acm_certificate_validation.main]

  domain          = aws_route53_record.main.fqdn
  user_pool_id    = aws_cognito_user_pool.main.id
  certificate_arn = aws_acm_certificate.main.arn
}

Steps to Reproduce

With the configuration file in the working directory:

terraform apply  # initial deployment
terraform taint aws_cognito_user_pool_domain.main
terraform apply  # error

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

This may be related to issue #5313. This SO question seems to describe this bug. (Failure to recreate a aws_cognito_user_pool_domain.)

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

endre-synnes commented 11 months ago

I just now had the same exact issue, with identical code, using the 4.40.0 AWS provider version.

For now the only workaround I have found is to manually delete the Route53 record (using the AWS console), and run terraform apply again.