Skip to content

Instantly share code, notes, and snippets.

@ipmb
Last active June 2, 2022 17:30
Show Gist options
  • Save ipmb/d6d0bbd995434df2f8ed822749a8135b to your computer and use it in GitHub Desktop.
Save ipmb/d6d0bbd995434df2f8ed822749a8135b to your computer and use it in GitHub Desktop.
docker exec on AWS ECS with SSM
#!/bin/bash
# USAGE: CLUSTER=mycluster SERVICE=myservice ./docker-exec-ecs.sh
set -euf -o pipefail
TASK_ARN=$(aws ecs list-tasks --cluster=$CLUSTER --service=$SERVICE \
| jq -r .taskArns[0])
if [ "$TASK_ARN" = "null" ]; then
echo "Could not find any running tasks for $SERVICE on cluster:$CLUSTER."
exit 1
fi
CONTAINER_INSTANCE=$(aws ecs describe-tasks --cluster=$CLUSTER --task=$TASK_ARN \
| jq -r .tasks[0].containerInstanceArn)
INSTANCE_ID=$(aws ecs describe-container-instances --cluster=$CLUSTER --container-instances=$CONTAINER_INSTANCE \
| jq -r .containerInstances[0].ec2InstanceId)
echo "Connecting the remote Docker host..."
echo "Run the following to open a shell in the container:"
bold=$(tput bold)
normal=$(tput sgr0)
echo $bold' sudo docker exec -it $(curl -s http://localhost:51678/v1/tasks?taskarn='$TASK_ARN' | jq -r .Containers[0].DockerId) bash'$normal
aws ssm start-session --target=$INSTANCE_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment