Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroying some Cloudfront certificates fail on first attempt #6082

Closed
ghost opened this issue Oct 5, 2018 · 9 comments · Fixed by #13513
Closed

Destroying some Cloudfront certificates fail on first attempt #6082

ghost opened this issue Oct 5, 2018 · 9 comments · Fixed by #13513
Assignees
Labels
bug Addresses a defect in current functionality. service/acm Issues and PRs that pertain to the acm service.
Milestone

Comments

@ghost
Copy link

ghost commented Oct 5, 2018

This issue was originally opened by @ophintor as hashicorp/terraform#18997. It was migrated here as a result of the provider split. The original body of the issue is below.


When I run a terraform destroy I get the error below:

Error: Error applying plan:

2 error(s) occurred:

* module.event_query_service.aws_acm_certificate.reporting_domain (destroy): 1 error(s) occurred:

* aws_acm_certificate.reporting_domain: Error deleting certificate: ResourceInUseException: Certificate arn:aws:acm:us-east-1:353795026198:certificate/e5ade17b-fcf4-4da7-941c-d8c89b0dcbb3 in account 353795026198 is in use.
        status code: 400, request id: d67726cc-c7b9-11e8-9ddd-fb35ba3de3d5
* module.event_receipt_service.aws_acm_certificate.reporting_domain (destroy): 1 error(s) occurred:

* aws_acm_certificate.reporting_domain: Error deleting certificate: ResourceInUseException: Certificate arn:aws:acm:us-east-1:353795026198:certificate/242ff6fc-d918-41ad-a8cf-760e40fbfb26 in account 353795026198 is in use.
        status code: 400, request id: d5f37794-c7b9-11e8-9ddd-fb35ba3de3d5

If I run it again it deletes them properly:

...
module.event_query_service.aws_acm_certificate.reporting_domain: Still destroying... (ID: arn:aws:acm:us-east-1:353795026198:cert...e/e5ade17b-fcf4-4da7-941c-d8c89b0dcbb3, 50s elapsed)
module.event_receipt_service.aws_acm_certificate.reporting_domain: Still destroying... (ID: arn:aws:acm:us-east-1:353795026198:cert...e/242ff6fc-d918-41ad-a8cf-760e40fbfb26, 50s elapsed)
module.event_receipt_service.aws_acm_certificate.reporting_domain: Still destroying... (ID: arn:aws:acm:us-east-1:353795026198:cert...e/242ff6fc-d918-41ad-a8cf-760e40fbfb26, 1m0s elapsed)
module.event_query_service.aws_acm_certificate.reporting_domain: Still destroying... (ID: arn:aws:acm:us-east-1:353795026198:cert...e/e5ade17b-fcf4-4da7-941c-d8c89b0dcbb3, 1m0s elapsed)
module.event_receipt_service.aws_acm_certificate.reporting_domain: Still destroying... (ID: arn:aws:acm:us-east-1:353795026198:cert...e/242ff6fc-d918-41ad-a8cf-760e40fbfb26, 1m10s elapsed)
module.event_query_service.aws_acm_certificate.reporting_domain: Still destroying... (ID: arn:aws:acm:us-east-1:353795026198:cert...e/e5ade17b-fcf4-4da7-941c-d8c89b0dcbb3, 1m10s elapsed)
module.event_receipt_service.aws_acm_certificate.reporting_domain: Still destroying... (ID: arn:aws:acm:us-east-1:353795026198:cert...e/242ff6fc-d918-41ad-a8cf-760e40fbfb26, 1m20s elapsed)
module.event_query_service.aws_acm_certificate.reporting_domain: Still destroying... (ID: arn:aws:acm:us-east-1:353795026198:cert...e/e5ade17b-fcf4-4da7-941c-d8c89b0dcbb3, 1m20s elapsed)
module.event_receipt_service.aws_acm_certificate.reporting_domain: Destruction complete after 1m21s
module.event_query_service.aws_acm_certificate.reporting_domain: Destruction complete after 1m21s

Destroy complete! Resources: 2 destroyed.

There are other modules with acm certificates that get deleted on the first go but for some reason the two above need terraform destroy to be run twice. The code for the acm certs is pretty much the same, but these are the only two certs used for API GWs:

resource "aws_acm_certificate" "reporting_domain" {
  provider          = "aws.us-east-1"
  domain_name       = "${format("%s.%s",local.reporting_component, local.reporting_domain)}"
  validation_method = "DNS"
  tags              = "${merge(var.tags, map("Environment", "${terraform.workspace}"))}"

  lifecycle {
    create_before_destroy = true
  }
}
resource "aws_api_gateway_domain_name" "reporting_service" {
  domain_name     = "${format("%s.%s",local.reporting_component, local.reporting_domain)}"
  certificate_arn = "${aws_acm_certificate.reporting_domain.arn}"
  depends_on      = ["aws_acm_certificate_validation.reporting_domain"]
}

Terraform and AWS providers are at the latest versions:

$ terraform -v
Terraform v0.11.8
+ provider.archive v1.1.0
+ provider.aws v1.39.0
+ provider.null v1.0.0
+ provider.random v2.0.0
+ provider.template v1.0.0
+ provider.tls v1.2.0

