CodexBloom - Programming Q&A Platform

Terraform not applying changes to existing S3 bucket policy despite correct configuration

👀 Views: 50 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-01
terraform aws s3 policy HCL

I've been banging my head against this for hours. I'm sure I'm missing something obvious here, but I'm experiencing an scenario where Terraform is not applying changes to an existing S3 bucket policy. I have updated the policy in my Terraform configuration, but when I run `terraform apply`, it doesn't seem to recognize the changes, and the output indicates that there are no changes to apply. Here's the relevant snippet of my configuration: ```hcl resource "aws_s3_bucket_policy" "my_bucket_policy" { bucket = aws_s3_bucket.my_bucket.id policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Principal = "*" Action = "s3:GetObject" Resource = "${aws_s3_bucket.my_bucket.arn}/*" } ] }) } ``` I made a change to allow `s3:PutObject` in addition to `s3:GetObject`, and my updated policy looks like this: ```hcl Statement = [ { Effect = "Allow" Principal = "*" Action = ["s3:GetObject", "s3:PutObject"] Resource = "${aws_s3_bucket.my_bucket.arn}/*" } ] ``` Despite these changes, when I run `terraform plan`, it shows no changes, and during `terraform apply`, it simply states "No changes needed". I have also checked if the bucket exists and the IAM permissions are correct for the user running the Terraform commands. I've tried manually destroying and recreating the bucket policy, but that also doesn't work as expected. Can someone guide to understand why Terraform isn't detecting changes to the S3 bucket policy? Is there a specific setting or command I might be overlooking? Am I missing something obvious? Is there a better approach?