文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS FIS 使用 的範例 AWS CLI
下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 來執行動作和實作常見案例 AWS FIS。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
主題
動作
下列程式碼範例示範如何使用 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 故障注入模擬器使用者指南中的建立實驗範本。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateExperimentTemplate
。
-
下列程式碼範例示範如何使用 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 故障注入模擬器使用者指南中的刪除實驗範本。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteExperimentTemplate
。
-
下列程式碼範例示範如何使用 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 故障注入模擬器使用者指南中的實驗範本。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetExperimentTemplate
。
-
下列程式碼範例示範如何使用 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 Fault Injection Simulator 使用者指南中的 Experiments for 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 故障注入模擬器使用者指南中的實驗範本。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListExperimentTemplates
。
-
下列程式碼範例示範如何使用 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 資源。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListTagsForResource
。
-
下列程式碼範例示範如何使用 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 Fault Injection Simulator 使用者指南中的 Experiments for 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 Fault Injection Simulator 使用者指南中的 Experiments for 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
\ --tagskey1=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
\ ---descriptionmyExperimentTemplate
輸出:
{ "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 故障注入模擬器使用者指南中的更新實驗範本。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateExperimentTemplate
。
-