How can I troubleshoot this further? Many thanks.

PS. I am aware of this one (which sounds exactly the same as my problem) --> #3866
But as mentioned above I am on newer versions and instead of ELBs the certs are used with API GWs.

Cheers.

@bflad bflad added bug Addresses a defect in current functionality. service/acm Issues and PRs that pertain to the acm service. labels Oct 5, 2018
@ophintor
Copy link

Not sure if the answer is too obvious or too difficult? :-)

Would be good to have some feedback on this, thanks!

@Andrew-Tan
Copy link

Andrew-Tan commented Jun 6, 2019

+1 on this

I think the cause of this issue is, cloudfront (or api gateway) resource in Terraform in general depends on acm certificates to be destroyed first. However, on AWS, you cannot delete a certificate when it is in use (by cloudfront, for example). So this potentially this creates a circular dependency that Terraform is not aware of.

In other words:
The correct order of procedure should be:

  • Change/Delete custom domain certificate settings in Cloudfront (API Gateway) to release lock on the certificate
  • Delete certificate
  • Change/Delete Cloudfront (API Gateway) resources

While in reality, Terraform might be doing only the last 2 steps:

  • Delete certificate (locked because Cloudfront is using it)
  • Change/Delete Cloudfront (API Gateway) resources

I'm not sure if this is a problem that needs to be solved on the Terraform side or AWS provider side..

@Andrew-Tan
Copy link

By the way, the reason why I made this guess is, through my experiment, after Terraform returns error of certificate in use, I went to checked the custom domain settings in my API Gateway resource and noticed that it is still set to the certificate to be destroyed, which means terraform didn't set it before destroying the certificate to release the lock.

@nikhilbalekundargi
Copy link

Facing the same issue. Any update on mitigating this?

@bransynluther
Copy link

This issue is still plaguing me, any chance this has been fixed? My terraform destroy command cannot destroy my cloudfront distribution that is using a aws_acm_certificate, because the certificate does not get destroyed either... halp meh =(

@TheFLHurricane
Copy link

Can we get some traction on this? The current timeout on destroy is 10 minutes but the average time for a cert to become aware that it is no longer in use by an API Gateway Custom Domain is ~15 minutes. Adjusting the timeout to 20 minutes seems reasonable and sufficient. Or giving the ability to adjust the timeouts on this particular resource would also suffice.

@bflad bflad self-assigned this May 27, 2020
bflad added a commit that referenced this issue May 27, 2020
Reference: #3855
Reference: #6082
Reference: #8755
Reference: #12075
Reference: #13053

Notable changes:

```
ENHANCEMENTS:

* resource/aws_acm_certificate: Add `status` attribute

BUG FIXES:

* resource/aws_acm_certificate: Detect `AMAZON_ISSUED` type `validation_method` value directly from API response instead of custom logic
* resource/aws_acm_certificate: Increase deletion retries from 10 minutes to 20 minutes (better support API Gateway Custom Domain deletion)
```

Other changes:

- Documents `subject_alternative_names` argument removal procedures
- Improves potentially confusing error message during asynchronous ACM validation assignment

Output from acceptance testing:

```
--- PASS: TestAccAWSAcmCertificate_imported_IpAddress (36.64s)
--- PASS: TestAccAWSAcmCertificate_root_TrailingPeriod (37.74s)
--- PASS: TestAccAWSAcmCertificate_wildcard (38.86s)
--- PASS: TestAccAWSAcmCertificate_wildcardAndRootSan (39.08s)
--- PASS: TestAccAWSAcmCertificate_emailValidation (39.11s)
--- PASS: TestAccAWSAcmCertificate_dnsValidation (39.35s)
--- PASS: TestAccAWSAcmCertificate_rootAndWildcardSan (39.62s)
--- PASS: TestAccAWSAcmCertificate_disableCTLogging (41.62s)
--- PASS: TestAccAWSAcmCertificate_san_single (41.93s)
--- PASS: TestAccAWSAcmCertificate_san_TrailingPeriod (42.00s)
--- PASS: TestAccAWSAcmCertificate_san_multiple (42.05s)
--- PASS: TestAccAWSAcmCertificate_root (42.07s)
--- PASS: TestAccAWSAcmCertificate_privateCert (47.46s)
--- PASS: TestAccAWSAcmCertificate_imported_DomainName (54.51s)
--- PASS: TestAccAWSAcmCertificate_tags (85.67s)
```
@bflad bflad added this to the v2.65.0 milestone Jun 4, 2020
@bflad
Copy link
Contributor

bflad commented Jun 4, 2020

The deletion retry timeout has been increased to 20 minutes to better account for this type of ACM Certificate usage where infrastructure deletion does not provide deletion status information (e.g. Cognito User Cool Custom Domains). This will release with version 2.65.0 of the Terraform AWS Provider, likely later today. 👍

@ghost
Copy link
Author

ghost commented Jun 5, 2020

This has been released in version 2.65.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@ghost
Copy link
Author

ghost commented Jul 5, 2020

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Jul 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/acm Issues and PRs that pertain to the acm service.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants