0
votes

I'm trying to create s3 buckets on aws using Terraform, few buckets exist in all environments (dev, qa and prod) whereas most buckets exist in only 1 or 2 environments. I'm using count to define resources selectively

So in the below code, I'm creating a bucket in S3 only if the environment is dev or qa using count and conditionals. But when I run the code for prod environment, I'm getting Error: module 's3-bucket-example': unknown variable referenced: 'lifecycle_rule_s3_bucket_example'; define it with a 'variable' block

lifecycle_rule for each bucket is defined in corresponding var file for each environment like lifecycle_rule_s3_bucket_example is defined in var-file.dev and var-file.qa but not in var-file.prod as the bucket exists in dev and qa. But since the bucket doesn't exist in prod, why am i getting the error to define lifecycle_rule variable ? My guess was that since the count = 0 for prod, none of the remaining variables should matter in the module.

module "s3-bucket-example" {
  source             = "./modules/s3"
  count              = "${var.aws-account == "dev"|| var.aws-account == "qa" ? 1 :0}"
  bucketname         = "${local.this_env_prefix}-s3-bucket-example"
  versioning-enabled = "true"
  lifecycle_rule     = "${var.lifecycle_rule_s3_bucket_example}"
  aws_account        = "${var.aws-account}"
}
1

1 Answers

0
votes

Prior to actually creating any resource in AWS (or any other provider for that matter), Terraform performs a validation to check wether all variables etc. are defined.

This way the chance of running into an error during resource creation is reduced.

Guess you have to define the variable for your prod env as well with a dummy value...