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

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

aws:executeScript – 執行指令碼

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

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

如果您想要將aws:executeScript動作的輸出傳送至 CloudWatch 記錄檔,或者您為aws:executeScript動作指定的指令碼呼叫 AWS API 作業,則執行 Runbook 始終需要 AWS Identity and Access Management (IAM) 服務角色 (或假設角色)。

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

  • Microsoft。 PowerShell. 主機。

  • Microsoft。 PowerShell. 管理。

  • Microsoft。 PowerShell. 安全。

  • Microsoft。 PowerShell. 公用程式

  • PackageManagement

  • PowerShellGet

若要使用未預先安裝的 PowerShell 核心模組,您的指令碼必須使用-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 指令程式。

    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

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

輸入

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

注意

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),Python 3.9(python3.9)蟒 3.10(Python 3.10),Python 3.11(蟒蛇 3.11)核心 6.0(點網核 2.1)和 7.0(多網核 3.1)腳本。 PowerShell PowerShell

支援的值:python3.7python3.8python3.9python3.10| python3.11 | 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
指令碼

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

類型:字串

必要:否 (Python) | 是 (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 個項目。