CodexBloom - Programming Q&A Platform

Terraform Applying Modules with Variable Overrides Not Reflecting Expected Outputs

๐Ÿ‘€ Views: 71 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-15
terraform aws modules hcl

I'm upgrading from an older version and I'm optimizing some code but I'm having trouble with Terraform when trying to apply variable overrides for a module that provisions several AWS resources. I have a module that creates an S3 bucket and a CloudFront distribution, and I want to set different bucket names based on the environment (dev, staging, prod). However, despite providing the `bucket_name` variable through the `-var` flag during `terraform apply`, the output still reflects the default values from the module. Hereโ€™s how I'm currently configuring my module: ```hcl module "s3_bucket" { source = "./modules/s3" bucket_name = var.bucket_name } ``` And my variables file looks like this: ```hcl variable "bucket_name" { type = string default = "my-default-bucket" } ``` When I run the following command: ```bash tf apply -var="bucket_name=my-dev-bucket" ``` I still see the output as `my-default-bucket` in the Terraform plan and apply output. Iโ€™ve also confirmed that the variable is being passed correctly by printing it inside the module: ```hcl output "bucket_name" { value = var.bucket_name } ``` Iโ€™ve tried reinitializing the Terraform workspace with `terraform init` and also running `terraform plan` before applying, but the behavior remains the same. Is there a specific reason why the variable override isn't being applied in this case? Could it be related to how the module is structured or am I missing something in my Terraform setup? Iโ€™m using Terraform v1.1.9, and I suspect there might be a conflict with the way Iโ€™m trying to pass the variable. Any help would be greatly appreciated! My development environment is Ubuntu. Has anyone else encountered this? I'm on Windows 10 using the latest version of Hcl. Any pointers in the right direction? The project is a CLI tool built with Hcl. Thanks for taking the time to read this!