Terraform Module Not Creating Resources Due to Invalid Interpolation - How to Debug?
I'm stuck on something that should probably be simple. I've searched everywhere and can't find a clear answer. I'm currently working with Terraform 1.4.3 and I have a module that is supposed to create an AWS S3 bucket along with some IAM roles. However, when I try to run `terraform apply`, I encounter the following behavior: ``` behavior: Invalid function argument on .terraform/modules/my_module/main.tf line 15, in resource "aws_s3_bucket" "my_bucket": 15: bucket = "bucket-${var.environment}-${count.index}" Invalid value for "bucket" parameter: must not contain uppercase characters or underscores. ``` The relevant part of my module code looks like this: ```hcl variable "environment" { type = string } resource "aws_s3_bucket" "my_bucket" { count = var.create_bucket ? 1 : 0 bucket = "bucket-${var.environment}-${count.index}" acl = "private" } ``` I'm expecting this to create a bucket with the name format `bucket-dev-0` if `var.environment` is set to `dev`. However, I think the scenario is that `var.environment` might contain uppercase letters or underscores, which would trigger that behavior regarding the S3 bucket name requirements. I tried to debug it by examining the variable's value right before the bucket creation, but I need to seem to find a way to output it. To troubleshoot, I added an output for `var.environment`: ```hcl output "env" { value = var.environment } ``` However, running `terraform output` only shows the last valid state and not the value during the plan/apply process. I've also checked that the variable is being passed correctly when I call the module: ```hcl module "my_module" { source = "./my_module" environment = var.environment create_bucket = true } ``` What steps can I take to ensure that `var.environment` adheres to the naming restrictions for S3 buckets? Are there any best practices for transforming or validating input variables in Terraform? I would appreciate any suggestions or insights on how to resolve this scenario effectively. How would you solve this? This is part of a larger CLI tool I'm building. Is there a better approach?