Alex
Alex

Reputation: 903

Terraform plan does not include all of my .tf changes

I am using AWS provider. I've added transaction blocks in my lifecycle_rule block with the appropriate days and storage_class properties. Besides that change I've also increased the expiry_days from 30 to 180.

The variable looks like this:

variable "bucket_details" {
  type = map(object({
    bucket_name = string
    purpose =     string
    infrequent_transition_days = number
    infrequent_transition_storage = string
    archive_transition_days = number
    archive_transition_storage = string
    expiry_days = number
    versioning =  bool
  }))
}

The resource looks like this: (I've removed unrelated configs)

resource "aws_s3_bucket" "bucket-s3" {
  for_each = var.bucket_details

  bucket = "${each.key}-${var.region}-${var.environment}"

  lifecycle_rule {
    id =      "clear"
    enabled = true

    transition {
      days = each.value.infrequent_transition_days
      storage_class = each.value.infrequent_transition_storage
    }

    transition {
      days = each.value.archive_transition_days
      storage_class = each.value.archive_transition_storage
    }
  
    expiration {
      days = each.value.expiry_days
    }
  }
}

I've followed this transition example for reference.

When I run transaction plan I get the following output:

~ lifecycle_rule {
    abort_incomplete_multipart_upload_days = 0
    enabled                                = true
    id                                     = "clear"
    tags                                   = {}

  + expiration {
      + days = 180
    }
  - expiration {
      - days                         = 30 -> null
      - expired_object_delete_marker = false -> null
    }
}

No transition changes listed. Could it be because transition is AWS-specific and thus Terraform does not catch it?

Upvotes: 0

Views: 221

Answers (1)

tomarv2
tomarv2

Reputation: 833

I tried your code as is and here is the response:

provider "aws" {
  region = "us-west-2"
}

variable "region" {
  default = "us-west-2"
}

variable "environment" {
  default = "dev"
}

variable "bucket_details" {
  type = map(object({
    bucket_name = string
    infrequent_transition_days = number
    infrequent_transition_storage = string
    archive_transition_days = number
    archive_transition_storage = string
    expiry_days = number
  }))
  default = {
    hello_world = {
    bucket_name: "demo-001",
    infrequent_transition_days: 10,
    infrequent_transition_storage: "STANDARD_IA",
    archive_transition_days: 10,
    archive_transition_storage: "GLACIER",
    expiry_days = 30
  }}
}

resource "aws_s3_bucket" "bucket-s3" {
  for_each = var.bucket_details

  bucket = "${each.key}-${var.region}-${var.environment}"

  lifecycle_rule {
    id =      "clear"
    enabled = true

    transition {
      days = each.value.infrequent_transition_days
      storage_class = each.value.infrequent_transition_storage
    }

    transition {
      days = each.value.archive_transition_days
      storage_class = each.value.archive_transition_storage
    }

    expiration {
      days = each.value.expiry_days
    }
  }
}

Response of Terraform plan:


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_s3_bucket.bucket-s3["hello_world"] will be created
  + resource "aws_s3_bucket" "bucket-s3" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = "hello_world-us-west-2-dev"
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags_all                    = (known after apply)
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + lifecycle_rule {
          + enabled = true
          + id      = "clear"

          + expiration {
              + days = 30
            }

          + transition {
              + days          = 10
              + storage_class = "GLACIER"
            }
          + transition {
              + days          = 10
              + storage_class = "STANDARD_IA"
            }
        }

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply"
now.

As you can see there are transition changes. Can you try setting defaults vars and check the response.

Upvotes: 2

Related Questions