aws-ecs-shell
Open an interactive shell in a running AWS ECS task, like a terminal on the container. A single Bash script, with a numbered menu for every choice you don’t pass as an option.
$ ./aws-ecs-shell.sh
1) alpha
2) beta
Cluster (number, Ctrl-D to cancel) > 2
Service: web
1) 7f3c0e1a9b… (started 2026-10-09T10:00:00Z)
2) c41d88b2e0… (started 2026-10-08T09:00:00Z)
Task (number, Ctrl-D to cancel) > 1
Container: app
To skip the menus next time: ./aws-ecs-shell.sh --cluster beta --service web --container app
Connecting to ECS task 7f3c0e1a9b… (cluster: beta, container: app)...
It uses ECS Exec: one persistent SSM session with a real TTY, so cd and export persist and vim, top, Ctrl-C, history and completion work. It starts bash, or sh if the image has no bash. No SSH, bastion or open port is needed.
Requirements
- Bash 3.2 or newer (macOS, Linux, WSL, Git Bash).
- The AWS CLI, already authenticated (
aws sso login, aws configure, …).
- The Session Manager plugin.
- A task started with ECS Exec enabled (
enableExecuteCommand, and a task role allowed to use the ssmmessages:* actions; see the ECS Exec setup).
- IAM permissions for the caller:
ecs:ListClusters, ecs:ListServices, ecs:ListTasks, ecs:DescribeTasks and ecs:ExecuteCommand.
Usage
aws-ecs-shell.sh [--cluster <name>] [--service <name>] [--task <id-or-arn>]
[--container <name>] [--region <region>] [--profile <profile>]
| Option |
Environment variable |
|
-c, --cluster |
ECS_CLUSTER |
cluster name |
-s, --service |
ECS_SERVICE |
service name |
-t, --task |
ECS_TASK |
task id or ARN |
--container |
ECS_CONTAINER |
container name |
-r, --region |
AWS_REGION |
AWS region |
-p, --profile |
AWS_PROFILE |
AWS CLI profile |
- Anything missing is chosen from a menu: clusters, then services, then running tasks and containers. A menu with a single entry is picked automatically.
- With
--service, the task is picked among the service’s running tasks. With --task, no service is needed.
- Before connecting, the script prints the command that skips the menus. The task id is left out when the service had only one running task, since ids change on every deployment.
- Without a terminal on stdin (script, cron, pipe) there are no menus: pass
--cluster, --service (or --task) and, for a task with several containers, --container.
Notes
- Everything you type runs in production if that is what you connected to. Sessions are logged by SSM and CloudTrail, depending on your account configuration.
- The tool only reads from the ECS API to build its menus; it changes nothing on AWS.
License
MIT
评论