lorenzo454
lorenzo454

Reputation: 9

Refactoring aws_s3_bucket resource using aws_s3_bucket_* resources but having issues with the lifecycle_policy being destroyed

I am trying to refactor my aws_s3_bucket resource into the updated aws_s3_bucket_* resources to eliminate the deprecated arguments message when running Terraform. I added and imported the resources, but the original aws_s3_bucket resource is trying to destroy the lifecycle policy.

My original code:

resource "aws_s3_bucket" "example_bucket" {
  bucket = "my-tf-test-bucket"
  acl = "private"
  lifecycle_rule {
    id = "expire_after_30_days"
    enabled = true
    expiration {
        days = 30
    }
  }
  tags = {
    Name        = "My bucket"
    Environment = "Dev"
  }
}

New code:

resource "aws_s3_bucket" "example_bucket" {
  bucket = "my-tf-test-bucket"
}

resource "aws_s3_bucket_acl" "example_bucket_acl" {
  bucket = aws_s3_bucket.example_bucket.id
  acl    = "private"
}

resource "aws_s3_bucket_lifecycle_configuration" "example_lifecycle_policy" {
  bucket = aws_s3_bucket.example_bucket.id

  rule {
    id = "expire_after_30_days"

    expiration {
        days = 30
    }

    status = "Enabled"
  }
}

I imported the state for the acl and the lifecycle configuration resources.

Ran a plan and expected to see no resources being destroyed or created. However I am seeing that the lifecycle_rule in the example_bucket is getting destroyed, as if it hasn't picked up the import. I tested this by allowing Terraform to destroy the resource, and then target applied the aws_s3_bucket_lifecycle_configuration resource to create it again. I then ran a plan but the example_bucket is still trying to destroy the policy.

Anyone got any ideas on how to resolve this?

Upvotes: 0

Views: 144

Answers (1)

Alex Harvey
Alex Harvey

Reputation: 15492

I note that in your refactored "new code" example, you have changed the name of the resource block from example to example_bucket. That would cause the resource to be recreated. I assume that is the issue you faced.

If you imported the resource using:

terraform import aws_s3_bucket_lifecycle_configuration.example_lifecycle_policy my-tf-test-bucket-1234abcd

This should have worked fine.

Upvotes: 0

Related Questions