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 operazioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Mentre le azioni mostrano come richiamare le singole funzioni di servizio, è possibile visualizzare le azioni nel loro contesto negli scenari correlati.

Ogni esempio include un collegamento al codice sorgente completo, in cui è possibile trovare istruzioni su come configurare ed eseguire il codice nel contesto.

Argomenti

Azioni

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

AWS CLI

Per creare un modello di esperimento

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

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

Per eliminare un modello di esperimento

L'delete-experiment-templateesempio 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, vedete 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

Per ottenere i dettagli delle azioni

L'get-actionesempio 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, vedere Azioni nella guida per l'utente di AWS Fault Injection Simulator.

  • Per API i dettagli, vedere GetActionin AWS CLI Command Reference.

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

AWS CLI

Per ottenere i dettagli del modello di esperimento

L'get-experiment-templateesempio 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, vedete Modelli di esperimenti nella Guida per l'utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzareget-experiment.

AWS CLI

Per ottenere i dettagli dell'esperimento

L'get-experimentesempio 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 API i dettagli, vedere GetExperimentin AWS CLI Command Reference.

Il seguente esempio di codice mostra come utilizzarelist-actions.

AWS CLI

Per elencare le azioni

L'list-actionsesempio 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, vedere Actions in the AWS Fault Injection Simulator User Guide.

  • Per API i dettagli, vedere ListActionsin AWS CLI Command Reference.

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

AWS CLI

Per elencare i modelli di esperimenti

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

Per 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, consultate Experiments in the AWS Fault Injection Simulator User Guide.

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

AWS CLI

Per elencare i tag di una risorsa

L'list-tags-for-resourceesempio 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, vedete Etichettare AWS FIS le vostre risorse nella Guida per l'utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzarestart-experiment.

AWS CLI

Per iniziare un esperimento

L'start-experimentesempio 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, vedete Experiments for AWS FIS nella Guida per l'utente di AWS Fault Injection Simulator.

Il seguente esempio di codice mostra come utilizzarestop-experiment.

AWS CLI

Interrompere un esperimento

L'stop-experimentesempio seguente interrompe 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, vedete Experiments for AWS FIS nella Guida per l'utente di AWS Fault Injection Simulator.

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

Il seguente esempio di codice mostra come utilizzaretag-resource.

AWS CLI

Per etichettare una risorsa

L'tag-resourceesempio seguente contrassegna la 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 AWS FIS risorse nella Guida per l'utente di AWS Fault Injection Simulator.

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

Il seguente esempio di codice mostra come utilizzareuntag-resource.

AWS CLI

Per rimuovere i tag da una risorsa

L'untag-resourceesempio seguente rimuove i tag dalla risorsa specificata.

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

Questo comando non produce alcun output.

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

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

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

AWS CLI

Per aggiornare un modello di esperimento

L'update-experiment-templateesempio 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, vedete Aggiornare un modello di esperimento nella Guida per l'utente di AWS Fault Injection Simulator.