CodexBloom - Programming Q&A Platform

Terraform: implementing Conditional Resources Not Being Created Based on Variable Input

๐Ÿ‘€ Views: 0 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-14
terraform aws s3 HCL

I'm stuck trying to I'm stuck on something that should probably be simple... I'm sure I'm missing something obvious here, but I'm experiencing a question with conditional resource creation in my Terraform configuration. I'm trying to create an S3 bucket only if a certain variable, `create_bucket`, is set to `true`. However, it seems that the bucket is not being created even when I set the variable to `true`. Hereโ€™s the relevant part of my Terraform code: ```hcl variable "create_bucket" { type = bool default = false } resource "aws_s3_bucket" "my_bucket" { count = var.create_bucket ? 1 : 0 bucket = "my-unique-bucket-name" } ``` When I run `terraform apply` with the command: ```bash terraform apply -var 'create_bucket=true' ``` I see the following output: ``` No changes. Your infrastructure matches the configuration. ``` It seems like the condition for the `count` argument is not being evaluated as expected. I also tried using a local variable to store the value of `count`, but it didn't change the outcome. Hereโ€™s what that looked like: ```hcl locals { bucket_count = var.create_bucket ? 1 : 0 } resource "aws_s3_bucket" "my_bucket" { count = local.bucket_count bucket = "my-unique-bucket-name" } ``` I still encounter the same result where no resources are created. Iโ€™ve verified that my variable is correctly passed through the command line and also tried hardcoding the value to `true` as well. What am I missing that prevents this bucket from being created when the condition is met? Any insights would be greatly appreciated! My development environment is Windows. What am I doing wrong? I'm developing on Ubuntu 20.04 with Hcl.