Sono disponibili altri esempi di AWS SDK nel repository AWS Doc SDK Examples.
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 di utilizzo 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
L’esempio di codice seguente mostra come utilizzare create-experiment-template.
- AWS CLI
-
Come creare un modello di esperimento
L'
create-experiment-templateesempio seguente crea un modello di esperimento nel tuo account FIS. AWSaws fis create-experiment-template \ --cli-input-jsonfile://myfile.jsonContenuto 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.
-
Per i dettagli sull'API, vedi CreateExperimentTemplate
in AWS CLI Command Reference.
-
L’esempio di codice seguente mostra come utilizzare delete-experiment-template.
- AWS CLI
-
Come eliminare un modello di esperimento
L’esempio
delete-experiment-templateseguente elimina il modello di esperimento specificato.aws fis delete-experiment-template \ --idABCDE1fgHIJkLmNopOutput:
{ "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.
-
Per i dettagli sull'API, vedi in Command Reference. DeleteExperimentTemplate
AWS CLI
-
L’esempio di codice seguente mostra come utilizzare get-action.
- AWS CLI
-
Come ottenere dettagli sull’azione
L’esempio
get-actionseguente ottiene i dettagli dell’azione specificata.aws fis get-action \ --idaws:ec2:stop-instancesOutput:
{ "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.
-
Per i dettagli sull'API, vedi GetAction
in AWS CLI Command Reference.
-
L’esempio di codice seguente mostra come utilizzare get-experiment-template.
- AWS CLI
-
Come ottenere i dettagli del modello di esperimento
L’esempio
get-experiment-templateseguente ottiene i dettagli del modello di esperimento specificato.aws fis get-experiment-template \ --idABCDE1fgHIJkLmNopOutput:
{ "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.
-
Per i dettagli sull'API, vedi GetExperimentTemplate
in Command Reference. AWS CLI
-
L’esempio di codice seguente mostra come utilizzare get-experiment.
- AWS CLI
-
Come ottenere i dettagli dell’esperimento
L’esempio
get-experimentseguente ottiene i dettagli dell’esperimento specificato.aws fis get-experiment \ --idABC12DeFGhI3jKLMNOPOutput:
{ "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, vedi GetExperiment
in AWS CLI Command Reference.
-
L’esempio di codice seguente mostra come utilizzare list-actions.
- AWS CLI
-
Come elencare le azioni
L’esempio
list-actionsseguente elenca le azioni disponibili.aws fis list-actionsOutput:
{ "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.
-
Per i dettagli sull'API, vedi ListActions
in AWS CLI Command Reference.
-
L’esempio di codice seguente mostra come utilizzare list-experiment-templates.
- AWS CLI
-
Come elencare i modelli di esperimento
L'
list-experiment-templatesesempio seguente elenca i modelli di esperimento presenti nel tuo account. AWSaws fis list-experiment-templatesOutput:
{ "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.
-
Per i dettagli sull'API, consulta ListExperimentTemplates
AWS CLI Command Reference.
-
L’esempio di codice seguente mostra come utilizzare list-experiments.
- AWS CLI
-
Come elencare gli esperimenti
L'
list-experimentsesempio seguente elenca gli esperimenti nel tuo AWS account.aws fis list-experimentsOutput:
{ "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.
-
Per i dettagli sull'API, vedi ListExperiments
in AWS CLI Command Reference.
-
L’esempio di codice seguente mostra come utilizzare list-tags-for-resource.
- AWS CLI
-
Come elencare i tag per una risorsa
L’esempio
list-tags-for-resourceseguente elenca i tag per la risorsa specificata.aws fis list-tags-for-resource \ --resource-arnarn:aws:fis:us-west-2:123456789012:experiment/ABC12DeFGhI3jKLMNOPOutput:
{ "tags": { "key1": "value1", "key2": "value2" } }Per ulteriori informazioni, consulta Etichettare le risorse FIS nella AWS Guida per l'utente di AWS Fault Injection Simulator.
-
Per i dettagli sulle API, consulta la sezione AWS CLI Command ListTagsForResource
Reference.
-
L’esempio di codice seguente mostra come utilizzare start-experiment.
- AWS CLI
-
Come avviare un esperimento
L’esempio
start-experimentseguente avvia l’esperimento specificato.aws fis start-experiment \ --experiment-template-idABCDE1fgHIJkLmNopOutput:
{ "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, vedi StartExperiment
in AWS CLI Command Reference.
-
L’esempio di codice seguente mostra come utilizzare stop-experiment.
- AWS CLI
-
Come interrompere un esperimento
L’esempio
stop-experimentseguente arresta l’esecuzione dell’esperimento specificato.aws fis stop-experiment \ --idABC12DeFGhI3jKLMNOPOutput:
{ "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, vedi StopExperiment
in AWS CLI Command Reference.
-
L’esempio di codice seguente mostra come utilizzare tag-resource.
- AWS CLI
-
Come taggare a una risorsa
L’esempio
tag-resourceseguente associa i tag alla risorsa specificata.aws fis tag-resource \ --resource-arnarn:aws:fis:us-west-2:123456789012:experiment/ABC12DeFGhI3jKLMNOP\ --tagskey1=value1,key2=value2Questo 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 sulle API, consulta la sezione AWS CLI Command TagResource
Reference.
-
L’esempio di codice seguente mostra come utilizzare untag-resource.
- AWS CLI
-
Come rimuovere un tag da una risorsa
L’esempio
untag-resourceseguente rimuove i tag dall’archivio specificato.aws fis untag-resource \ --resource-arnarn:aws:fis:us-west-2:123456789012:experiment/ABC12DeFGhI3jKLMNOPQuesto 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 sulle API, consulta la sezione AWS CLI Command UntagResource
Reference.
-
L’esempio di codice seguente mostra come utilizzare update-experiment-template.
- AWS CLI
-
Come aggiornare un modello di esperimento
L’esempio
update-experiment-templateseguente aggiorna la descrizione del modello di esperimento specificato.aws fis update-experiment-template \ --idABCDE1fgHIJkLmNop\ ---descriptionmyExperimentTemplateOutput:
{ "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.
-
Per i dettagli sull'API, vedi in Command Reference. UpdateExperimentTemplate
AWS CLI
-