Terraform: Issue while reading value from aws ssm parameter store
I am looking to retrieve value from aws ssm paramter store and assigning to a varibale of type map
AWS SSM parameter store:
name: lambda_env
value: {"var1"="value1","var2"="value2","var3"="value3"}
variable.tf:
variable "lambda_env_vars" {
type = map(string)
default = null
}
main.tf:
data "aws_ssm_parameter" "lambda_env" {
name = "lambda_env"
}
module "lambda" {
source = "../modules/lambda/"
lambda_env_vars = tomap(data.aws_ssm_parameter.lambda_env.value)
}
terraform plan is giving below error:
**"Invalid value for "v" parameter: cannot convert string to map of any single type"**
It is working fine if I give direct value as
lambda_env_vars = tomap({"var1"="value1","var2"="value2","var3"="value3"})
The output of data.aws_ssm_parameter.lambda_env.value is as shown below
outputs:
aws_ssm_var = "{\"var1\"=\"value1\",\"var2\"=\"value2\",\"var3\"=\"value3\"}"
The data.aws_ssm_parameter.lambda_env.value
is a JSON formatted string. You need to convert it to a map with the jsondecode
function. tomap
converts HCL2 types to a map
.
lambda_env_vars = jsondecode(data.aws_ssm_parameter.lambda_env.value)