In AWS ECS, the hostname of the docker container is the container runtime id. Is there any way that given a container rumtime id, the task ARN under which the container is running can be fetched?
There isn't any specific AWS command which can fetch the task ARN from the container runtime id. But this can be achieved using list-tasks
and describe-tasks
command of aws ecs, if you know the cluster and service name in prior.
Here is the idea of how you can achieve this:
tasksList=`aws ecs list-tasks --cluster mycluster --service-name myservice | awk '/mycluster/ {print}' | sed 's/,$//' | awk '{ sub(/^[ \t]+/, ""); print }' | awk '{printf "%s ",$0} END {print ""}'`
taskDesc="aws ecs describe-tasks --cluster mycluster --tasks ${tasksList}"
eval $taskDesc | awk '/taskArn|runtimeId/ {print $0}' | awk '{ sub(/^[ \t]+/, ""); print }' | awk '!visited[$0]++' | awk '/taskArn/ {$0=$0"->"} 1'
This will give you an output something like this:
"taskArn": "arn:aws:ecs:<aws_region>:<account_id>:task/mycluster/043de9ab06bb41d29e97576f1f1d1d33",->
"runtimeId": "191c90bae67844124ff2d079f4de997dc8cb9e3c93cd451d931c806283ea527d",
"runtimeId": "61e6fa6e1cbb5039e1c4df31d5c522b9439c22fbeff3c9cc1fb4429ffbb5a94d",
"taskArn": "arn:aws:ecs:<aws_region>:<account_id>:task/mycluster/14478901e8364466b8fd8236d6a66c5e",->
"runtimeId": "b679c5139f019f526c4301c8cc511723abd6aed3fa8cf9c397147d33275a860c",
"runtimeId": "0e727f660616df970b2cf767ea389bee97cd748ea9e09266cb5ee651ad2d2971",
"runtimeId": "ee7fb30715d9ff325eebcfacdde978179f07b1e3bf91a0dac92abd3b54970307",
"taskArn": "arn:aws:ecs:<aws_region>:<account_id>:task/mycluster/23d037af17f54a59afe50f810afdecf0",->
"runtimeId": "67d4ebea82e6e6622171816e1aef53489070aea5b03d5942e8e41c2dc4f49fd3",
Below every taskArn
line, there are multiple runtimeId
lines specifying the container runtime ids of the containers running under that task.
Hope this helps. Happy scripting.