本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
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 操作,則執行 Runbook 時一律需要 AWS Identity and Access Management (IAM) 服務角色 (或擔任角色)。
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
amzn-s3-demo-bucket
-
Amazon EC2 cmdlet。
Install-Module AWS.Tools.EC2 -Force Get-EC2InstanceStatus -InstanceId
instance-id
-
常見 或服務獨立 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 的範例,請參閱 Automation 執行手冊的視覺化設計體驗。
輸入
提供執行指令碼所需訊息。將每個範例資源預留位置
取代為您自己的資訊。
注意
Python 指令碼的附件可以是包含指令碼的 .py 檔案或 .zip 檔案。PowerShell 指令碼必須儲存在 .zip 檔案中。
- 執行期
-
用於執行所提供指令碼的執行時期語言。
aws:executeScript
支援 Python 3.7 (python3.7)、Python 3.8 (python3.8)、Python 3.9 (python3.9)、Python 3.10 (python3.10)、Python 3.11 (python3.11)、PowerShell Core 6.0 (dotnetcore2.1) 與 PowerShell 7.0 (dotnetcore3.1) 指令碼。支援的值:
python3.7
|python3.8
|python3.9
|python3.10
|python3.11
|PowerShell Core 6.0
|PowerShell 7.0
類型:字串
必要:是
注意
對於 python 執行時間,環境提供 512MB 的記憶體和 512MB 的磁碟空間。對於 PowerShell 執行期,環境提供 1024MB 的記憶體和 512MB 的磁碟空間。
- 處理常式
-
函數名稱。您必須確保處理常式中定義的函數有兩個參數,
events
和context
。PowerShell 執行階段不支援此參數。類型:字串
必要:是 (Python) | 不支援 (PowerShell)
- InputPayload
-
將傳遞給處理程序的第一個參數的 JSON 或 YAML 物件。這可以用來將輸入資料傳入至指令碼。
類型:字串
必要:否
- 指令碼
-
您想要在自動化期間執行的內嵌指令碼。
類型:字串
必要: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 個項目。