pvpkiran
pvpkiran

Reputation: 27048

How to create AWS SSM Parameter from Terraform

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?

Upvotes: 1

Views: 9771

Answers (3)

paulg
paulg

Reputation: 728

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}"

Upvotes: 3

Bob B
Bob B

Reputation: 41

I just faced this issue the $ in the CW config is causing the problem. Use $$

"Note: If you specify the template as a literal string instead of loading a file, the inline template must use double dollar signs (like $${hello}) to prevent Terraform from interpolating values from the configuration into the string. "

https://www.terraform.io/docs/configuration-0-11/interpolation.html

    "metrics": {
    "append_dimensions": {
        "AutoScalingGroupName": "$${aws:AutoScalingGroupName}",
        "ImageId": "$${aws:ImageId}",
        "InstanceId": "$${aws:InstanceId}",
        "InstanceType": "$${aws:InstanceType}"
    },

I prefer Pauls aproach though.

Upvotes: 1

Igor Eulálio
Igor Eulálio

Reputation: 109

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\"}\"",

Upvotes: -1

Related Questions