simple way to see container image(s) running in a AWS ECS service in command line

Viewed 1694

Whats the easiest way to display the currently running container images in a ECS service from a command line? Currently I'm using an ugly mix of aws cli and jq:

aws ecs list-services --cluster CLUSTER_NAME | jq -r '.serviceArns | .[] | select(. | contains("SERVICE_NAME"))|split("/")|last' |xargs -I {} aws ecs describe-services --cluster CLUSTER_NAME --services {}|jq -r '.services[0].taskDefinition|split("/")|last'| xargs -I {} aws ecs describe-task-definition --task-definition {}|jq '.taskDefinition.containerDefinitions[0].image'

Surely there must be an easier way?

2 Answers

You can do a little better if you describe task definitions directly instead of going through services first. You can get rid of jq if you use --query. And you can get rid of xargs if you use a nested command.

aws ecs describe-tasks --output text --query tasks[].containers[].[image] --tasks `aws ecs list-tasks --desired-status RUNNING --query taskArns --output text`

list-tasks supports --cluster if you want to limit it to just one of the clusters.

ecs-cli is a command line tool specifically made for ECS, but it doesn't print out the image, only the task definition name.

I have just tried to simplify your given query & it works. Please give it try:

aws ecs describe-services \
--services SERVICE_NAME \
--cluster CLUSTER_NAME |  jq ".services[0].deployments[0].taskDefinition" | xargs -I {} aws ecs describe-task-definition --task-definition {} | jq '.taskDefinition.containerDefinitions[0].image'
Related