Amazon Cloud Service (AWS)
Containers/Elastic Container Service (ECS)
1.1. ECS: Enabling Amazon ECS Exec
Problem
note
Default Amazon ECS Exec is disabled in the ECS cluster and we can not execute the command in the container to debug the application, eg: healthcheck, log, etc.
Solution
tip
Since 2021-03-16, Amazon ECS Exec is available for all ECS clusters https://aws.amazon.com/blogs/containers/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2/
Preparation
Step 1: Update IAM Trust Relationship
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Step 2: Update IAM Role Policies
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": "*"
}
]
}
Step 3: Update ECS Task Definition
- Access ECS and Update Task Definition
- Ensure you add the following JSON snippet under containerDefinitions to enable
initProcessEnabled
{
"family": "foo-service",
"containerDefinitions": [
{
...
"volumesFrom": [],
"linuxParameters": {
"initProcessEnabled": true
},
...
"healthCheck": {
"command": [
"CMD-SHELL",
"curl -f http://localhost/healthcheck || exit 1"
],
"interval": 90,
"timeout": 10,
"retries": 3,
"startPeriod": 10
},
"systemControls": []
}
]
}
Step 4: Enable Execution Command in ECS Cluster
- After updating the task definition, we need to update the ECS service to enable the execution command.
{
export ECS_CLUSTER_NAME='foo-cluster'
export ECS_SERVICE_NAME='foo-service'
export AWS_REGION='us-east-2'
aws ecs update-service \
--cluster $ECS_CLUSTER_NAME \
--service $ECS_SERVICE_NAME \
--region $AWS_REGION \
--enable-execute-command \
--force-new-deployment
}
- Check if the execution command is enabled, the output result in showing that is
true
{
export ECS_CLUSTER_NAME='foo-cluster'
export ECS_SERVICE_NAME='foo-service'
export AWS_REGION='us-east-2'
aws ecs describe-services \
--cluster $ECS_CLUSTER_NAME \
--service $ECS_SERVICE_NAME \
--region $AWS_REGION \
| jq '.services[0].enableExecuteCommand'
}

Execution
- Access the ECS console and click on the ECS cluster and select the ECS service to get ECS task arn

{
export ECS_CLUSTER_NAME='foo-cluster'
export AWS_REGION='us-east-2'
aws ecs describe-services \
--cluster $ECS_CLUSTER_NAME \
--service $ECS_SERVICE_NAME \
--region $AWS_REGION \
| jq '.services[0].events[].message'
}
- Execute the command to access the container
{
export ECS_CLUSTER_NAME='foo-cluster'
export ECS_TASK_ARN='arn:aws:ecs:us-east-2:123456789012:task/foo-cluster/1234567890123456789'
export AWS_REGION='us-east-2'
aws ecs execute-command \
--cluster $ECS_CLUSTER_NAME \
--task $ECS_TASK_ARN \
--container foo-container \
--interactive \
--command "/bin/bash"
}
- If we get the error message, so need to install the SessionManagerPlugin
danger
SessionManagerPlugin is not found. Please refer to SessionManager Documentation here: http://docs.aws.amazon.com/console/systems-manager/session-manager-plugin-not-found
Reference
- NEW – Using Amazon ECS Exec to access your containers on AWS Fargate and Amazon EC2
- A Step-by-Step Guide to Enabling Amazon ECS Exec
- Install the Session Manager plugin on macOS