简体   繁体   中英

Terraform: Use default variable if parameter store value doesn't exist

Overview

  • using Terraform cloud.
  • seed variable "environment" specified in Terraform cloud workspace eg dev/test/prod
  • The "environment" variable is used to look up values in AWS parameter store eg
data "aws_ssm_parameter" "rds_password" {
  name = "/${var.environment}/rds/pg/rds_password"
}

module "db" {
  password               = data.aws_ssm_parameter.rds_password.value
}

Question

What's the best way to go about setting default values? It seems as though using locals to check for the existence of the parameter otherwise use the default var.

Thanks in advance for any pointers.

You could probably use the try built-in function [1]. For example:

module "db" {
  password = try(data.aws_ssm_parameter.rds_password.value, local.rds_password)
}

As you have mentioned, you would of course have to provide a local or a normal variable. If you decide for the latter, you would have to either provide the value when running plan/apply or a default one.


[1] https://developer.hashicorp.com/terraform/language/functions/try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM