Search code examples
terraformterraform-provider-awsaws-parameter-store

Terraform: How to store a map in a single ssm-parameter and get back a value pair?


let's assume i have a map like this:

variable "test_parameters" {
type = map
default = {
"A" = "subnet-73e35d3e",
"B" = "subnet-7e00d503",
"C" = "subnet-d9d446b2",
}

}

What is the terraform-code

  1. to store the values of the map in a single aws_ssm_parameter ?
  2. get a single value from the parameter like: B = subnet-7e00d503 or B:subnet-7e00d503 ?

Many thanks for help ;)


Solution

  • You can store it as json, and then get json back.

    resource "aws_ssm_parameter" "foo" {
      name  = "myparam"
      type  = "String"
      value = jsonencode(var.test_parameters)
    }
    

    To read it:

    data "aws_ssm_parameter" "foo" {
      name = "myparam"
    }
    
    # to use 
    
    locals {
      myparam_values = jsondecode(data.aws_ssm_parameter.foo.value)
    }