Load many values from SSM Parameter Store

Hello, can someone help me with Data Source: aws_ssm_parameter ?
I have a few ssm_params stored in AWS, and i need to read them and then assign as values. Currently i’m using a code bellow

 data "aws_ssm_parameter" "slack_url" {
  name = "/path/to/parmater"
}

But soon my number of the parameters will be increased, is there any way to iterate through them, without duplicating of code ?

If yes, how i can access them later ? It needed for me to set some parameters in helm release.

Now I’m setting them like bellow

set {
    name  = "Desktopfiles.files\\.yml.global.values"
    value = data.aws_ssm_parameter.slack_url.value
  }

Hi @menvol3,

This problem of fetching an arbitrary number of parameter values from SSM Parameter Store and gathering them together into a map could be a good candidate for a small, general Terraform module focused only on that problem.

Here’s an example of what that module might look like. I’ve not actually tested this so it might contain some typos or similar but hopefully it’s good enough to demonstrate the general structure and give you somewhere to start from:

variable "parameters" {
  type = set(string)
}

data "aws_ssm_parameter" "main" {
  for_each = var.parameters

  name = each.value
}

output "values" {
  value = {
    for n, p in data.aws_ssm_parameter.main : n => p.value
  }
}

This module uses resource for_each to declare one instance of aws_ssm_parameter.main for each element of var.parameters. It then uses a for expression to simplify the resulting map of whole parameter objects into a map just of the parameter values, where the map keys are the same names given in var.parameters.

In the calling module you might use this module as follows:

module "parameters" {
  # (use a suitable source address for where you've
  # placed this shared module.)
  source = "../modules/ssm-parameters"

  # List all of the parameters you want to load
  parameters = [
    "Desktopfiles.files\\.yml.global.values",
  ]
}

# I'm just using an output value as an example
# here, but you can write references to the
# parameter values anywhere that Terraform
# allows arbitrary expressions:
#    module.parameters.values["Desktopfiles.files\\.yml.global.values"]
output "example" {
  value = module.parameters.values["Desktopfiles.files\\.yml.global.values"]
}

Hi @apparentlymart ,

Thank You for the reply

Is it possible to do it without modules ?

For example we moved ssm parameter to variables in structure like bellow

variable "ssm-params" {
    default = [
      {
        name       = "parameter-one"
        ParamValue = "/path/to/parameter-one"
      },
      {
        name       = "parameter-two"
        ParamValue = "/path/to/parameter-two"
      },

Then we iterate over each of them

data "aws_ssm_parameter" "ssm_params" {
    for_each = {
      for v in var.ssm-params : v.ParamValue => v.name
    }
    name = each.key
  }

And now my question is, how i can refer to their values ?