AWS FIS 範例使用 AWS CLI - AWS Command Line Interface

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS FIS 範例使用 AWS CLI

下列程式碼範例說明如何使用 AWS Command Line Interface 與來執行動作及實作常見案例 AWS FIS。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

每個範例都包含一個連結 GitHub,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例會示範如何使用create-experiment-template

AWS CLI

建立實驗樣板的步驟

下列create-experiment-template範例會在 AWS FIS 帳戶中建立實驗範本。

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

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" }

輸出:

{ "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": {} } }

有關更多信息,請參閱AWS 故障注入模擬器用戶指南中的創建實驗模板

下列程式碼範例會示範如何使用delete-experiment-template

AWS CLI

刪除實驗樣板的步驟

下列delete-experiment-template範例會刪除指定的實驗範本。

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

輸出:

{ "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" } }

有關更多信息,請參閱AWS 故障注入模擬器用戶指南中的刪除實驗模板

下列程式碼範例會示範如何使用get-action

AWS CLI

取得動作詳細資訊

下列get-action範例會取得指定動作的詳細資訊。

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

輸出:

{ "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": {} } }

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南中的動作

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考GetAction中的。

下列程式碼範例會示範如何使用get-experiment-template

AWS CLI

獲取實驗模板詳細信息

下列get-experiment-template範例會取得指定實驗範本的詳細資訊。

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

輸出:

{ "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" } } }

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南中的實驗範本

下列程式碼範例會示範如何使用get-experiment

AWS CLI

取得實驗詳細資料

下列get-experiment範例會取得指定實驗的詳細資訊。

aws fis get-experiment \ --id ABC12DeFGhI3jKLMNOP

輸出:

{ "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": {} } }

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南中的 AWS FIS 實驗

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考GetExperiment中的。

下列程式碼範例會示範如何使用list-actions

AWS CLI

列出動作的步驟

下列list-actions範例會列出可用的動作。

aws fis list-actions

輸出:

{ "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": {} } ] }

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南中的動作

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考ListActions中的。

下列程式碼範例會示範如何使用list-experiment-templates

AWS CLI

列出實驗範本的步驟

下列範list-experiment-templates例會列出您 AWS 帳戶中的實驗範本。

aws fis list-experiment-templates

輸出:

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

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南中的實驗範本

下列程式碼範例會示範如何使用list-experiments

AWS CLI

列出實驗

下列list-experiments範例會列出您 AWS 帳戶中的實驗。

aws fis list-experiments

輸出:

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

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南中的實驗

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考ListExperiments中的。

下列程式碼範例會示範如何使用list-tags-for-resource

AWS CLI

若要列出資源的標籤

下列list-tags-for-resource範例會列出指定資源的標籤。

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

輸出:

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

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南的標記 AWS FIS 資源

下列程式碼範例會示範如何使用start-experiment

AWS CLI

開始實驗的步驟

下面的start-experiment例子啟動指定的實驗。

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

輸出:

{ "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": {} } }

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南中的 AWS FIS 實驗

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考StartExperiment中的。

下列程式碼範例會示範如何使用stop-experiment

AWS CLI

停止實驗的步驟

下列stop-experiment範例會停止執行指定的實驗。

aws fis stop-experiment \ --id ABC12DeFGhI3jKLMNOP

輸出:

{ "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": {} } }

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南中的 AWS FIS 實驗

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考StopExperiment中的。

下列程式碼範例會示範如何使用tag-resource

AWS CLI

標記資源的步驟

下列tag-resource範例會標記指定的資源。

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

此命令不會產生輸出。

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南的標記 AWS FIS 資源

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考TagResource中的。

下列程式碼範例會示範如何使用untag-resource

AWS CLI

若要取消標記資源

下列untag-resource範例會從指定的資源移除標籤。

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

此命令不會產生輸出。

如需詳細資訊,請參閱AWS 故障注入模擬器使用指南的標記 AWS FIS 資源

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考UntagResource中的。

下列程式碼範例會示範如何使用update-experiment-template

AWS CLI

更新實驗樣板的步驟

下列update-experiment-template範例會更新指定實驗範本的描述。

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

輸出:

{ "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" } } }

有關更多信息,請參閱AWS 故障注入模擬器用戶指南中的更新實驗模板