Aws Cli, Using `--Cli-Input-Json` in a Pipeline

I'd like to use the aws command line application in a pipeline, but it doesn't appear to be possible.

A working example is:

$ aws ecs register-task-definition --cli-input-json file://./mytask.json

However the following do not work:

$ cat ./mytask.json \
    | aws ecs register-task-definition --cli-input-json file:///dev/stdin

Error parsing parameter 'cli-input-json': Invalid JSON: Expecting value: line 1 column 1 (char 0)
JSON received:
$ aws ecs register-task-definition --cli-input-json file://<(cat ./mytask.json)

Error parsing parameter 'cli-input-json': Invalid JSON: Expecting value: line 1 column 1 (char 0)
JSON received:

3 Answers

Found a workaround for the time being with xargs that is quite clean:

cat ./mytask.json \
    | xargs -0 aws ecs register-task-definition --cli-input-json

It only adds xargs -0 and requires --cli-input-json to be the last argument

1

I went digging... It looks like aws will read the indicated file twice, using the second dataset for it's operation. Of course, in a pipeline, the second read() will get nothing.

I've added a pipe:// prefix/schema (commit) for use in this situation which will cache the value... I've also made a pull request.

I was able to pass the JSON as a variable on --cli-input-json and inject bash variables too.

So your example should be:

aws ecs register-task-definition --cli-input-json "$(cat < ./mytask.json)"

In my scenario at hand I have:

  1. Either pass a JSON as you would like

    aws autoscaling start-instance-refresh --cli-input-json "$(cat < ./options.json )"

  2. Or as HEREDOC in order to pass other variables too. (Not requested but may be useful)

asg_nominal_name=AutoScalingGroupName

aws autoscaling start-instance-refresh --cli-input-json "$(cat <<JSON
{ 
    "AutoScalingGroupName": "${asg_nominal_name}",
    "Preferences": {
        "InstanceWarmup": 300,
        "MinHealthyPercentage": 100
    }
}
JSON
)"

I am not so familiar to explain the why in bash terms. Feel free.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest