14

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:
Attie
  • 20,734

4 Answers4

13

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

166_MMX
  • 1,471
8

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.

Attie
  • 20,734
1

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.

1

In my case, it is the file encoding problem. Save your file in UTF-8 without BOM, then it will work.

If you use Sublime Text, then go to File -> Save with Encoding -> UTF-8.

In VS Code, at the bottom bar of VS Code, you'll see the label "UTF-8 with BOM". Click it to open the action bar and select "Save with encoding". Choose UTF-8 and save the file.

hatted
  • 251