0
votes

I want to build an s3 bucket only when we are using our prod account. We are using terraform 11.7 which I don't think supports the 'null' choice for if statement

Does anyone know if something like this can be done in terraform 11:

resource "aws_s3_bucket" "feedback_service_bucket" {
  bucket = "${var.account_name == "prod" ? "ces-${var.environment}-QA-compendex" : null}" 
  acl = "private"

}

I will upgrade soon to terraform 13, but as this is required in this sprint I don't have time. We have a prod.tfvars file which is used when the applied environment is set to prod (obviously), so if the above is not possible, is there a way to put an entire resource into a tfvars file. I know you can use variables, but I'm not sure about resource. Any help would be great, thanks

1

1 Answers

2
votes

Does anyone know if something like this can be done in terraform 11:

You can use count for such scenario:

resource "aws_s3_bucket" "feedback_service_bucket" {
  count = "${var.account_name == "prod" ? 1 : 0}"
  bucket = "ces-${var.environment}-QA-compendex" 
  acl = "private"
}

You can refer to this: https://www.terraform.io/docs/configuration-0-11/interpolation.html#conditionals

I will upgrade soon to terraform 13, but as this is required in this sprint I don't have time.

For info, Terraform 0.13 won't give you much advantages for this specific use case. You would still use count. What changes in 0.13 regarding conditional resources is that you have an alternative using for_each - introduced with v0.12 - and in which you can achieve the same behavior.