Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Usa le azioni AWS FIS aws:ecs:task
Puoi utilizzare le azioni aws:ecs:task per inserire errori nelle tue attività Amazon ECS. Sono supportati EC2 i tipi di capacità Amazon e Fargate.
Queste azioni utilizzano documenti AWS Systems Manager (SSM) per inserire errori. Per utilizzare aws:ecs:task
le azioni, dovrai aggiungere un contenitore con un agente SSM alla definizione del task Amazon Elastic Container Service (Amazon ECS). Il contenitore esegue uno script definito dal AWS FIS che registra l'attività Amazon ECS come istanza gestita nel servizio SSM. Inoltre, lo script recupera i metadati delle attività per aggiungere tag all'istanza gestita. La configurazione consentirà a AWS FIS di risolvere il task di destinazione. Questo paragrafo si riferisce alla configurazione riportata nel diagramma seguente.
Quando esegui un targeting di un esperimento AWS FISaws:ecs:task
, AWS FIS mappa le attività Amazon ECS di destinazione specificate in un modello di esperimento AWS FIS a un set di istanze gestite SSM utilizzando un tag di risorsa,. ECS_TASK_ARN
Il valore del tag è l'ARN del task Amazon ECS associato in cui devono essere eseguiti i documenti SSM. Questo paragrafo si riferisce alla Fault Injection nel diagramma seguente.
Il diagramma seguente esemplifica la configurazione e l'iniezione dei guasti in un'operazione con un contenitore esistente.

Operazioni
Limitazioni
-
Le seguenti azioni non possono essere eseguite in parallelo:
aws:ecs:task-network-blackhole-port
aws:ecs:task-network-latency
aws:ecs:task-network-packet-loss
-
Se hai abilitato Amazon ECS Exec, devi disabilitarlo prima di poter utilizzare queste azioni.
-
L'esecuzione del documento SSM potrebbe avere lo stato annullato anche se l'esperimento ha lo stato completato. Quando si eseguono azioni Amazon ECS, la durata fornita dal cliente viene utilizzata sia per la durata dell'azione nell'esperimento che per la durata del documento Amazon EC2 Systems Manager (SSM). Dopo l'avvio dell'azione, è necessario del tempo prima che il documento SSM inizi a funzionare. Di conseguenza, quando viene raggiunta la durata dell'azione specificata, al documento SSM potrebbero rimanere ancora alcuni secondi per completare l'esecuzione. Quando viene raggiunta la durata dell'azione dell'esperimento, l'azione viene interrotta e l'esecuzione del documento SSM viene annullata. L'iniezione del guasto ha avuto successo.
Requisiti
-
Aggiungere le seguenti autorizzazioni al ruolo dell'esperimento AWS FIS:
ssm:SendCommand
ssm:ListCommands
ssm:CancelCommand
-
Aggiungi le seguenti autorizzazioni al ruolo IAM dell'attività Amazon ECS:
ssm:CreateActivation
ssm:AddTagsToResource
iam:PassRole
Tieni presente che puoi specificare l'ARN del ruolo dell'istanza gestita come risorsa per.
iam:PassRole
-
Crea un ruolo IAM per l'esecuzione delle attività di Amazon ECS e aggiungi la policy ECSTask ExecutionRolePolicy gestita di Amazon.
-
Nella definizione dell'attività, imposta la variabile
MANAGED_INSTANCE_ROLE_NAME
di ambiente sul nome del ruolo dell'istanza gestita. Questo è il ruolo che verrà assegnato alle attività registrate come istanze gestite in SSM. -
Aggiungi le seguenti autorizzazioni al ruolo dell'istanza gestita:
ssm:DeleteActivation
ssm:DeregisterManagedInstance
-
Aggiungi la policy SSMManaged InstanceCore gestita di Amazon al ruolo dell'istanza gestita.
-
Aggiungi un contenitore di agenti SSM alla definizione del task di Amazon ECS. Lo script di comando registra le attività di Amazon ECS come istanze gestite.
{ "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": [] }Per una versione più leggibile dello script, consulta. Versione di riferimento dello script
-
Abilita Amazon ECS Fault Injection APIs impostando il
enableFaultInjection
campo nella definizione del task di Amazon ECS:"enableFaultInjection": true,
-
Quando si utilizzano le
aws:ecs:task-network-packet-loss
azioniaws:ecs:task-network-blackhole-port
aws:ecs:task-network-latency
, o sulle attività di Fargate, l'azione deve avere iluseEcsFaultInjectionEndpoints
parametro impostato su.true
-
Quando si utilizzano le
aws:ecs:task-network-packet-loss
azioniaws:ecs:task-kill-process
,aws:ecs:task-network-blackhole-port
, eaws:ecs:task-network-latency
,,,,, la definizione del task di Amazon ECS deve esserepidMode
impostata su.task
-
Quando si utilizzano le
aws:ecs:task-network-packet-loss
azioniaws:ecs:task-network-blackhole-port
aws:ecs:task-network-latency
, e,,,,, la definizione del task di Amazon ECS deve esserenetworkMode
impostata su un valore diversobridge
da.
Versione di riferimento dello script
Di seguito è riportata una versione più leggibile dello script nella sezione Requisiti, come riferimento.
#!/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
Esempio di modello di esperimento
Di seguito è riportato un esempio di modello di esperimento per l'aws:ecs:task-cpu-stressazione.
{ "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": {} }