

# 使用 AWS CLI 的 AWS FIS 示例
<a name="cli_fis_code_examples"></a>

以下代码示例演示如何通过将 AWS Command Line Interface与 AWS FIS 结合使用，来执行操作和实现常见场景。

*操作是大型程序的代码摘录*，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**Topics**
+ [操作](#actions)

## 操作
<a name="actions"></a>

### `create-experiment-template`
<a name="fis_CreateExperimentTemplate_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[创建试验模板](https://docs.aws.amazon.com/fis/latest/userguide/working-with-templates.html#create-template)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [CreateExperimentTemplate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/create-experiment-template.html)。

### `delete-experiment-template`
<a name="fis_DeleteExperimentTemplate_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[删除试验模板](https://docs.aws.amazon.com/fis/latest/userguide/working-with-templates.html#delete-template)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DeleteExperimentTemplate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/delete-experiment-template.html)。

### `get-action`
<a name="fis_GetAction_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[操作](https://docs.aws.amazon.com/fis/latest/userguide/actions.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetAction](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/get-action.html)。

### `get-experiment-template`
<a name="fis_GetExperimentTemplate_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[试验模板](https://docs.aws.amazon.com/fis/latest/userguide/experiment-templates.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetExperimentTemplate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/get-experiment-template.html)。

### `get-experiment`
<a name="fis_GetExperiment_cli_topic"></a>

以下代码示例演示了如何使用 `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 用户指南》**中的 [AWS FIS 试验](https://docs.aws.amazon.com/fis/latest/userguide/experiments.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetExperiment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/get-experiment.html)。

### `list-actions`
<a name="fis_ListActions_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[操作](https://docs.aws.amazon.com/fis/latest/userguide/actions.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListActions](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/list-actions.html)。

### `list-experiment-templates`
<a name="fis_ListExperimentTemplates_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[试验模板](https://docs.aws.amazon.com/fis/latest/userguide/experiment-templates.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListExperimentTemplates](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/list-experiment-templates.html)。

### `list-experiments`
<a name="fis_ListExperiments_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[试验](https://docs.aws.amazon.com/fis/latest/userguide/experiments.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListExperiments](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/list-experiments.html)。

### `list-tags-for-resource`
<a name="fis_ListTagsForResource_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[标记 AWS FIS 资源](https://docs.aws.amazon.com/fis/latest/userguide/tagging.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/list-tags-for-resource.html)。

### `start-experiment`
<a name="fis_StartExperiment_cli_topic"></a>

以下代码示例演示了如何使用 `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 用户指南》**中的 [AWS FIS 试验](https://docs.aws.amazon.com/fis/latest/userguide/experiments.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考**》中的 [StartExperiment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/start-experiment.html)。

### `stop-experiment`
<a name="fis_StopExperiment_cli_topic"></a>

以下代码示例演示了如何使用 `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 用户指南》**中的 [AWS FIS 试验](https://docs.aws.amazon.com/fis/latest/userguide/experiments.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [StopExperiment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/stop-experiment.html)。

### `tag-resource`
<a name="fis_TagResource_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[标记 AWS FIS 资源](https://docs.aws.amazon.com/fis/latest/userguide/tagging.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/tag-resource.html)。

### `untag-resource`
<a name="fis_UntagResource_cli_topic"></a>

以下代码示例演示了如何使用 `untag-resource`。

**AWS CLI**  
**取消标记资源**  
以下 `untag-resource` 示例从指定资源中删除标签。  

```
aws fis untag-resource \
    --resource-arn arn:aws:fis:us-west-2:123456789012:experiment/ABC12DeFGhI3jKLMNOP
```
此命令不生成任何输出。  
有关更多信息，请参阅《AWS Fault Injection Simulator 用户指南》**中的[标记 AWS FIS 资源](https://docs.aws.amazon.com/fis/latest/userguide/tagging.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/untag-resource.html)。

### `update-experiment-template`
<a name="fis_UpdateExperimentTemplate_cli_topic"></a>

以下代码示例演示了如何使用 `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 Fault Injection Simulator 用户指南》**中的[更新试验模板](https://docs.aws.amazon.com/fis/latest/userguide/working-with-templates.html#update-template)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateExperimentTemplate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/fis/update-experiment-template.html)。