Amazon ECS exec was released with CloudFormation day 1 support. Before we have AWS CDK L2 support, we can simply addPropertyOverride
to enable this feature for our cluster and service.
This repository demonstrates how it works in action.
$ cdk deploy -c use_default_vpc
Write a little helper script(i.e. helper.sh
)
# ecs_exec_service CLUSTER SERVICE CONTAINER
function ecs_exec_service() {
CLUSTER=$1
SERVICE=$2
CONTAINER=$3
TASK=$(aws ecs list-tasks --service-name $SERVICE --cluster $CLUSTER --query 'taskArns[0]' --output text)
ecs_exec_task $CLUSTER $TASK $CONTAINER
}
# ecs_exec_task CLUSTER TASK CONTAINER
function ecs_exec_task() {
aws ecs execute-command \
--cluster $1 \
--task $2 \
--container $3 \
--command "/bin/bash" \
--interactive
}
Test it
source helper.sh
ecs_exec_service CLUSTER SERVICE CONTAINER