contains placeholder within quotes error AWS Cloudwatch rule

Viewed 1090

I am trying to use an AWS Cloudwatch rule that runs a powershell script on a EC2 Instance using SSM. The script works when I hard code the instance ID in the command section of the RunPowerShellScript Document. But when I use the same command in the Input Transformer to get the Instanceid as a variable from the Cloudwatch event I get the following error:

"There was an error while saving rule DisableInAD. Details: InputTemplate for target Id111111 contains placeholder within quotes.."

The input Command I am trying to use is: {"commands":["C:\\users \\documents\\Disable_Single_Instance -Id <instance>"]}

2 Answers

I asked AWS about this and was told that this is a known limitation of Cloudwatch Events Input Transformer. They suggested creating a Lambda to use as the target as you are able to pass parameters that way. Then the lambda could potentially invoke the SSM command.

One workaround is to escape the quotes

e.g.:

InputPathsMap

{"pipeline":"$.detail.pipeline","state":"$.detail.state"}

InputTemplate

"{\"message\":\"<pipeline> - <state>\"}"

In cloud formation:

InputTransformer:
  InputPathsMap: 
    pipeline: "$.detail.pipeline"
    state: "$.detail.state"
  InputTemplate: '"{\"message\":\"<pipeline> - <state>\"}"'

You will get the result as a quoted string (not JSON) e.g.

"\"{\\\"message\\\":\\\"TestPipleline - SUCCEEDED\\\"}\""

so you will have double parse the result to get the proper JSON.

Related