AWS FIS 實驗範例範本 - AWS 故障注入服務

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

AWS FIS 實驗範例範本

如果您使用 AWS FIS API 或命令列工具建立實驗範本,您可以在 JavaScript 物件標記法 (JSON) 中建構範本。若要取得有關實驗範本元件的更多資訊,請參閱〈〉範本元件

若要使用其中一個範例範本建立實驗,請將其儲存至 JSON 檔案 (例如my-template.json),將預留位置值以斜體取代為您自己的值,然後執行下列建實驗範本命令。

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

根據篩選器停止 EC2 執行個體

下列範例會使用指定 VPC 中的指定標籤停止指定區域中所有執行中的 Amazon EC2 執行個體。它在兩分鐘後重新啟動它們。

{ "tags": { "Name": "StopEC2InstancesWithFilters" }, "description": "Stop and restart all instances in us-east-1b with the tag env=prod in the specified VPC", "targets": { "myInstances": { "resourceType": "aws:ec2:instance", "resourceTags": { "env": "prod" }, "filters": [ { "path": "Placement.AvailabilityZone", "values": ["us-east-1b"] }, { "path": "State.Name", "values": ["running"] }, { "path": "VpcId", "values": [ "vpc-aabbcc11223344556"] } ], "selectionMode": "ALL" } }, "actions": { "StopInstances": { "actionId": "aws:ec2:stop-instances", "description": "stop the instances", "parameters": { "startInstancesAfterDuration": "PT2M" }, "targets": { "Instances": "myInstances" } } }, "stopConditions": [ { "source": "aws:cloudwatch:alarm", "value": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:alarm-name" } ], "roleArn": "arn:aws:iam::111122223333:role/role-name" }

停止指定數量的 EC2 執行個體

下列範例會停止具有指定標籤的三個執行個體。 AWS FIS 會選取要隨機停止的特定執行個體。會在兩分鐘後重新啟動這些執行個體。

{ "tags": { "Name": "StopEC2InstancesByCount" }, "description": "Stop and restart three instances with the specified tag", "targets": { "myInstances": { "resourceType": "aws:ec2:instance", "resourceTags": { "env": "prod" }, "selectionMode": "COUNT(3)" } }, "actions": { "StopInstances": { "actionId": "aws:ec2:stop-instances", "description": "stop the instances", "parameters": { "startInstancesAfterDuration": "PT2M" }, "targets": { "Instances": "myInstances" } } }, "stopConditions": [ { "source": "aws:cloudwatch:alarm", "value": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:alarm-name" } ], "roleArn": "arn:aws:iam::111122223333:role/role-name" }

執行預先設定的 AWS FIS SSM 文件

下列範例會使用預先設定的 AWS FIS SSM 文件-執行 CPU 壓力,在指定的 EC2 執行個體上執行 60 秒的 CPU 故障注入。AWSFIS AWS FIS 會監控實驗兩分鐘。

{ "tags": { "Name": "CPUStress" }, "description": "Run a CPU fault injection on the specified instance", "targets": { "myInstance": { "resourceType": "aws:ec2:instance", "resourceArns": ["arn:aws:ec2:us-east-1:111122223333:instance/instance-id"], "selectionMode": "ALL" } }, "actions": { "CPUStress": { "actionId": "aws:ssm:send-command", "description": "run cpu stress using ssm", "parameters": { "duration": "PT2M", "documentArn": "arn:aws:ssm:us-east-1::document/AWSFIS-Run-CPU-Stress", "documentParameters": "{\"DurationSeconds\": \"60\", \"InstallDependencies\": \"True\", \"CPU\": \"0\"}" }, "targets": { "Instances": "myInstance" } } }, "stopConditions": [ { "source": "aws:cloudwatch:alarm", "value": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:alarm-name" } ], "roleArn": "arn:aws:iam::111122223333:role/role-name" }

執行預先定義的自動化手冊

下列範例會使用 AWS 發佈通知 Systems Manager 所提供的工作流程簿,將通知發佈至 Amazon SNS。角色必須具有將通知發佈至指定 SNS 主題的權限。

{ "description": "Publish event through SNS", "stopConditions": [ { "source": "none" } ], "targets": { }, "actions": { "sendToSns": { "actionId": "aws:ssm:start-automation-execution", "description": "Publish message to SNS", "parameters": { "documentArn": "arn:aws:ssm:us-east-1::document/AWS-PublishSNSNotification", "documentParameters": "{\"Message\": \"Hello, world\", \"TopicArn\": \"arn:aws:sns:us-east-1:111122223333:topic-name\"}", "maxDuration": "PT1M" }, "targets": { } } }, "roleArn": "arn:aws:iam::111122223333:role/role-name" }

使用目標 IAM 角色調節 EC2 執行個體上的 API 動作

下列範例會限制動作定義中針對目標定義中指定的 IAM 角色進行的 API 呼叫所指定的 100% API 呼叫。

注意

如果您想要鎖定屬於 Auto Scaling 群組成員的 EC2 執行個體,請使用 aw:ec2: asg-不足-執行個體容量錯誤動作,並改為由 Auto Scaling 群組鎖定目標。如需詳細資訊,請參閱

{ "tags": { "Name": "ThrottleEC2APIActions" }, "description": "Throttle the specified EC2 API actions on the specified IAM role", "targets": { "myRole": { "resourceType": "aws:iam:role", "resourceArns": ["arn:aws:iam::111122223333:role/role-name"], "selectionMode": "ALL" } }, "actions": { "ThrottleAPI": { "actionId": "aws:fis:inject-api-throttle-error", "description": "Throttle APIs for 5 minutes", "parameters": { "service": "ec2", "operations": "DescribeInstances,DescribeVolumes", "percentage": "100", "duration": "PT2M" }, "targets": { "Roles": "myRole" } } }, "stopConditions": [ { "source": "aws:cloudwatch:alarm", "value": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:alarm-name" } ], "roleArn": "arn:aws:iam::111122223333:role/role-name" }

Kubernetes 叢集中網繭的壓力測試 CPU

下列範例使用混沌網格對 Amazon EKS Kubernetes 叢集中網繭的 CPU 進行 stress 測試一分鐘。

{ "description": "ChaosMesh StressChaos example", "targets": { "Cluster-Target-1": { "resourceType": "aws:eks:cluster", "resourceArns": [ "arn:aws:eks:arn:aws::111122223333:cluster/cluster-id" ], "selectionMode": "ALL" } }, "actions": { "TestCPUStress": { "actionId": "aws:eks:inject-kubernetes-custom-resource", "parameters": { "maxDuration": "PT2M", "kubernetesApiVersion": "chaos-mesh.org/v1alpha1", "kubernetesKind": "StressChaos", "kubernetesNamespace": "default", "kubernetesSpec": "{\"selector\":{\"namespaces\":[\"default\"],\"labelSelectors\":{\"run\":\"nginx\"}},\"mode\":\"all\",\"stressors\": {\"cpu\":{\"workers\":1,\"load\":50}},\"duration\":\"1m\"}" }, "targets": { "Cluster": "Cluster-Target-1" } } }, "stopConditions": [{ "source": "none" }], "roleArn": "arn:aws:iam::111122223333:role/role-name", "tags": {} }

下列範例使用石蕊對 Amazon EKS Kubernetes 叢集中網繭的 CPU 進行 stress 測試一分鐘。

{ "description": "Litmus CPU Hog", "targets": { "MyCluster": { "resourceType": "aws:eks:cluster", "resourceArns": [ "arn:aws:eks:arn:aws::111122223333:cluster/cluster-id" ], "selectionMode": "ALL" } }, "actions": { "MyAction": { "actionId": "aws:eks:inject-kubernetes-custom-resource", "parameters": { "maxDuration": "PT2M", "kubernetesApiVersion": "litmuschaos.io/v1alpha1", "kubernetesKind": "ChaosEngine", "kubernetesNamespace": "litmus", "kubernetesSpec": "{\"engineState\":\"active\",\"appinfo\":{\"appns\":\"default\",\"applabel\":\"run=nginx\",\"appkind\":\"deployment\"},\"chaosServiceAccount\":\"litmus-admin\",\"experiments\":[{\"name\":\"pod-cpu-hog\",\"spec\":{\"components\":{\"env\":[{\"name\":\"TOTAL_CHAOS_DURATION\",\"value\":\"60\"},{\"name\":\"CPU_CORES\",\"value\":\"1\"},{\"name\":\"PODS_AFFECTED_PERC\",\"value\":\"100\"},{\"name\":\"CONTAINER_RUNTIME\",\"value\":\"docker\"},{\"name\":\"SOCKET_PATH\",\"value\":\"/var/run/docker.sock\"}]},\"probe\":[]}}],\"annotationCheck\":\"false\"}" }, "targets": { "Cluster": "MyCluster" } } }, "stopConditions": [{ "source": "none" }], "roleArn": "arn:aws:iam::111122223333:role/role-name", "tags": {} }