Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to turn the access_logs block on and off via the environment_name variable?

I'm looking at using the new conditionals in Terraform v0.11 to basically turn a config block on or off depending on the evnironment.

Here's the block that I'd like to make into a conditional, if, for example I have a variable to turn on for production.

access_logs {     bucket = "my-bucket"     prefix = "${var.environment_name}-alb" } 

I think I have the logic for checking the environment conditional, but I don't know how to stick the above configuration into the logic.

"${var.environment_name == "production" ? 1 : 0 }" 

Is it possible to turn the access_logs block on and off via the environment_name variable? If this is not possible, is there a workaround?

like image 348
jmreicha Avatar asked Feb 25 '17 21:02

jmreicha


2 Answers

One way to achieve this with TF 0.12 onwards is to use dynamic blocks:

dynamic "access_logs" {   for_each = var.environment_name == "production" ? [var.environment_name] : []   content {     bucket  = "my-bucket"     prefix  = "${var.environment_name}-alb"   } } 

This will create one or zero access_logs blocks depending on the value of var.environment_name.

like image 170
Juho Rutila Avatar answered Oct 06 '22 09:10

Juho Rutila


In the current terraform, the if statement is only a value and can not be used for the block.

There is a workaround in this case. You can set the enabled attribute of the access_log block to false. Note that this is not a general solution but can only be used with the access_log block.

access_logs {     bucket  = "my-bucket"     prefix  = "${var.environment_name}-alb"     enabled = "${var.environment_name == "production" ? true : false }" } 

See also:

  • https://www.terraform.io/docs/providers/aws/r/elb.html#access_logs
  • https://www.terraform.io/docs/providers/aws/r/alb.html#access_logs
  • https://github.com/hashicorp/terraform/pull/11120
like image 41
minamijoyo Avatar answered Oct 06 '22 08:10

minamijoyo