# Trying to migrate off of aws cloudfront origin\_access\_identity but apply graphs incorrectly

**URL:** <https://discuss.hashicorp.com/t/trying-to-migrate-off-of-aws-cloudfront-origin-access-identity-but-apply-graphs-incorrectly/51857>\
**Category:** Terraform\
**Created:** [March 24, 2023, 2:05pm UTC](https://discuss.hashicorp.com/t/trying-to-migrate-off-of-aws-cloudfront-origin-access-identity-but-apply-graphs-incorrectly/51857 "2023-03-24T14:05:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grimm26](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/grimm26/32/1623_2.png) [@grimm26](https://discuss.hashicorp.com/u/grimm26)\
**Post date:** [March 24, 2023, 2:05pm UTC](https://discuss.hashicorp.com/t/trying-to-migrate-off-of-aws-cloudfront-origin-access-identity-but-apply-graphs-incorrectly/51857/1 "2023-03-24T14:05:45Z")

</div>

I’m using `aws_cloudfront_origin_access_identity` with cloudfront and am working on changing my terraform to migrate to `aws_cloudfront_origin_access_control` as recommended. I’m at the step where I am removing the `aws_cloudfront_origin_access_identity` from cloudfront and destroying the `aws_cloudfront_origin_access_control` itself, but terraform always fails the apply because it tries to destroy the `aws_cloudfront_origin_access_control` before removing its reference from the `aws_cloudfront_distribution` resource.

```auto
module.test-static.aws_cloudfront_origin_access_identity.origin_access_identity[0]: Destroying... [id=E3O7TE8TTRI6UF]
╷
│ Error: deleting Amazon CloudFront Origin Access Identity (E3O7TE8TTRI6UF): CloudFrontOriginAccessIdentityInUse: The CloudFront origin access identity is still being used.
│ status code: 409, request id: 461cbcdb-357f-4c57-8b5f-5043cb1fa327

```

The OAI is referenced as `aws_cloudfront_origin_access_identity.origin_access_identity[0].cloudfront_access_identity_path ` in the `aws_cloudfront_distribution` resource, and the `aws_cloudfront_origin_access_identity` resource doesn’t reference anything.

Here’s a snippet:

```auto
resource "aws_cloudfront_origin_access_identity" "origin_access_identity" {
  count = var.enable_origin_access_identity ? 1 : 0
  provider = aws.cloudfront
  comment = "Access Identity for Cloudfront for ${local.product} ${local.service} ${local.environment}"
}

resource "aws_cloudfront_origin_access_control" "oac" {
  count = local.enable_origin_access_control ? 1 : 0
  provider = aws.cloudfront
  name = substr("${local.cloudfront_name}_${local.origin_id}", 0, 63)
  description = "Lockdown control to ${local.origin_id}"
  origin_access_control_origin_type = "s3"
  signing_behavior = "always"
  signing_protocol = "sigv4"
}

resource "aws_cloudfront_distribution" "d" {
  count = var.enable_cloudfront ? 1 : 0
  provider = aws.cloudfront

  origin {
    domain_name = aws_s3_bucket.primary.bucket_regional_domain_name
    origin_id = local.origin_id
    origin_access_control_id = local.enable_origin_access_control ? aws_cloudfront_origin_access_control.oac[0].id : null

    dynamic "s3_origin_config" {
      for_each = var.enable_origin_access_identity ? [true] : []
      content {
        origin_access_identity = aws_cloudfront_origin_access_identity.origin_access_identity[0].cloudfront_access_identity_path
      }
    }
  }....

```

---

<div class="post-metadata">

**Author:** ![maxb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/maxb/32/26655_2.png) [@maxb](https://discuss.hashicorp.com/u/maxb)\
**Post date:** [March 24, 2023, 9:44pm UTC](https://discuss.hashicorp.com/t/trying-to-migrate-off-of-aws-cloudfront-origin-access-identity-but-apply-graphs-incorrectly/51857/2 "2023-03-24T21:44:07Z")

</div>

Add

```auto
  lifecycle {
    create_before_destroy = true
  }

```

to the `aws_cloudfront_distribution`.

It is incredibly hard to understand the details of what this actually does - I think I’ve read through [terraform/destroying.md at main · hashicorp/terraform · GitHub](https://github.com/hashicorp/terraform/blob/main/docs/destroying.md) about 5 times now, and only about ~80% understand what is going on, especially as the user-facing docs only explain this option in terms of what happens when replacing the resource it is set on.

However, in addition to that meaning, it also influences the order of operations between **updating** the distribution, and destroying things the distribution depends on.

---

<div class="post-metadata">

**Author:** ![grimm26](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/grimm26/32/1623_2.png) [@grimm26](https://discuss.hashicorp.com/u/grimm26)\
**Post date:** [March 24, 2023, 10:39pm UTC](https://discuss.hashicorp.com/t/trying-to-migrate-off-of-aws-cloudfront-origin-access-identity-but-apply-graphs-incorrectly/51857/3 "2023-03-24T22:39:46Z")

</div>

yeah that doesn’t help. The distribution isn’t getting recreated anyway.

---

<div class="post-metadata">

**Author:** ![grimm26](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/grimm26/32/1623_2.png) [@grimm26](https://discuss.hashicorp.com/u/grimm26)\
**Post date:** [March 24, 2023, 10:43pm UTC](https://discuss.hashicorp.com/t/trying-to-migrate-off-of-aws-cloudfront-origin-access-identity-but-apply-graphs-incorrectly/51857/4 "2023-03-24T22:43:52Z")

</div>

that does give me an idea of just leaving the `aws_cloudfront_origin_access_identity` resource in the module without a count on it. Then once I get everyone off of that, I’ll remove it from the module.

Still vexing that it doesn’t remove it in the right order.
