Home > Mobile >  How to create AWS SSM Parameter from Terraform
How to create AWS SSM Parameter from Terraform

Time:10-13

I am trying to copy AWS SSM parameter(for cloudwatch) from one region to another. I have the json which is created as a String in one region.

I am trying to write a terraform script to create this ssm parameter in another region.
According to the terraform documentation, I need to do this

resource "aws_ssm_parameter" "foo" {
  name  = "foo"
  type  = "String"
  value = "bar"
}

In my case value is a json. Is there a way to store the json in a file and pass this file as value to the above resource? I tried using jsonencode,

resource "aws_ssm_parameter" "my-cloudwatch" {
  name  = "my-cloudwatch"
  type  = "String"
  value = jsonencode({my-json})

that did not work either. I am getting this error Extra characters after interpolation expression I believe this is because the json has characters like quotes and colon.

Any idea?

CodePudding user response:

I tested the following & this worked for me:

resource "aws_ssm_parameter" "my-cloudwatch" {
  name  = "my-cloudwatch"
  type  = "String"
  #value = file("${path.module}/ssm-param.json")
  value = jsonencode(file("${path.module}/files/ssm-param.json"))

./files/ssm-param.json content:

{
    "Value": "Something"
}

and the parameter store value looks like this:

"{\n    \"Value\": \"Something\"\n}"

CodePudding user response:

You need to insert your json with escaped quotas, is a little trick in AWS, and you need to parse this when retrieve:

const value = JSON.parse(Value)

Example of insert:

 "Value": "\"{\"flag\":\"market_store\",\"app\":\"ios\",\"enabled\":\"false\"}\"",
  • Related