'AWS SSM - store multiple parameters using terraform and json file

We have a couple of legacy applications we're migrating to ec2 and these use a bunch of application configuration parameters. I need to be able to store each config as an individual parameter per application.

I'm trying the following but clearly not doing it right as it appends all values to a single parameter per application:

locals {
  application = {
    "application1" = { app_shortcode = "app1"},
    "application2" = { app_shortcode = "app2"}
}

resource "aws_ssm_parameter" "application_parameters" {
  for_each = local.application
  name     = each.key
  value    = jsonencode(file("${path.module}/${each.key}/ssm_param.json"))
}

my app1's ssm_param.json is something like

{
    "app1_config1": "config_value_1",
    "app1_config2": "config_value_2",
    "app1_config3": "config_value_3"
}

and app2's ssm_param.jsonis

{
    "app2_config_a": "config_value_a",
    "app2_config_b": "config_value_b",
    "app2_config_c": "config_value_c"
}

The current output is a single parameter like this for each application: "{\r\n \"app2_config_a\": \"config_value_a\",\r\n \"app2_config_b\": \"config_value_b\"\r\n, \r\n \"app2_config_c\": \"config_value_c\"\r\n}"

Looking for suggestions please.



Solution 1:[1]

I solved this by using a slightly different approach (not quite the same as my initial one but this works for me for now):

used a ssm_params.yaml as below (the project team was kind enough to give me the config settings as yaml output)

parameter:
    app1:
      name: app1_config1
      description: "application config test"
      type: "String"
      value: "some_randoM_value"
    app1:
      name: app_config2
      description: "another test"
      type: "SecureString"
      value: "some_random123_value###"
    app2:
      name: app_config_2
      description: "config test"
      type: "String"
      value: "some_randoM_value_2"
locals {
params             = yamldecode(file("${path.module}/ssm_params.yaml"))
}

resource "aws_ssm_parameter" "app_params" {
  for_each = local.params.parameter
  name     = each.value.name
  type     = each.value.type
  value    = each.value.value
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CMR H