Skip to content

Instantly share code, notes, and snippets.

@sjednac
Last active July 30, 2024 08:17
Show Gist options
  • Save sjednac/c877e8e8dc09d6e2279209dfc7d9d4f8 to your computer and use it in GitHub Desktop.
Save sjednac/c877e8e8dc09d6e2279209dfc7d9d4f8 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Access Docker containers running on ECS/Fargate using SSH.
#
# You'll need to install the Session Manager plugin for the `aws` command for this to
# work properly. On macOS: `brew install session-manager-plugin`.
#
# References:
# * https://gist.github.com/sjednac/c877e8e8dc09d6e2279209dfc7d9d4f8 (source code)
# * https://aws.amazon.com/blogs/containers/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2/
#
COMMAND=${@:-/bin/sh}
if [ -z "$ECS_FAMILY" ]; then
echo "Please select an ECS task family..."
ECS_FAMILIES=`aws ecs list-task-definition-families --status ACTIVE --output text --query families`
if [ -z "$ECS_FAMILIES" ]; then
echo "No ECS task families found."
exit 1
fi
select ITEM in $ECS_FAMILIES
do
if [ "$ITEM" != "" ]; then
ECS_FAMILY=$ITEM
break
fi
done
fi
echo "ECS family: $ECS_FAMILY"
if [ -z "$ECS_CLUSTER" ]; then
echo "Please select an ECS cluster..."
ECS_CLUSTER_ARNS=`aws ecs list-clusters --output text --query clusterArns`
if [ -z "$ECS_CLUSTER_ARNS" ]; then
echo "No ECS clusters found."
exit 1
fi
select ITEM in $ECS_CLUSTER_ARNS
do
if [ "$ITEM" != "" ]; then
ECS_CLUSTER=$ITEM
break
fi
done
fi
echo "ECS cluster: $ECS_CLUSTER"
if [ -z "$ECS_TASK" ]; then
echo "Please select an ECS task..."
ECS_TASK_ARNS=`aws ecs list-tasks --cluster $ECS_CLUSTER --family $ECS_FAMILY --output text --query taskArns`
if [ -z "$ECS_TASK_ARNS" ]; then
echo "No ECS tasks found."
exit 1
fi
select ITEM in $ECS_TASK_ARNS
do
if [ "$ITEM" != "" ]; then
ECS_TASK=$ITEM
break
fi
done
fi
echo "ECS task: $ECS_TASK"
if [ -z "$CONTAINER" ]; then
echo "Please select a Docker container.."
CONTAINERS=`aws ecs describe-tasks --tasks $ECS_TASK --cluster $ECS_CLUSTER --query "tasks[*].containers[*].name" --output text`
if [ -z "$CONTAINERS" ]; then
echo "No containers found."
exit 1
fi
select ITEM in $CONTAINERS
do
if [ "$ITEM" != "" ]; then
CONTAINER=$ITEM
break
fi
done
fi
echo "Docker container: $CONTAINER"
aws ecs execute-command \
--region $AWS_REGION \
--profile $AWS_PROFILE \
--cluster $ECS_CLUSTER \
--task $ECS_TASK \
--container $CONTAINER \
--command "$COMMAND" \
--interactive
#!/bin/bash
#
# Sample script for a specific project/use-case.
#
export AWS_PROFILE=myproject
export AWS_REGION=eu-central-1
export ECS_FAMILY=myapp
export ECS_CLUSTER=mycluster
# Ask for this one each time the script is run
#export ECS_TASK=mytask
export CONTAINER=nginx
COMMAND=${@:-/bin/sh}
./aws_ecs_exec.sh $COMMAND
MIT No Attribution
Copyright 2024 Szymon Jednac
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment