aws:executeScript - 运行脚本 - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

aws:executeScript - 运行脚本

使用指定的运行时和处理程序提供的 Python 或 PowerShell 脚本。每个 aws:executeScript 操作最多可以运行 600 秒(10 分钟)时间。您可以通过指定 aws:executeScript 步骤的 timeoutSeconds 参数来限制超时。

在函数中使用 return 语句将输出添加到输出有效负载中。有关为您的 aws:executeScript 操作定义输出的示例,请参阅示例 2:脚本化运行手册。您还可以将运行手册中 aws:executeScript 操作的输出发送到您指定的 Amazon CloudWatch Logs 日志组。有关更多信息,请参阅使用 CloudWatch Logs 记录自动化操作输出

如果想要将 aws:executeScript 操作的输出发送到 CloudWatch Logs,或者如果您为 aws:executeScript 操作指定的脚本调用 Amazon API 操作,则始终需要 Amazon Identity and Access Management (IAM) 服务角色(或承担角色)运行运行手册。

aws:executeScript 操作包含以下预安装的 PowerShell 核心模块。

  • Microsoft.PowerShell.Host

  • Microsoft.PowerShell.Management

  • Microsoft.PowerShell.Security

  • Microsoft.PowerShell.Utility

  • PackageManagement

  • PowerShellGet

要使用未预装的 PowerShell 核心模块,脚本必须安装带有 -Force 标志的模块,如以下命令所示。不支持 AWSPowerShell.NetCore 模块。用您想要安装的模块替换 ModuleName

Install-Module ModuleName -Force

要在脚本中使用 PowerShell 核心 cmdlet,我们建议您使用 AWS.Tools 模块,如以下命令所示。将每个示例资源占位符替换为您自己的信息。

  • Amazon S3 cmdlet。

    Install-Module AWS.Tools.S3 -Force Get-S3Bucket -BucketName bucketname
  • Amazon EC2 cmdlet

    Install-Module AWS.Tools.EC2 -Force Get-EC2InstanceStatus -InstanceId instanceId
  • 通用或独立于服务的 Amazon Tools for Windows PowerShell cmdlet。

    Install-Module AWS.Tools.Common -Force Get-AWSRegion

如果脚本除了使用 PowerShell 核心 cmdlet 之外还初始化新对象,则还必须导入模块,如以下命令所示。

Install-Module AWS.Tools.EC2 -Force Import-Module AWS.Tools.EC2 $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "Tag" $tag.Value = "TagValue" New-EC2Tag -Resource i-02573cafcfEXAMPLE -Tag $tag

有关安装和导入 AWS.Tools 模块以及在运行手册中使用 PowerShell 核心 cmdlet 的示例,请参阅 正在使用文档生成器创建运行手册

输入

提供运行脚本所需的信息。将每个示例资源占位符替换为您自己的信息。

注意

Python 脚本的附件可以是 .py 文件或包含该脚本的 .zip 文件。PowerShell 脚本必须存储在 .zip 文件中。

YAML
action: "aws:executeScript" inputs: Runtime: runtime Handler: "functionName" InputPayload: scriptInput: '{{parameterValue}}' Script: |- def functionName(events, context): ... Attachment: "scriptAttachment.zip"
JSON
{ "action": "aws:executeScript", "inputs": { "Runtime": "runtime", "Handler": "functionName", "InputPayload": { "scriptInput": "{{parameterValue}}" }, "Attachment": "scriptAttachment.zip" } }
运行时系统

用于运行提供的脚本的运行时系统语言。aws:executeScript 支持 Python 3.7(python3.7)、Python 3.8(python3.8)、PowerShell Core 6.0 (dotnetcore2.1)和 PowerShell 7.0(dotnetcore3.1)脚本。

支持的值:python3.7 | python3.8 | PowerShell Core 6.0 | PowerShell 7.0

类型:字符串

必需:是

处理程序

函数的名称。您必须确保在处理程序中定义的函数具有两个参数:eventscontext。PowerShell 运行时不支持此参数。

类型:字符串

必需:是(Python)| 不支持(PowerShell)

InputPayload

将传递给处理程序的第一个参数的 JSON 或 YAML 对象。这可用于将输入数据传递给脚本。

类型:字符串

必需:否

Python
description: Tag an instance schemaVersion: '0.3' assumeRole: '{{AutomationAssumeRole}}' parameters: AutomationAssumeRole: type: String description: '(Required) The Amazon Resource Name (ARN) of the IAM role that allows Automation to perform the actions on your behalf. If no role is specified, Systems Manager Automation uses your IAM permissions to operate this runbook.' InstanceId: type: String description: (Required) The ID of the EC2 instance you want to tag. mainSteps: - name: tagInstance action: 'aws:executeScript' inputs: Runtime: "python3.8" Handler: tagInstance InputPayload: instanceId: '{{InstanceId}}' Script: |- def tagInstance(events,context): import boto3 #Initialize client ec2 = boto3.client('ec2') instanceId = events['instanceId'] tag = { "Key": "Env", "Value": "Example" } ec2.create_tags( Resources=[instanceId], Tags=[tag] )
PowerShell
description: Tag an instance schemaVersion: '0.3' assumeRole: '{{AutomationAssumeRole}}' parameters: AutomationAssumeRole: type: String description: '(Required) The Amazon Resource Name (ARN) of the IAM role that allows Automation to perform the actions on your behalf. If no role is specified, Systems Manager Automation uses your IAM permissions to operate this runbook.' InstanceId: type: String description: (Required) The ID of the EC2 instance you want to tag. mainSteps: - name: tagInstance action: 'aws:executeScript' inputs: Runtime: PowerShell 7.0 InputPayload: instanceId: '{{InstanceId}}' Script: |- Install-Module AWS.Tools.EC2 -Force Import-Module AWS.Tools.EC2 $input = $env:InputPayload | ConvertFrom-Json $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "Env" $tag.Value = "Example" New-EC2Tag -Resource $input.instanceId -Tag $tag
Script

要在自动化期间运行的嵌入式脚本。

类型:字符串

必需:否 (Python) | 是 (PowerShell)

Attachment

可以由操作调用的单独脚本文件或 .zip 文件的名称。指定与您在 Attachments 请求参数中指定的文档附件文件的 Name 相同的值。有关更多信息,请参阅 Amazon Systems Manager API 参考中的附件。如果您使用附件提供脚本,还必须在您的运行手册的顶级元素部分中定义一个 files 部分。有关更多信息,请参阅架构版本 0.3

要为 Python 调用文件,请在 Handler 中使用 filename.method_name 格式。

注意

Python 脚本的附件可以是 .py 文件或包含该脚本的 .zip 文件。PowerShell 脚本必须存储在 .zip 文件中。

当在附件中包含 Python 库时,我们建议在每个模块目录中添加一个空__init__.py 文件。这允许您从脚本内容的附件中的库导入模块。例如:from library import module

类型:字符串

必需:否

输出
有效负载

函数返回的对象的 JSON 表示形式。返回最多 100KB 的数据。如果输出列表,则最多返回 100 个项目。