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

前端 未结 5 494
一个人的身影
一个人的身影 2020-12-29 19:43

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 b

相关标签:
5条回答
  • 2020-12-29 20:08

    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
    0 讨论(0)
  • 2020-12-29 20:09

    Expanding on Juho Rutila's answer as it's too much to fit in a comment.

    This is possible using dynamic blocks from v0.12, but I found the properties had to be included in a nested content block. The for_each statement is also a bit tricky, so I found it sensible to extract that into a local to make the important stuff more readable:

    locals {
      isProd = var.environment_name == "production" ? [1] : []
    
      // Not necessary, but just illustrating that the reverse is possible
      isNotProd = var.environment_name == "production" ? [] : [1]
    }
    
    dynamic "access_logs" {
      for_each = local.isProd
      content {
        bucket  = "my-bucket"
        prefix  = "${var.environment_name}-alb"
      }
    }
    

    You can read more about dynamic blocks here: https://www.terraform.io/docs/configuration/expressions.html#dynamic-blocks

    0 讨论(0)
  • 2020-12-29 20:12

    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.

    0 讨论(0)
  • 2020-12-29 20:15

    Expanding on Juho Rutila’s answer too; I like to use the range function for this use case:

    dynamic "access_logs" {
      for_each = range(var.environment_name == "production" ? 1 : 0)
    
      contents {
        bucket  = "my-bucket"
        prefix  = "${var.environment_name}-alb"
      }
    }
    

    range(n) produces a n-element list.

    0 讨论(0)
  • 2020-12-29 20:21

    Conditionals in terraform are currently only to be used to determine a value, not to be used as an if statement wrapping a block.

    And you can also use conditionals to determine a value based on some logic.

    https://www.terraform.io/docs/configuration/interpolation.html#conditionals

    0 讨论(0)
提交回复
热议问题