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.64k stars 9.03k forks source link

[Bug]: aws_s3_bucket infinite diff when using datasource as part of the name #38303

Open carlosjgp opened 2 weeks ago

carlosjgp commented 2 weeks ago

Terraform Core Version

1.5.6

AWS Provider Version

5.56.0

Affected Resource(s)

Expected Behavior

No differences in plan or apply

Actual Behavior

On every plan or apply we get a long list of changes related to S3 resources. Bucket, policies...

when the bucket is detected as "to be replaced" all the resources using it are also marked to be replaced

Relevant Error/Panic Output Snippet

-/+ resource "aws_s3_bucket" "pipeline" {
      + acceleration_status         = (known after apply)
      + acl                         = (known after apply)
      ~ arn                         = "arn:aws:s3:::<application>-pipeline-<region>-<account alias>" -> (known after apply)
      ~ bucket                      = "<application>-pipeline-<region>-<account alias>" # forces replacement -> (known after apply) # forces replacement
      ~ bucket_domain_name          = "<application>-pipeline-<region>-<account alias>.s3.amazonaws.com" -> (known after apply)
      + bucket_prefix               = (known after apply)
      ~ bucket_regional_domain_name = "<application>-pipeline-<region>-<account alias>.s3.eu-west-1.amazonaws.com" -> (known after apply)
      ~ hosted_zone_id              = "XXXXXXXXXXX" -> (known after apply)
      ~ id                          = "<application>-pipeline-<region>-<account alias>" -> (known after apply)
      ~ object_lock_enabled         = false -> (known after apply)
      ~ policy                      = jsonencode(

where

application   = "my-app"
region        = data.aws_region.current.name
account alias = data.aws_iam_account_alias.current.account_alias

Terraform Configuration Files

resource "aws_s3_bucket" "pipeline" {
  bucket = "myapp-pipeline-${data.aws_region.current.name}-${data.aws_iam_account_alias.current.account_alias}"

  force_destroy = true
}

resource "aws_s3_bucket_policy" "pipeline" {
  bucket = aws_s3_bucket.pipeline.id
  policy = data.aws_iam_policy_document.codepipeline_s3_policy.json
}

data "aws_iam_policy_document" "codepipeline_s3_policy" {
  statement {
    sid       = "DenyUnEncryptedObjectUploads"
    effect    = "Deny"
    resources = ["${aws_s3_bucket.pipeline.arn}/*"]
    actions   = ["s3:PutObject"]

    condition {
      test     = "StringNotEquals"
      variable = "s3:x-amz-server-side-encryption"
      values   = ["aws:kms"]
    }

    principals {
      type        = "*"
      identifiers = ["*"]
    }
  }
}

terraform {
  required_version = ">= 1.5"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 5.26.0"
    }
  }
}

Steps to Reproduce

terraform init terraform apply -auto-approve terraform plan # <--- diff!? terraform apply -auto-approve terraform plan # <--- diff!?

Debug Output

No response

Panic Output

No response

Important Factoids

I understand that the some datasources might change values on apply but in this particular case these are quite immutable

We should be able to use these in a very reliable way

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 2 weeks ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

justinretzolk commented 2 weeks ago

Hey @carlosjgp 👋 Thank you for taking the time to raise this! We'd need a bit more information to determine exactly what's going on here, I suspect. Are you able to supply a complete configuration that can be used to reproduce this (containing the data source definitions as well) and/or debug logs (redacted as needed)?