Use the AWS FIS aws:ecs:task actions
You can use the aws:ecs:task actions to inject faults into your Amazon ECS tasks. Amazon EC2 and Fargate capacity types are supported.
These actions use
AWS Systems Manager (SSM) documents
to inject faults. To use aws:ecs:task actions, you will need to add a container with an SSM Agent to
your Amazon Elastic Container Service (Amazon ECS) task definition. The container runs an
AWS FIS defined script that registers the Amazon ECS task as Managed Instance
in the SSM service. Additionally, the script retrieves task metadata to add tags to the Managed Instance. The
setup will allow AWS FIS to resolve the target task. This paragraph refers to the Setup in
the diagram below.
AWS FIS also provides a non-root variant of the SSM agent image. The non-root image has the
required fault injection dependencies pre-installed and runs the SSM agent as a dedicated
ssmagent user (UID/GID 61555), requiring no root privileges. For image URIs, see
Non-root container images.
When you run an AWS FIS experiment targeting aws:ecs:task, AWS FIS maps the target Amazon ECS tasks you specify
in an AWS FIS experiment template to a set of SSM managed instances using a resource tag, ECS_TASK_ARN.
The tag value is the ARN of the associated Amazon ECS task where the SSM documents should be executed. This paragraph
refers to the Fault Injection in the diagram below.
The following diagram exemplifies the setup and fault injection on a task with one existing container.
Actions
Limitations
-
The following actions cannot run in parallel:
aws:ecs:task-network-blackhole-port
aws:ecs:task-network-latency
aws:ecs:task-network-packet-loss
-
To use these actions with the root (default) SSM agent container image, first disable Amazon ECS Exec. With the AWS FIS non-root SSM agent container image, these actions can run in the same task as Amazon ECS Exec when the sidecar container drops all Linux capabilities. To do this, set
linuxParameters.capabilities.dropto["ALL"]in the task definition. Note that this preventsaws ecs execute-commandfrom running in the sidecar container itself, but exec into your application containers is unaffected. -
With the AWS FIS non-root SSM agent container image, the aws:ecs:task-kill-process action can stop only a limited set of processes. It can stop only processes that run as uid
61555(thessmagentuser). If you try to stop a process owned by another user, the action returns anOperation not permittederror. This happens because a non-root process without theCAP_KILLcapability can signal only processes of its own uid. To stop processes that run as other users, use the root (default) SSM agent container image, which does not work with Amazon ECS Exec. -
The SSM document execution might have Status Cancelled even if the experiment has State Completed. When executing Amazon ECS actions, the customer-provided duration is used both for the action duration in the experiment and the Amazon EC2 Systems Manager (SSM) document duration. After the action is initiated, it takes some time for the SSM document to start running. Consequently, by the time the specified action duration is reached, the SSM document may still have a few seconds remaining to complete its execution. When the experiment action duration is reached, the action is stopped, and the SSM document execution is cancelled. The fault injection was successful.
Requirements
-
Add the following permissions to the AWS FIS experiment role:
ecs:DescribeTasksssm:SendCommandssm:ListCommandsssm:CancelCommand
-
Add the following permissions to the Amazon ECS task IAM role:
ssm:CreateActivationssm:AddTagsToResourceiam:PassRole
Note that you can specify the ARN of the managed instance role as the resource for
iam:PassRole. -
Create an Amazon ECS task execution IAM role and add the AmazonECSTaskExecutionRolePolicy managed policy.
-
In the task definition, set the environment variable
MANAGED_INSTANCE_ROLE_NAMEto the name of the managed instance role. This is the role that will be attached to the tasks registered as managed instances in SSM. -
Add the following permissions to the managed instance role:
ssm:DeleteActivationssm:DeregisterManagedInstance
-
Add the AmazonSSMManagedInstanceCore managed policy to the managed instance role.
-
Add an SSM agent container to the Amazon ECS task definition. The command script registers Amazon ECS tasks as managed instances.
{ "name": "amazon-ssm-agent", "image": "public.ecr.aws/amazon-ssm-agent/amazon-ssm-agent:latest", "cpu": 0, "links": [], "portMappings": [], "essential": false, "entryPoint": [], "command": [ "/bin/bash", "-c", "set -e; dnf upgrade -y; dnf install jq procps awscli -y; term_handler() { echo \"Deleting SSM activation $ACTIVATION_ID\"; if ! aws ssm delete-activation --activation-id $ACTIVATION_ID --region $ECS_TASK_REGION; then echo \"SSM activation $ACTIVATION_ID failed to be deleted\" 1>&2; fi; MANAGED_INSTANCE_ID=$(jq -e -r .ManagedInstanceID /var/lib/amazon/ssm/registration); echo \"Deregistering SSM Managed Instance $MANAGED_INSTANCE_ID\"; if ! aws ssm deregister-managed-instance --instance-id $MANAGED_INSTANCE_ID --region $ECS_TASK_REGION; then echo \"SSM Managed Instance $MANAGED_INSTANCE_ID failed to be deregistered\" 1>&2; fi; kill -SIGTERM $SSM_AGENT_PID; }; trap term_handler SIGTERM SIGINT; if [[ -z $MANAGED_INSTANCE_ROLE_NAME ]]; then echo \"Environment variable MANAGED_INSTANCE_ROLE_NAME not set, exiting\" 1>&2; exit 1; fi; if ! ps ax | grep amazon-ssm-agent | grep -v grep > /dev/null; then if [[ -n $ECS_CONTAINER_METADATA_URI_V4 ]] ; then echo \"Found ECS Container Metadata, running activation with metadata\"; TASK_METADATA=$(curl \"${ECS_CONTAINER_METADATA_URI_V4}/task\"); ECS_TASK_AVAILABILITY_ZONE=$(echo $TASK_METADATA | jq -e -r '.AvailabilityZone'); ECS_TASK_ARN=$(echo $TASK_METADATA | jq -e -r '.TaskARN'); ECS_TASK_REGION=$(echo $ECS_TASK_AVAILABILITY_ZONE | sed 's/.$//'); ECS_TASK_AVAILABILITY_ZONE_REGEX='^(af|ap|ca|cn|eu|me|sa|us|us-gov)-(central|north|(north(east|west))|south|south(east|west)|east|west)-[0-9]{1}[a-z]{1}$'; if ! [[ $ECS_TASK_AVAILABILITY_ZONE =~ $ECS_TASK_AVAILABILITY_ZONE_REGEX ]]; then echo \"Error extracting Availability Zone from ECS Container Metadata, exiting\" 1>&2; exit 1; fi; ECS_TASK_ARN_REGEX='^arn:(aws|aws-cn|aws-us-gov):ecs:[a-z0-9-]+:[0-9]{12}:task/[a-zA-Z0-9_-]+/[a-zA-Z0-9]+$'; if ! [[ $ECS_TASK_ARN =~ $ECS_TASK_ARN_REGEX ]]; then echo \"Error extracting Task ARN from ECS Container Metadata, exiting\" 1>&2; exit 1; fi; CREATE_ACTIVATION_OUTPUT=$(aws ssm create-activation --iam-role $MANAGED_INSTANCE_ROLE_NAME --tags Key=ECS_TASK_AVAILABILITY_ZONE,Value=$ECS_TASK_AVAILABILITY_ZONE Key=ECS_TASK_ARN,Value=$ECS_TASK_ARN Key=FAULT_INJECTION_SIDECAR,Value=true --region $ECS_TASK_REGION); ACTIVATION_CODE=$(echo $CREATE_ACTIVATION_OUTPUT | jq -e -r .ActivationCode); ACTIVATION_ID=$(echo $CREATE_ACTIVATION_OUTPUT | jq -e -r .ActivationId); if ! amazon-ssm-agent -register -code $ACTIVATION_CODE -id $ACTIVATION_ID -region $ECS_TASK_REGION; then echo \"Failed to register with AWS Systems Manager (SSM), exiting\" 1>&2; exit 1; fi; amazon-ssm-agent & SSM_AGENT_PID=$!; wait $SSM_AGENT_PID; else echo \"ECS Container Metadata not found, exiting\" 1>&2; exit 1; fi; else echo \"SSM agent is already running, exiting\" 1>&2; exit 1; fi" ], "environment": [ { "name": "MANAGED_INSTANCE_ROLE_NAME", "value": "SSMManagedInstanceRole" } ], "environmentFiles": [], "mountPoints": [], "volumesFrom": [], "secrets": [], "dnsServers": [], "dnsSearchDomains": [], "extraHosts": [], "dockerSecurityOptions": [], "dockerLabels": {}, "ulimits": [], "logConfiguration": {}, "systemControls": [] }This example installs packages on startup. If that installation exceeds the memory available to the sidecar, task registration fails and the experiment fails. For more information, see Troubleshooting ECS task actions.
For a more readable version of the script, see Reference version of the script.
-
Enable the Amazon ECS Fault Injection APIs, by setting the
enableFaultInjectionfield in the Amazon ECS task definition:"enableFaultInjection": true, -
When using the
aws:ecs:task-network-blackhole-port,aws:ecs:task-network-latency, oraws:ecs:task-network-packet-lossactions on Fargate tasks, the action must have theuseEcsFaultInjectionEndpointsparameter set totrue. -
When you use the AWS FIS non-root SSM agent container image, set the
useEcsFaultInjectionEndpointsparameter totrue. This requirement applies to both Fargate and Amazon ECS (EC2) launch types. The non-root agent runs without theNET_ADMINLinux capability. On EC2, it cannot use the in-container traffic-control (tc) path. The agent must use the Amazon ECS fault injection endpoints for both launch types. -
When using the
aws:ecs:task-kill-process,aws:ecs:task-network-blackhole-port,aws:ecs:task-network-latency, oraws:ecs:task-network-packet-lossactions, the Amazon ECS task definition must havepidModeset totask. -
When using the
aws:ecs:task-network-blackhole-port,aws:ecs:task-network-latency, oraws:ecs:task-network-packet-lossactions on tasks with EC2 launch type, the networking option in the task definition must be set toawsvpcorhost.
If you use the AWS FIS non-root SSM agent container image, add
ssm:DeleteActivation and ssm:DeregisterManagedInstance
to the Amazon ECS task IAM role instead. Do not add them to the
managed instance role. The non-root image sets ShareCreds:false.
With this setting, the SSM agent keeps the managed instance role
credentials out of a shared credentials file. The entrypoint cleanup then
runs aws ssm delete-activation and
aws ssm deregister-managed-instance with the Amazon ECS task IAM
role.
If the task role lacks these permissions, every stopped task leaves a
managed instance behind in the ConnectionLost state. For more
information about image URIs, see Non-root container images.
Reference version of the script
The following is a more readable version of the script in the Requirements section, for your reference.
#!/usr/bin/env bash # This is the activation script used to register ECS tasks as Managed Instances in SSM # The script retrieves information form the ECS task metadata endpoint to add three tags to the Managed Instance # - ECS_TASK_AVAILABILITY_ZONE: To allow customers to target Managed Instances / Tasks in a specific Availability Zone # - ECS_TASK_ARN: To allow customers to target Managed Instances / Tasks by using the Task ARN # - FAULT_INJECTION_SIDECAR: To make it clear that the tasks were registered as managed instance for fault injection purposes. Value is always 'true'. # The script will leave the SSM Agent running in the background # When the container running this script receives a SIGTERM or SIGINT signal, it will do the following cleanup: # - Delete SSM activation # - Deregister SSM managed instance set -e # stop execution instantly as a query exits while having a non-zero dnf upgrade -y dnf install jq procps awscli -y term_handler() { echo "Deleting SSM activation $ACTIVATION_ID" if ! aws ssm delete-activation --activation-id $ACTIVATION_ID --region $ECS_TASK_REGION; then echo "SSM activation $ACTIVATION_ID failed to be deleted" 1>&2 fi MANAGED_INSTANCE_ID=$(jq -e -r .ManagedInstanceID /var/lib/amazon/ssm/registration) echo "Deregistering SSM Managed Instance $MANAGED_INSTANCE_ID" if ! aws ssm deregister-managed-instance --instance-id $MANAGED_INSTANCE_ID --region $ECS_TASK_REGION; then echo "SSM Managed Instance $MANAGED_INSTANCE_ID failed to be deregistered" 1>&2 fi kill -SIGTERM $SSM_AGENT_PID } trap term_handler SIGTERM SIGINT # check if the required IAM role is provided if [[ -z $MANAGED_INSTANCE_ROLE_NAME ]] ; then echo "Environment variable MANAGED_INSTANCE_ROLE_NAME not set, exiting" 1>&2 exit 1 fi # check if the agent is already running (it will be if ECS Exec is enabled) if ! ps ax | grep amazon-ssm-agent | grep -v grep > /dev/null; then # check if ECS Container Metadata is available if [[ -n $ECS_CONTAINER_METADATA_URI_V4 ]] ; then # Retrieve info from ECS task metadata endpoint echo "Found ECS Container Metadata, running activation with metadata" TASK_METADATA=$(curl "${ECS_CONTAINER_METADATA_URI_V4}/task") ECS_TASK_AVAILABILITY_ZONE=$(echo $TASK_METADATA | jq -e -r '.AvailabilityZone') ECS_TASK_ARN=$(echo $TASK_METADATA | jq -e -r '.TaskARN') ECS_TASK_REGION=$(echo $ECS_TASK_AVAILABILITY_ZONE | sed 's/.$//') # validate ECS_TASK_AVAILABILITY_ZONE ECS_TASK_AVAILABILITY_ZONE_REGEX='^(af|ap|ca|cn|eu|me|sa|us|us-gov)-(central|north|(north(east|west))|south|south(east|west)|east|west)-[0-9]{1}[a-z]{1}$' if ! [[ $ECS_TASK_AVAILABILITY_ZONE =~ $ECS_TASK_AVAILABILITY_ZONE_REGEX ]] ; then echo "Error extracting Availability Zone from ECS Container Metadata, exiting" 1>&2 exit 1 fi # validate ECS_TASK_ARN ECS_TASK_ARN_REGEX='^arn:(aws|aws-cn|aws-us-gov):ecs:[a-z0-9-]+:[0-9]{12}:task/[a-zA-Z0-9_-]+/[a-zA-Z0-9]+$' if ! [[ $ECS_TASK_ARN =~ $ECS_TASK_ARN_REGEX ]] ; then echo "Error extracting Task ARN from ECS Container Metadata, exiting" 1>&2 exit 1 fi # Create activation tagging with Availability Zone and Task ARN CREATE_ACTIVATION_OUTPUT=$(aws ssm create-activation \ --iam-role $MANAGED_INSTANCE_ROLE_NAME \ --tags Key=ECS_TASK_AVAILABILITY_ZONE,Value=$ECS_TASK_AVAILABILITY_ZONE Key=ECS_TASK_ARN,Value=$ECS_TASK_ARN Key=FAULT_INJECTION_SIDECAR,Value=true \ --region $ECS_TASK_REGION) ACTIVATION_CODE=$(echo $CREATE_ACTIVATION_OUTPUT | jq -e -r .ActivationCode) ACTIVATION_ID=$(echo $CREATE_ACTIVATION_OUTPUT | jq -e -r .ActivationId) # Register with AWS Systems Manager (SSM) if ! amazon-ssm-agent -register -code $ACTIVATION_CODE -id $ACTIVATION_ID -region $ECS_TASK_REGION; then echo "Failed to register with AWS Systems Manager (SSM), exiting" 1>&2 exit 1 fi # the agent needs to run in the background, otherwise the trapped signal # won't execute the attached function until this process finishes amazon-ssm-agent & SSM_AGENT_PID=$! # need to keep the script alive, otherwise the container will terminate wait $SSM_AGENT_PID else echo "ECS Container Metadata not found, exiting" 1>&2 exit 1 fi else echo "SSM agent is already running, exiting" 1>&2 exit 1 fi
Non-root container images
The non-root SSM agent container image provided by AWS FIS is hosted in Amazon ECR in each supported AWS Region. When you reference an image from Amazon ECR, you must use the full image URI.
Each image URI ends with an architecture-specific tag. Replace architecture with
x86_64 or arm64 to match the CPU architecture of your task, for example
latest-x86_64 or latest-arm64.
The non-root container image is also available in the AWS ECR Public Gallery
| AWS Region | Image URI |
|---|---|
| US East (Ohio) | 111204668964.dkr.ecr.us-east-2.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| US East (N. Virginia) | 237024525506.dkr.ecr.us-east-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| US West (N. California) | 044771288121.dkr.ecr.us-west-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| US West (Oregon) | 584751651166.dkr.ecr.us-west-2.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Africa (Cape Town) | 323960980714.dkr.ecr.af-south-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Asia Pacific (Hong Kong) | 382188660849.dkr.ecr.ap-east-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Asia Pacific (Mumbai) | 547703943082.dkr.ecr.ap-south-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Asia Pacific (Osaka) | 577137987168.dkr.ecr.ap-northeast-3.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Asia Pacific (Seoul) | 706243847951.dkr.ecr.ap-northeast-2.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Asia Pacific (Singapore) | 893410593473.dkr.ecr.ap-southeast-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Asia Pacific (Sydney) | 088923313816.dkr.ecr.ap-southeast-2.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Asia Pacific (Tokyo) | 734102830451.dkr.ecr.ap-northeast-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Canada (Central) | 303185496657.dkr.ecr.ca-central-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Europe (Frankfurt) | 294983767828.dkr.ecr.eu-central-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Europe (Ireland) | 676591241388.dkr.ecr.eu-west-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Europe (London) | 292832244570.dkr.ecr.eu-west-2.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Europe (Milan) | 950165721128.dkr.ecr.eu-south-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Europe (Paris) | 175562788122.dkr.ecr.eu-west-3.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Europe (Spain) | 762616536414.dkr.ecr.eu-south-2.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Europe (Stockholm) | 981005111022.dkr.ecr.eu-north-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| Europe (Zurich) | 532975550017.dkr.ecr.eu-central-2.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| South America (São Paulo) | 005407597607.dkr.ecr.sa-east-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| AWS GovCloud (US-East) | 636998192474.dkr.ecr.us-gov-east-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
| AWS GovCloud (US-West) | 636998914323.dkr.ecr.us-gov-west-1.amazonaws.com/aws-fis-ssm-agent-non-root:latest- |
Example experiment template
The following is an example experiment template for the aws:ecs:task-cpu-stress action.
{ "description": "Run CPU stress on the target ECS tasks", "targets": { "myTasks": { "resourceType": "aws:ecs:task", "resourceArns": [ "arn:aws:ecs:us-east-1:111122223333:task/my-cluster/09821742c0e24250b187dfed8EXAMPLE" ], "selectionMode": "ALL" } }, "actions": { "EcsTask-cpu-stress": { "actionId": "aws:ecs:task-cpu-stress", "parameters": { "duration": "PT1M" }, "targets": { "Tasks": "myTasks" } } }, "stopConditions": [ { "source": "none", } ], "roleArn": "arn:aws:iam::111122223333:role/fis-experiment-role", "tags": {} }
Troubleshooting ECS task actions
The following topics help you resolve problems when you run AWS FIS experiments against Amazon ECS tasks.
ECS task not registered as an SSM managed instance
This error means the SSM agent sidecar container did not register the task as an SSM
managed instance. The task can be RUNNING with correct IAM permissions and
still produce this error.
One cause is a sidecar container that runs out of memory and stops before it registers. The
container is not essential and writes no logs in this state, so the task stays
RUNNING and the stop is easy to miss. Check the container for exit code
137 and an out-of-memory stopped reason, in the Amazon ECS console or the
DescribeTasks response. To fix it, give the sidecar more memory, or use a AWS FIS
non-root SSM agent container image (see
Non-root container images). These
images install no packages on startup, so the sidecar needs less memory.