Home > Blockchain >  Get only the task id or attribute from aws-cli's response json
Get only the task id or attribute from aws-cli's response json

Time:03-03

I can get the task id by this command.

$aws ecs list-tasks --cluster mycluster

{
    "taskArns": [
        "arn:aws:ecs:ap-northeast-1:142324134231:task/mycluster/44645ae88f294ed9af3f4d809b3f1721",
        "arn:aws:ecs:ap-northeast-1:142324134231:task/mycluster/73af9f9645d2412bba1344bf0e362fd5"
    ]
}

then I copy and paste task id and exec command.

aws ecs execute-command \
    --cluster mycluster \
    --task 44645ae88f294ed9af3f4d809b3f1721 \
    --container DjangoContainer \
    --interactive \
    --command "/bin/bash"

Is there any way to do this automatically?

It's the problem of shell script or do they have any function like this in aws-cli?

(for example (it's fake))

$aws ecs list-tasks --cluster mycluster --show-taskonly
44645ae88f294ed9af3f4d809b3f1721

So,my current idea is using php Regular expressions to get the id from the response, and compose new command.

However it dose'nt sound cool.

Is there any good practice?

CodePudding user response:

To get the IDs only, you can use --query:

aws ecs list-tasks --cluster JAJP1LFGTN075  --query "taskArns" --output text
  • Related