Skip to content

Instantly share code, notes, and snippets.

@hakanilter
Last active December 7, 2023 16:50
Show Gist options
  • Save hakanilter/e5961a3c25689ceef3f47ddd4ba91208 to your computer and use it in GitHub Desktop.
Save hakanilter/e5961a3c25689ceef3f47ddd4ba91208 to your computer and use it in GitHub Desktop.
AWS ECS run task and wait for the result
# Requies JSON as the output format and "jq" commandline tool
# If task runs successfuly, exits 0
run_result=$(aws ecs run-task \
--cluster ${CLUSTER} \
--task-definition ${TASK_DEFINITION} \
--launch-type EC2 \
--overrides "${OVERRIDES}")
echo ${run_result}
container_arn=$(echo $run_result | jq -r '.tasks[0].taskArn')
aws ecs wait tasks-stopped \
--cluster ${CLUSTER} \
--tasks "${container_arn}"
@mpozniak
Copy link

mpozniak commented Aug 31, 2022

You can use 'jq -r' to avoid using sed to remove ' " ' (escaping characters):

$(echo $run_result | jq '.tasks[0].taskArn' | sed -e 's/^"//' -e 's/"$//') --->>>
$(echo $run_result | jq -r '.tasks[0].taskArn')

jq --help:
"-r output raw strings, not JSON texts;"

@hakanilter
Copy link
Author

Thanks for the update @mpozniak !

@arkangle
Copy link

arkangle commented Dec 7, 2023

Here is an approach without needing jq

container_arn=$(aws ecs run-task \
    --cluster ${CLUSTER} \
    --task-definition ${TASK_DEFINITION} \
    --launch-type EC2 \
    --query tasks[0].taskArn \
    --output text \
    --overrides "${OVERRIDES}")
aws ecs wait tasks-stopped \
    --cluster ${CLUSTER} \
     --tasks "${container_arn}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment