Gowthamakanthan Gopal
Gowthamakanthan Gopal

Reputation: 111

Terraform AWS SSM Parameter with multiple values

I need to store multiple values related to the same environment in the SSM parameter store. What would be the best practice? Storing the values with the individual parameter? or storing everything in a single parameter?

When am storing it individually, it's pretty straight forward to get the value using the following terraform code.

SSM parameter foo

bar
data "aws_ssm_parameter" "ssm_parameters" {
  for_each = var.ssm_parameters
  name     = each.key
}

output "ssm_parameters" {
  value = tolist([
    for ssm_parameter in data.aws_ssm_parameter.ssm_parameters : ssm_parameter.value
  ])
}

Output:

➜  aws_ssm git:(add/ssm_resources) ✗ terraform apply -var-file="test.tfvars"
data.aws_ssm_parameter.ssm_parameters["foo"]: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

ssm_parameters = [
  "bar",
]

When am storing all values in a single parameter. Is there a way to get the values based on the particular key? For example, if I store the values something like below, is there a way to get only server2 value? or server1 eth1 value?

SSM parameter foo:

[
  {
    server1_eth0 = “server_ip1"
    server1_eth1 = “server_ip2"
    server2 = “server2_ip"
    server3 = “server3_ip"
  }
]

Upvotes: 1

Views: 5392

Answers (1)

Gowthamakanthan Gopal
Gowthamakanthan Gopal

Reputation: 111

The following method helped me to solve this. Thanks, @Marcin, @Matt Schuchard

Paramter value:

"server_ips":
{ 
 "server1":"10.1.21.1",
 "server2":"10.1.22.1", 
 "server3":"10.1.23.1"
}
}

And the code is

output "ssm_parameters" {
  value = jsondecode(data.aws_ssm_parameter.ssm_parameters.value)["server_ips"].server3
}

Output is

Outputs:

ssm_parameters = 10.1.23.1

Upvotes: 1

Related Questions