CodexBloom - Programming Q&A Platform

Terraform scenarios to Reference S3 Bucket Policy in Module after State Refresh

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-20
terraform aws modules HCL

I'm trying to figure out I'm not sure how to approach I have been working with Terraform version 1.3.6 to manage my AWS infrastructure, and I've encountered an scenario where my S3 bucket policy in a module isn't being correctly referenced after a state refresh..... I've defined the S3 bucket and the policy in a module, but when I run `terraform apply` after a state refresh, I get the following behavior: ``` behavior: Invalid function argument on .terraform/modules/my_module/main.tf line 10, in resource "aws_s3_bucket_policy" "my_policy": 10: bucket = module.my_s3_bucket.bucket_id The argument "bucket" is required, but no argument was found. ``` It seems Terraform is unable to locate the output from the module where I defined the S3 bucket. My module is structured like this: ```hcl module "my_module" { source = "./modules/my_module" bucket_name = var.bucket_name } ``` Inside `my_module`, I have: ```hcl resource "aws_s3_bucket" "my_bucket" { bucket = var.bucket_name } output "bucket_id" { value = aws_s3_bucket.my_bucket.id } ``` And in the root module, I try to use the output like so: ```hcl resource "aws_s3_bucket_policy" "my_policy" { bucket = module.my_module.bucket_id policy = data.aws_iam_policy_document.my_policy.json } ``` I checked the output of `terraform output`, and it correctly shows the bucket ID. I also tried removing the `.terraform` directory and running `terraform init` again, but the scenario continues. Could this be a question with how Terraform caches the state, or is it possible that I have a misconfiguration? Any insights or suggestions on how to resolve this would be greatly appreciated! This issue appeared after updating to Hcl latest. Has anyone else encountered this? I've been using Hcl for about a year now. Thanks for your help in advance!