I got the "Error creating SSM document: InvalidDocumentContent: JSON not well-formed"

Hi,

I got the following terraform JSON not well-formed error from the following aws_ssm_document resource file. I spent about one hour and have not found the issue. Anyone here would help me with this?

resource "aws_ssm_document" "linux_tivoli" {
  name          = "Install_Tivoli_Linux"
  document_type = "Command"
  content = <<DOC
  {
      "schemaVersion":"2.2"
      "description":"SSM Document to install ITM agent in linux OS"
      "mainSteps":[
        {
            "action":"aws:runShellScript",
            "name":"runShellScript",
            "inputs":{
               "runCommand":[
                  "sudo su -",
                  "source ~/.bash_profile && yum install wget -y",
                  "cd /tmp",
                  "token=$(aws secretsmanager get-secret-value  --region us-east-1 --secret-id tokenInsightVM --query SecretString --output text)\n",
                  "wget https://xxxx.blob.core.windows.net/installers/RHEL_Agents_Installer.tar.gz",
                  "tar -xvf RHEL_Agents_Installer.tar.gz",
                  "sleep 30",
                  "echo 'Installing ITM'",
                  "cd RHEL_Agents_Installer/ITM/lz_063007000_LINUX",
                  "./itmInstall.ksh -a mva -p  -s  - For non-prod servers"
                ]
            }
         }
      ]
    }
DOC
}

Hi @condescendent,

The first two properties in your object do not have commas after them to introduce the next property.

To avoid problems with invalid JSON syntax, I would suggest changing this to be a normal Terraform value and then use the jsonencode function to produce the JSON syntax, since Terraform will guarantee to always produce valid JSON syntax, and the Terraform language syntax is not so fussy about commas:

  content = jsonencode({
    schemaVersion = "2.2"
    description   = "SSM Document to install ITM agent in linux OS"

    mainSteps = [
      {
        action = "aws:runShellScript"
        name   = "runShellScript"
        inputs = {
          runCommand = [
            "sudo su -",
            "source ~/.bash_profile && yum install wget -y",
            "cd /tmp",
            "token=$(aws secretsmanager get-secret-value  --region us-east-1 --secret-id tokenInsightVM --query SecretString --output text)\n",
            "wget https://xxxx.blob.core.windows.net/installers/RHEL_Agents_Installer.tar.gz",
            "tar -xvf RHEL_Agents_Installer.tar.gz",
            "sleep 30",
            "echo 'Installing ITM'",
            "cd RHEL_Agents_Installer/ITM/lz_063007000_LINUX",
            "./itmInstall.ksh -a mva -p  -s  - For non-prod servers",
          ]
        }
      },
    ]
  })

Hi, Apparentlymart, Happy new year!

Thank you so much for the reply. i really didn’t expect someone could reply to my question. But your solution works and it is the commas. I will start to use jsonencode function to write JSON document in terraform code.

jason

Hello there,

I am also having the issues with my script as I am getting

Error: creating SSM Document (tag-remediation): InvalidDocumentContent: JSON not well-formed. at Line: 1, Column: 15. I am trying to figure out where the issue is. I used the jsonlint and it validated my Json script

Below is my script:

content = jsonencode(
{
“schemaVersion”: “0.3”,
“description”: “Auto-remediation-for-required-tags”,

"assumeRole": {
	"action": "sts:AssumeRole",
	"roleArn": "aws_iam_role.tag_remediation.arn"
},

"mainSteps": [{
	"name": "remediation",
	"action": "aws:executeAwsApi",
	"inputs": {
		"Service": "ec2",
		"Api": "createTags",
		"Resources": [
			"{{ResourceId}}"
		],
		"Tags": [{
			"Key": "name",
			"Value": "dev "
		}]
	}
}]

})