aws:executeScript – 執行指令碼 - AWS Systems Manager

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

aws:executeScript – 執行指令碼

執行使用指定的執行時間和處理程序提供的 Python 或 PowerShell 指令碼。每個 aws:executeScript 動作最久可執行 600 秒 (10 分鐘)。您可以透過指定 aws:executeScript 步驟的 timeoutSeconds 參數來限制逾時。

在函數中使用傳回陳述式將輸出加入輸出承載。有關 aws:executeScript 動作定義輸出的範例,請參閱 範例 2:指令碼式 Runbook。您也可以把 Runbook 的 aws:executeScript 動作輸出傳送到您指定的 Amazon CloudWatch Logs 日誌群組。如需更多詳細資訊,請參閱 使用 CloudWatch Logs 記錄自動化動作輸出

如果想要將 aws:executeScript 動作的輸出傳送至 CloudWatch Logs,或者如果為 aws:executeScript 動作指定的指令碼叫用 AWS API 操作,則始終需要 AWS Identity and Access Management (IAM) 服務角色 (或擔任角色) 執行 Runbook。

aws:executeScript 動作包含下列預先安裝的 PowerShell Core 模組。

  • Microsoft.PowerShell.Host

  • Microsoft.PowerShell.Management

  • Microsoft.PowerShell.Security

  • Microsoft.PowerShell.Utility

  • PackageManagement

  • PowerShellGet

若要使用未預先安裝的 PowerShell Core 模組,您的指令碼必須使用 -Force 旗標安裝模組,如下列命令所示。不支援 AWSPowerShell.NetCore 模組。把 ModuleName (模組名稱) 取代為您想要安裝的模組。

Install-Module ModuleName -Force

若要在指令碼中使用 PowerShell Core Cmdlet,建議您使用 AWS.Tools 模組,如下列命令所示。將每個範例資源預留位置取代為您自己的資訊。

  • Amazon Simple Storage Service (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
  • 通用或與服務無關的 AWS Tools for Windows PowerShell Cmdlet。

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

如果指令碼除了使用 PowerShell Core 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

如需在 Runbook 內容中安裝和匯入 AWS.Tools 模組,以及使用 PowerShell Core Cmdlet 的範例,請參閱 使用文件建置器建立執行手冊

Input

提供執行指令碼所需訊息。將每個範例資源預留位置取代為您自己的資訊。

注意

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
指令碼

您想要在自動化期間執行的內嵌指令碼。

類型:字串

必要:No (Python) | Yes (PowerShell)

連接

動作可呼叫的獨立指令碼檔案或 .zip 檔案的名稱。指定與您在 Attachments 請求參數中指定的文件附件檔案的 Name 相同的數值。如須詳細資訊,請參閱《AWS Systems Manager API 參考》的附件。如果您使用附件提供指令碼,還必須定義 Runbook 頂層元素的 files 區段。如需更多詳細資訊,請參閱 結構描述版本 0.3

若要為 Python 呼叫檔案,請在 Handler 中使用 filename.method_name 格式。

注意

Python 指令碼的附件可以是包含指令碼的 .py 檔案或 .zip 檔案。PowerShell 指令碼必須儲存在 .zip 檔案中。

當在附件中包含 Python 庫時,建議在每個模組目錄中新增空的 __init__.py 檔案。這允許您從指令碼內容中的附件庫匯入模組。例如:from library import module

類型:字串

必要:否

輸出
承載

由函數傳回之物件的 JSON 表示法。最多會傳回 100 KB。如果輸出清單,則最多只能傳回 100 個項目。