AWS FIS esempi utilizzando AWS CLI - AWS Command Line Interface

Questa documentazione è valida AWS CLI solo per la versione 1. Per la documentazione relativa alla versione 2 di AWS CLI, consulta la Guida per l'utente della versione 2.

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à.

AWS FIS esempi utilizzando AWS CLI

I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando AWS Command Line Interface with AWS FIS.

Le azioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le azioni mostrino come richiamare le singole funzioni del servizio, è possibile visualizzarle contestualizzate negli scenari correlati.

Ogni esempio include un link al codice sorgente completo, in cui vengono fornite le istruzioni su come configurare ed eseguire il codice nel contesto.

Argomenti

Azioni

Il seguente esempio di codice mostra come utilizzarecreate-experiment-template.

AWS CLI

Come creare un modello di esperimento

L'create-experiment-templateesempio seguente crea un modello di esperimento nel tuo account AWS FIS.

aws fis create-experiment-template \ --cli-input-json file://myfile.json

Contenuto di myfile.json:

{ "description": "experimentTemplate", "stopConditions": [ { "source": "aws:cloudwatch:alarm", "value": "arn:aws:cloudwatch:us-west-2:123456789012:alarm:alarmName" } ], "targets": { "Instances-Target-1": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:us-west-2:123456789012:instance/i-12a3b4c56d78e9012" ], "selectionMode": "ALL" } }, "actions": { "reboot": { "actionId": "aws:ec2:reboot-instances", "description": "reboot", "parameters": {}, "targets": { "Instances": "Instances-Target-1" } } }, "roleArn": "arn:aws:iam::123456789012:role/myRole" }

Output:

{ "experimentTemplate": { "id": "ABCDE1fgHIJkLmNop", "description": "experimentTemplate", "targets": { "Instances-Target-1": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:us-west-2:123456789012:instance/i-12a3b4c56d78e9012" ], "selectionMode": "ALL" } }, "actions": { "reboot": { "actionId": "aws:ec2:reboot-instances", "description": "reboot", "parameters": {}, "targets": { "Instances": "Instances-Target-1" } } }, "stopConditions": [ { "source": "aws:cloudwatch:alarm", "value": "arn:aws:cloudwatch:us-west-2:123456789012:alarm:alarmName" } ], "creationTime": 1616434850.659, "lastUpdateTime": 1616434850.659, "roleArn": "arn:aws:iam::123456789012:role/myRole", "tags": {} } }

Per ulteriori informazioni, consulta Creare un modello di esperimento nella Guida per l’utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzaredelete-experiment-template.

AWS CLI

Come eliminare un modello di esperimento

L’esempio delete-experiment-template seguente elimina il modello di esperimento specificato.

aws fis delete-experiment-template \ --id ABCDE1fgHIJkLmNop

Output:

{ "experimentTemplate": { "id": "ABCDE1fgHIJkLmNop", "description": "myExperimentTemplate", "targets": { "Instances-Target-1": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:us-west-2:123456789012:instance/i-12a3b4c56d78e9012" ], "selectionMode": "ALL" } }, "actions": { "testaction": { "actionId": "aws:ec2:stop-instances", "parameters": {}, "targets": { "Instances": "Instances-Target-1" } } }, "stopConditions": [ { "source": "none" } ], "creationTime": 1616017191.124, "lastUpdateTime": 1616017859.607, "roleArn": "arn:aws:iam::123456789012:role/FISRole" } }

Per ulteriori informazioni, consulta Eliminare un modello di esperimento nella Guida per l’utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzareget-action.

AWS CLI

Come ottenere dettagli sull’azione

L’esempio get-action seguente ottiene i dettagli dell’azione specificata.

aws fis get-action \ --id aws:ec2:stop-instances

Output:

{ "action": { "id": "aws:ec2:stop-instances", "description": "Stop the specified EC2 instances.", "parameters": { "startInstancesAfterDuration": { "description": "The time to wait before restarting the instances (ISO 8601 duration).", "required": false } }, "targets": { "Instances": { "resourceType": "aws:ec2:instance" } }, "tags": {} } }

Per ulteriori informazioni, consulta Azioni nella Guida per l’utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzareget-experiment-template.

AWS CLI

Come ottenere i dettagli del modello di esperimento

L’esempio get-experiment-template seguente ottiene i dettagli del modello di esperimento specificato.

aws fis get-experiment-template \ --id ABCDE1fgHIJkLmNop

Output:

{ "experimentTemplate": { "id": "ABCDE1fgHIJkLmNop", "description": "myExperimentTemplate", "targets": { "Instances-Target-1": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:us-west-2:123456789012:instance/i-12a3b4c56d78e9012" ], "selectionMode": "ALL" } }, "actions": { "testaction": { "actionId": "aws:ec2:stop-instances", "parameters": {}, "targets": { "Instances": "Instances-Target-1" } } }, "stopConditions": [ { "source": "none" } ], "creationTime": 1616017191.124, "lastUpdateTime": 1616017331.51, "roleArn": "arn:aws:iam::123456789012:role/FISRole", "tags": { "key: "value" } } }

Per ulteriori informazioni, consulta Modelli di esperimenti nella Guida per l’utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzareget-experiment.

AWS CLI

Come ottenere i dettagli dell’esperimento

L’esempio get-experiment seguente ottiene i dettagli dell’esperimento specificato.

aws fis get-experiment \ --id ABC12DeFGhI3jKLMNOP

Output:

{ "experiment": { "id": "ABC12DeFGhI3jKLMNOP", "experimentTemplateId": "ABCDE1fgHIJkLmNop", "roleArn": "arn:aws:iam::123456789012:role/myRole", "state": { "status": "completed", "reason": "Experiment completed." }, "targets": { "Instances-Target-1": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:us-west-2:123456789012:instance/i-12a3b4c56d78e9012" ], "selectionMode": "ALL" } }, "actions": { "reboot": { "actionId": "aws:ec2:reboot-instances", "parameters": {}, "targets": { "Instances": "Instances-Target-1" }, "state": { "status": "completed", "reason": "Action was completed." } } }, "stopConditions": [ { "source": "none" } ], "creationTime": 1616432509.662, "startTime": 1616432509.962, "endTime": 1616432522.307, "tags": {} } }

Per ulteriori informazioni, vedere Experiments for AWS FIS nella Guida per l'utente di AWS Fault Injection Simulator.

  • Per i dettagli sull'API, vedere GetExperimentin AWS CLI Command Reference.

Il seguente esempio di codice mostra come utilizzarelist-actions.

AWS CLI

Come elencare le azioni

L’esempio list-actions seguente elenca le azioni disponibili.

aws fis list-actions

Output:

{ "actions": [ { "id": "aws:ec2:reboot-instances", "description": "Reboot the specified EC2 instances.", "targets": { "Instances": { "resourceType": "aws:ec2:instance" } }, "tags": {} }, { "id": "aws:ec2:stop-instances", "description": "Stop the specified EC2 instances.", "targets": { "Instances": { "resourceType": "aws:ec2:instance" } }, "tags": {} }, { "id": "aws:ec2:terminate-instances", "description": "Terminate the specified EC2 instances.", "targets": { "Instances": { "resourceType": "aws:ec2:instance" } }, "tags": {} }, { "id": "aws:ecs:drain-container-instances", "description": "Drain percentage of underlying EC2 instances on an ECS cluster.", "targets": { "Clusters": { "resourceType": "aws:ecs:cluster" } }, "tags": {} }, { "id": "aws:eks:terminate-nodegroup-instances", "description": "Terminates a percentage of the underlying EC2 instances in an EKS cluster.", "targets": { "Nodegroups": { "resourceType": "aws:eks:nodegroup" } }, "tags": {} }, { "id": "aws:fis:inject-api-internal-error", "description": "Cause an AWS service to return internal error responses for specific callers and operations.", "targets": { "Roles": { "resourceType": "aws:iam:role" } }, "tags": {} }, { "id": "aws:fis:inject-api-throttle-error", "description": "Cause an AWS service to return throttled responses for specific callers and operations.", "targets": { "Roles": { "resourceType": "aws:iam:role" } }, "tags": {} }, { "id": "aws:fis:inject-api-unavailable-error", "description": "Cause an AWS service to return unavailable error responses for specific callers and operations.", "targets": { "Roles": { "resourceType": "aws:iam:role" } }, "tags": {} }, { "id": "aws:fis:wait", "description": "Wait for the specified duration. Stop condition monitoring will continue during this time.", "tags": {} }, { "id": "aws:rds:failover-db-cluster", "description": "Failover a DB Cluster to one of the replicas.", "targets": { "Clusters": { "resourceType": "aws:rds:cluster" } }, "tags": {} }, { "id": "aws:rds:reboot-db-instances", "description": "Reboot the specified DB instances.", "targets": { "DBInstances": { "resourceType": "aws:rds:db" } }, "tags": {} }, { "id": "aws:ssm:send-command", "description": "Run the specified SSM document.", "targets": { "Instances": { "resourceType": "aws:ec2:instance" } }, "tags": {} } ] }

Per ulteriori informazioni, consulta Azioni nella Guida per l’utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzarelist-experiment-templates.

AWS CLI

Come elencare i modelli di esperimento

L'list-experiment-templatesesempio seguente elenca i modelli di esperimento presenti nel tuo AWS account.

aws fis list-experiment-templates

Output:

{ "experimentTemplates": [ { "id": "ABCDE1fgHIJkLmNop", "description": "myExperimentTemplate", "creationTime": 1616017191.124, "lastUpdateTime": 1616017191.124, "tags": { "key": "value" } } ] }

Per ulteriori informazioni, consulta Modelli di esperimenti nella Guida per l’utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzarelist-experiments.

AWS CLI

Come elencare gli esperimenti

L'list-experimentsesempio seguente elenca gli esperimenti nel tuo AWS account.

aws fis list-experiments

Output:

{ "experiments": [ { "id": "ABCdeF1GHiJkLM23NO", "experimentTemplateId": "ABCDE1fgHIJkLmNop", "state": { "status": "running", "reason": "Experiment is running." }, "creationTime": 1616017341.197, "tags": { "key": "value" } } ] }

Per ulteriori informazioni, consulta Esperimenti nella Guida per l’utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzarelist-tags-for-resource.

AWS CLI

Come elencare i tag per una risorsa

L’esempio list-tags-for-resource seguente elenca i tag per la risorsa specificata.

aws fis list-tags-for-resource \ --resource-arn arn:aws:fis:us-west-2:123456789012:experiment/ABC12DeFGhI3jKLMNOP

Output:

{ "tags": { "key1": "value1", "key2": "value2" } }

Per ulteriori informazioni, consulta Etichettare le risorse FIS nella AWS Guida per l'utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzarestart-experiment.

AWS CLI

Come avviare un esperimento

L’esempio start-experiment seguente avvia l’esperimento specificato.

aws fis start-experiment \ --experiment-template-id ABCDE1fgHIJkLmNop

Output:

{ "experiment": { "id": "ABC12DeFGhI3jKLMNOP", "experimentTemplateId": "ABCDE1fgHIJkLmNop", "roleArn": "arn:aws:iam::123456789012:role/myRole", "state": { "status": "initiating", "reason": "Experiment is initiating." }, "targets": { "Instances-Target-1": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:us-west-2:123456789012:instance/i-12a3b4c56d78e9012" ], "selectionMode": "ALL" } }, "actions": { "reboot": { "actionId": "aws:ec2:reboot-instances", "parameters": {}, "targets": { "Instances": "Instances-Target-1" }, "state": { "status": "pending", "reason": "Initial state" } } }, "stopConditions": [ { "source": "none" } ], "creationTime": 1616432464.025, "startTime": 1616432464.374, "tags": {} } }

Per ulteriori informazioni, vedere Experiments for AWS FIS nella Guida per l'utente di AWS Fault Injection Simulator.

  • Per i dettagli sull'API, vedere StartExperimentin AWS CLI Command Reference.

Il seguente esempio di codice mostra come utilizzarestop-experiment.

AWS CLI

Come interrompere un esperimento

L’esempio stop-experiment seguente arresta l’esecuzione dell’esperimento specificato.

aws fis stop-experiment \ --id ABC12DeFGhI3jKLMNOP

Output:

{ "experiment": { "id": "ABC12DeFGhI3jKLMNOP", "experimentTemplateId": "ABCDE1fgHIJkLmNop", "roleArn": "arn:aws:iam::123456789012:role/myRole", "state": { "status": "stopping", "reason": "Stopping Experiment." }, "targets": { "Instances-Target-1": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:us-west-2:123456789012:instance/i-12a3b4c56d78e9012" ], "selectionMode": "ALL" } }, "actions": { "reboot": { "actionId": "aws:ec2:reboot-instances", "parameters": {}, "targets": { "Instances": "Instances-Target-1" }, "startAfter": [ "wait" ], "state": { "status": "pending", "reason": "Initial state." } }, "wait": { "actionId": "aws:fis:wait", "parameters": { "duration": "PT5M" }, "state": { "status": "running", "reason": "" } } }, "stopConditions": [ { "source": "none" } ], "creationTime": 1616432680.927, "startTime": 1616432681.177, "tags": {} } }

Per ulteriori informazioni, vedere Experiments for AWS FIS nella Guida per l'utente di AWS Fault Injection Simulator.

  • Per i dettagli sull'API, vedere StopExperimentin AWS CLI Command Reference.

Il seguente esempio di codice mostra come utilizzaretag-resource.

AWS CLI

Come taggare a una risorsa

L’esempio tag-resource seguente associa i tag alla risorsa specificata.

aws fis tag-resource \ --resource-arn arn:aws:fis:us-west-2:123456789012:experiment/ABC12DeFGhI3jKLMNOP \ --tags key1=value1,key2=value2

Questo comando non produce alcun output.

Per ulteriori informazioni, consulta Etichettare le risorse FIS nella AWS Guida per l'utente di AWS Fault Injection Simulator.

  • Per i dettagli sull'API, vedere TagResourcein AWS CLI Command Reference.

Il seguente esempio di codice mostra come utilizzareuntag-resource.

AWS CLI

Come rimuovere un tag da una risorsa

L’esempio untag-resource seguente rimuove i tag dall’archivio specificato.

aws fis untag-resource \ --resource-arn arn:aws:fis:us-west-2:123456789012:experiment/ABC12DeFGhI3jKLMNOP

Questo comando non produce alcun output.

Per ulteriori informazioni, consulta Etichettare le risorse FIS nella AWS Guida per l'utente di AWS Fault Injection Simulator.

  • Per i dettagli sull'API, vedere UntagResourcein AWS CLI Command Reference.

Il seguente esempio di codice mostra come utilizzareupdate-experiment-template.

AWS CLI

Come aggiornare un modello di esperimento

L’esempio update-experiment-template seguente aggiorna la descrizione del modello di esperimento specificato.

aws fis update-experiment-template \ --id ABCDE1fgHIJkLmNop \ ---description myExperimentTemplate

Output:

{ "experimentTemplate": { "id": "ABCDE1fgHIJkLmNop", "description": "myExperimentTemplate", "targets": { "Instances-Target-1": { "resourceType": "aws:ec2:instance", "resourceArns": [ "arn:aws:ec2:us-west-2:123456789012:instance/i-12a3b4c56d78e9012" ], "selectionMode": "ALL" } }, "actions": { "testaction": { "actionId": "aws:ec2:stop-instances", "parameters": {}, "targets": { "Instances": "Instances-Target-1" } } }, "stopConditions": [ { "source": "none" } ], "creationTime": 1616017191.124, "lastUpdateTime": 1616017859.607, "roleArn": "arn:aws:iam::123456789012:role/FISRole", "tags": { "key": "value" } } }

Per ulteriori informazioni, consulta Aggiornare un modello di esperimento nella Guida per l’utente di AWS Fault Injection Simulator.