Home > Enterprise >  AWS CLI on Windows won't work though using double quotes and escapes
AWS CLI on Windows won't work though using double quotes and escapes

Time:11-15

I made an Amazon Event Bridge rule and I wanted to attach targets on it using AWS CLI.

I knew that when you use json string in aws cli on windows 10 command prompt, you need to

  1. Use double quotes.(Single quotes are now allowed.)
  2. Put back slashes before double quotes except for the first and the last one.

so, I call this on command prompt

aws events put-targets --rule xxxxxxxxxxxxxxxxxxxxxxxxx --targets "{\"Arn\":\"arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StartEC2Instance\",\"Input\":\"{\"InstanceId\":[\"i-xxxxxxxxxxxxxxxxx\"]}\",\"Id\":\"aaaaaaaa\",\"RoleArn\":\"arn:aws:iam::awsaccountid:role/some-ssm-role\"}"

but got error:

Error parsing parameter '--targets': Invalid JSON: Expecting ',' delimiter: line 1 column 104 (char 103)
JSON received: {"Arn":"arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StartEC2Instance","Input":"{"InstanceId":["i-xxxxxxxxxxxxxxxxx"]}","Id":"aaaaaaaa","RoleArn":"arn:aws:iam::awsaccountid:role/some-ssm-role"}

any suggestion?

p.s.
I made sure command below succeeded on Linux terminal.

aws events put-targets --rule xxxxxxxxxxxxxxxxxxxxxxxxx --targets '{"Arn": "arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StopEC2Instance","Input":"{\"InstanceId\":[\"i-xxxxxxxxxxxxxxxxx\"]}","Id":"aaaaaaaa","RoleArn":"arn:aws:iam::awsaccountid:role/some-ssm-role"}'

CodePudding user response:

Could you try the following:

aws events put-targets --rule xxxxxxxxxxxxxxxxxxxxxxxxx --targets "{\"Arn\":\"arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StartEC2Instance\",\"Input\":\"{\\\"InstanceId\\\":[\\\"i-xxxxxxxxxxxxxxxxx\\\"]}\",\"Id\":\"aaaaaaaa\",\"RoleArn\":\"arn:aws:iam::awsaccountid:role/some-ssm-role\"}"

Or, same thing but marginally more readable this time with caret continuation markers:

aws events put-targets ^
    --rule xxxxxxxxxxxxxxxxxxxxxxxxx ^
    --targets "{\"Arn\":\"arn:aws:ssm:ap-northeast-1:awsaccountid:automation-definition/AWS-StartEC2Instance\",\"Input\":\"{\\\"InstanceId\\\":[\\\"i-xxxxxxxxxxxxxxxxx\\\"]}\",\"Id\":\"aaaaaaaa\",\"RoleArn\":\"arn:aws:iam::awsaccountid:role/some-ssm-role\"}"

The key thing to recognize is that the Input attribute in the JSON is itself a string containing JSON, so it needs to be double-escaped. This at least passes the initial format validation for me.

  • Related