Desired outcome
I want to conditionally define a setting
block for an aws_elastic_beanstalk_environment
Terraform resource based on another variable, environment
.
Current attempt
My normal go-to for this situation is to use count
:
resource "aws_elastic_beanstalk_environment" "backend_prod" {
name = "backend-${var.env}"
application = aws_elastic_beanstalk_application.backend.name
solution_stack_name = "64bit Amazon Linux 2 v3.4.9 running Docker"
wait_for_ready_timeout = "10m"
# other settings omitted
setting {
count = var.environment == "prod" ? 1 : 0
namespace = "aws:elasticbeanstalk:application:environment"
name = "API_KEY"
value = var.api_key
}
}
Current outcome
Unfortunately, whenever I run terraform plan
this results in:
An argument named "count" is not expected here.
CodePudding user response:
You can do this using dynamic block:
resource "aws_elastic_beanstalk_environment" "backend_prod" {
name = "backend-${var.env}"
application = aws_elastic_beanstalk_application.backend.name
solution_stack_name = "64bit Amazon Linux 2 v3.4.9 running Docker"
wait_for_ready_timeout = "10m"
# other settings omitted
dynamic "setting" {
for_each = var.environment == "prod" ? [1] : []
content {
namespace = "aws:elasticbeanstalk:application:environment"
name = "API_KEY"
value = var.api_key
}
}
}