Skip to content

Instantly share code, notes, and snippets.

@matwerber1
Last active October 13, 2023 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matwerber1/e9979bda8ce97025632d06e3f96ae745 to your computer and use it in GitHub Desktop.
Save matwerber1/e9979bda8ce97025632d06e3f96ae745 to your computer and use it in GitHub Desktop.
Example to start an ECS Fargate task an existing cluster, task definition, and VPC, and then use ECS Exec to open an interactive shell with a containr in the task
CLUSTER="your_cluster_name"
REGION="cluster_region"
PRIVATE_SUBNETS="subnet-someSubnet123,subnet-someOtherSubnet456"
TASK_SECURITY_GROUP="security-group-for-ECS-task-ID"
PUBLIC_IP_SETTING="DISABLED"
TASK_CONTAINER_NAME_FOR_ECS_EXEC="container-name-for-ecs-exec"
# Run a Fargate task with ECS Exec enabled:
RUN_TASK_RESULT=$(
aws ecs run-task \
--task-definition d3be26d92154c50f_ecs_td:13 \
--enable-execute-command \
--cluster example \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[$PRIVATE_SUBNETS],securityGroups=[$TASK_SECURITY_GROUP],assignPublicIp=$PUBLIC_IP_SETTING}" \
--region $REGION \
--output json
)
TASK_ID=$(echo $RUN_TASK_RESULT | jq -r '.tasks[0].taskArn')
# Use ECS Exec to open a remote shell with your task; you will need to wait for the task to reach a running status, first.
# You may need to change the command to use a different shell, depending on what is installed in your base image. For example, "/bin/bash"
aws ecs execute-command \
--region $REGION \
--cluster $CLUSTER \
--task $TASK_ID \
--container $TASK_CONTAINER_NAME_FOR_ECS_EXEC \
--command '/bin/sh' \
--interactive
# Stop the task
aws ecs stop-task --cluster $CLUSTER --task $TASK_ID --region $REGION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment