本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Systems Manager Automation 操作参考
此参考介绍可在 AWS Systems Manager Automation 运行手册中指定的 Automation 操作。这些操作不能用于其他类型的 SSM 文档。有关其他 SSM 文档类型插件的信息,请参阅 Systems Manager 命令文档插件参考.
Systems Manager Automation 运行在 Automation 运行手册中定义的步骤。每步都与特定操作相关。此操作确定本步的输入、行为和输出。步骤在运行手册的
mainSteps
部分中定义。
目前,macOS 的 EC2 实例仅支持以下插件:
-
aws:configurePackage
-
aws:refreshAssociation
-
aws:runShellScript
-
aws:softwareInventory
您无需指定操作或步骤的输出。由与本步关联的操作预先确定输出。当您在运行手册中指定步骤输入时,可以引用先前步骤中的一个或多个输出。例如,您可以使 aws:runInstances
的输出可用于后续的 aws:runCommand
操作。您还可以在运行手册的 Output
部分中引用前面步骤中的输出。
如果您运行使用 AWS Identity and Access Management (IAM) 服务角色调用其他服务的自动化工作流程,请注意必须使用权限将该服务角色配置为调用这些服务。此要求适用于所有
AWS Automation 文档(AWS-*
文档),例如 AWS-ConfigureS3BucketLogging
、AWS-CreateDynamoDBBackup
和 AWS-RestartEC2Instance
文档。对于您创建的所有自定义自动化文档,如果这些文档使用调用其他服务的操作调用其他 AWS 服务,此要求同样适用。例如,如果您使用 aws:executeAwsApi
、aws:createStack
或 aws:copyImage
操作,则必须配置具有权限的服务角色来调用这些服务。您可以通过将 IAM 内联策略添加到角色来启用其他 AWS 服务的权限。有关更多信息,请参阅(可选)添加 Automation 内联策略来调用其他 AWS 服务。
主题
- 所有操作共享的属性
- aws:approve – 暂停自动化以进行手动批准
- aws:assertAwsResourceProperty – 声明 AWS 资源状态或事件状态
- aws:branch – 运行条件自动化步骤
- aws:changeInstanceState – 更改或声明实例状态
- aws:copyImage – 复制或加密 Amazon 系统映像
- aws:createImage – 创建 Amazon 系统映像
- aws:createStack – 创建 AWS CloudFormation 堆栈
- aws:createTags – 为 AWS 资源创建标签
- aws:deleteImage – 删除 Amazon 系统映像
- aws:deleteStack – 删除 AWS CloudFormation 堆栈
- aws:executeAutomation – 运行另一个自动化
- aws:executeAwsApi – 调用并运行 AWS API 操作
- aws:executeScript – 运行脚本
- aws:executeStateMachine – 运行 AWS Step Functions 状态机
- aws:invokeLambdaFunction – 调用 AWS Lambda 函数
- aws:pause – 暂停自动化
- aws:runCommand – 在托管实例上运行命令
- aws:runInstances – 启动 EC2 实例
- aws:sleep – 延迟自动化
- aws:waitForAwsResourceProperty – 等待 AWS 资源属性
- Automation 系统变量
所有操作共享的属性
通用属性是位于所有操作中的参数或选项。一些选项定义步骤的行为,例如,等待步骤完成的时间以及步骤失败时要做什么。以下属性是所有操作的通用属性。
- name
-
在运行手册中的所有步骤名称中必须是唯一的标识符。
类型:字符串
必需:是
- action
-
步骤要运行的操作的名称。aws:runCommand – 在托管实例上运行命令 是可在此处指定的操作的示例。本文档提供有关所有可用操作的详细信息。
类型:字符串
必需:是
- maxAttempts
-
在发生故障的情况下应重试步骤的次数。如果值大于 1,则直到所有重试尝试失败后,才会将此步骤视为失败。默认值是 1。
类型:整数
必需:否
- timeoutSeconds
-
步骤的超时值。如果超时并且
maxAttempts
的值大于 1,则本步未考虑超时,直至已尝试所有重试。类型:整数
必需:否
- onFailure
-
指示在失败时 Automation 是应中止、继续还是转到其他步骤。该选项的默认值为“中止”。
类型:字符串
有效值:Abort | Continue | step:
step_name
必需:否
- onCancel
-
指示在用户取消自动化时 Automation 应转至的步骤。Automation 最多运行两分钟的取消工作流程。
类型:字符串
有效值:Abort | step:
step_name
必需:否
属性不支持移动到以下操作:
onCancel
-
aws:approve
-
aws:copyImage
-
aws:createImage
-
aws:createStack
-
aws:createTags
-
aws:pause
-
aws:runInstances
-
aws:sleep
-
- isEnd
-
此选项在特定步骤结束时停止自动化。如果步骤失败或成功,自动化将停止。默认值为 False。
类型:布尔值
有效值:true | false
必需:否
- nextStep
-
指定在成功完成一个步骤后,接下来处理自动化中的哪个步骤。
类型:字符串
必需:否
- isCritical
-
将一个步骤指定为成功完成自动化的关键步骤。如果具有此分派的步骤失败,则自动化会将自动化的最终状态报告为失败。仅当您在步骤中明确定义此属性时,才会评估此属性。如果
onFailure
属性在某个步骤中设置为Continue
,则该值默认为 false。否则,此选项的默认值为 true。类型:布尔值
有效值:true | false
必需:否
- inputs
-
特定于操作的属性。
类型:映射
必需:是
Example
--- description: "Custom Automation Example" schemaVersion: '0.3' assumeRole: "{{ AutomationAssumeRole }}" parameters: AutomationAssumeRole: type: String description: "(Required) The ARN of the role that allows Automation to perform the actions on your behalf. If no role is specified, Systems Manager Automation uses your IAM permissions to run this runbook." default: '' InstanceId: type: String description: "(Required) The Instance Id whose root EBS volume you want to restore the latest Snapshot." default: '' mainSteps: - name: getInstanceDetails action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: DescribeInstances InstanceIds: - "{{ InstanceId }}" outputs: - Name: availabilityZone Selector: "$.Reservations[0].Instances[0].Placement.AvailabilityZone" Type: String - Name: rootDeviceName Selector: "$.Reservations[0].Instances[0].RootDeviceName" Type: String nextStep: getRootVolumeId - name: getRootVolumeId action: aws:executeAwsApi maxAttempts: 3 onFailure: Abort inputs: Service: ec2 Api: DescribeVolumes Filters: - Name: attachment.device Values: ["{{ getInstanceDetails.rootDeviceName }}"] - Name: attachment.instance-id Values: ["{{ InstanceId }}"] outputs: - Name: rootVolumeId Selector: "$.Volumes[0].VolumeId" Type: String nextStep: getSnapshotsByStartTime - name: getSnapshotsByStartTime action: aws:executeScript timeoutSeconds: 45 onFailure: Abort inputs: Runtime: python3.6 Handler: getSnapshotsByStartTime InputPayload: rootVolumeId : "{{ getRootVolumeId.rootVolumeId }}" Script: |- def getSnapshotsByStartTime(events,context): import boto3 #Initialize client ec2 = boto3.client('ec2') rootVolumeId = events['rootVolumeId'] snapshotsQuery = ec2.describe_snapshots( Filters=[ { "Name": "volume-id", "Values": [rootVolumeId] } ] ) if not snapshotsQuery['Snapshots']: noSnapshotFoundString = "NoSnapshotFound" return { 'noSnapshotFound' : noSnapshotFoundString } else: jsonSnapshots = snapshotsQuery['Snapshots'] sortedSnapshots = sorted(jsonSnapshots, key=lambda k: k['StartTime'], reverse=True) latestSortedSnapshotId = sortedSnapshots[0]['SnapshotId'] return { 'latestSnapshotId' : latestSortedSnapshotId } outputs: - Name: Payload Selector: $.Payload Type: StringMap - Name: latestSnapshotId Selector: $.Payload.latestSnapshotId Type: String - Name: noSnapshotFound Selector: $.Payload.noSnapshotFound Type: String nextStep: branchFromResults - name: branchFromResults action: aws:branch onFailure: Abort onCancel: step:startInstance inputs: Choices: - NextStep: createNewRootVolumeFromSnapshot Not: Variable: "{{ getSnapshotsByStartTime.noSnapshotFound }}" StringEquals: "NoSnapshotFound" isEnd: true - name: createNewRootVolumeFromSnapshot action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: CreateVolume AvailabilityZone: "{{ getInstanceDetails.availabilityZone }}" SnapshotId: "{{ getSnapshotsByStartTime.latestSnapshotId }}" outputs: - Name: newRootVolumeId Selector: "$.VolumeId" Type: String nextStep: stopInstance - name: stopInstance action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: StopInstances InstanceIds: - "{{ InstanceId }}" nextStep: verifyVolumeAvailability - name: verifyVolumeAvailability action: aws:waitForAwsResourceProperty timeoutSeconds: 120 inputs: Service: ec2 Api: DescribeVolumes VolumeIds: - "{{ createNewRootVolumeFromSnapshot.newRootVolumeId }}" PropertySelector: "$.Volumes[0].State" DesiredValues: - "available" nextStep: verifyInstanceStopped - name: verifyInstanceStopped action: aws:waitForAwsResourceProperty timeoutSeconds: 120 inputs: Service: ec2 Api: DescribeInstances InstanceIds: - "{{ InstanceId }}" PropertySelector: "$.Reservations[0].Instances[0].State.Name" DesiredValues: - "stopped" nextStep: detachRootVolume - name: detachRootVolume action: aws:executeAwsApi onFailure: Abort isCritical: true inputs: Service: ec2 Api: DetachVolume VolumeId: "{{ getRootVolumeId.rootVolumeId }}" nextStep: verifyRootVolumeDetached - name: verifyRootVolumeDetached action: aws:waitForAwsResourceProperty timeoutSeconds: 30 inputs: Service: ec2 Api: DescribeVolumes VolumeIds: - "{{ getRootVolumeId.rootVolumeId }}" PropertySelector: "$.Volumes[0].State" DesiredValues: - "available" nextStep: attachNewRootVolume - name: attachNewRootVolume action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: AttachVolume Device: "{{ getInstanceDetails.rootDeviceName }}" InstanceId: "{{ InstanceId }}" VolumeId: "{{ createNewRootVolumeFromSnapshot.newRootVolumeId }}" nextStep: verifyNewRootVolumeAttached - name: verifyNewRootVolumeAttached action: aws:waitForAwsResourceProperty timeoutSeconds: 30 inputs: Service: ec2 Api: DescribeVolumes VolumeIds: - "{{ createNewRootVolumeFromSnapshot.newRootVolumeId }}" PropertySelector: "$.Volumes[0].Attachments[0].State" DesiredValues: - "attached" nextStep: startInstance - name: startInstance action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: StartInstances InstanceIds: - "{{ InstanceId }}"