terraformterraform-provider-awshcl

Terraform conditional nested attribute


What's the correct syntax to condition a nested attribute in terraform ? For example serverlessv2_scaling_configuration in the config below :

resource "aws_rds_cluster" "example" {
  # ... other configuration ...

  serverlessv2_scaling_configuration {
    max_capacity = 128.0
    min_capacity = 0.5
  }
}

Goal is to be able to create serverless or non-serverless RDS depending of an environment.


Solution

  • You have to use the Terraform dynamic block syntax for that, with a list of size 1 or 0. Assuming var.serverless is a boolean input variable:

    resource "aws_rds_cluster" "example" {
      # ... other configuration ...
    
      dynamic "serverlessv2_scaling_configuration" {
        for_each = var.serverless ? [1] : []
        content {
          max_capacity = 128.0
          min_capacity = 0.5
        }
      }
    }