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
有关安装和导入
AWS.Tools
模块以及在运行手册中使用 PowerShell Core cmdlet 的示例,请参阅
正在使用文档生成器创建运行手册
。
提供运行脚本所需的信息。将每个
示例资源占位符
替换为您自己的信息。
Python 脚本的附件可以是 .py 文件或包含该脚本的 .zip 文件。 PowerShell 脚本必须存储在 .zip 文件中。
action: "aws:executeScript"
inputs:
Runtime: runtime
Handler: "functionName
"
InputPayload:
scriptInput
: '{{parameterValue
}}'
Script: |-
def functionName
(events, context):
Attachment: "scriptAttachment
.zip"
"action": "aws:executeScript",
"inputs":
{
"Runtime": "
runtime
",
"Handler": "
functionName
",
"InputPayload":
{
"
scriptInput
": "
{
{
parameterValue
}}"
"Attachment": "
scriptAttachment
.zip"
用于运行提供的脚本。
aws:executeScript
支持 Python 3.6 (python3.6)、Python 3.7 (python3.7)、 PowerShell Python 3.6 (python3.6) 和 PowerShell 7.0 (dotnetcore3.1) 脚本。
支持的值:
python3.6
|
python3.7
|
python3.8
|
PowerShell Core 6.0
|
PowerShell
类型:字符串
函数的名称。您必须确保在处理程序中定义的函数具有两个参数:
events
和
context
。 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
要在自动化期间运行的嵌入式脚本。JSON 运行手册不支持此参数。JSON 运行手册必须使用 Attachment
输入参数提供脚本内容。
类型:字符串
必填项:否 (Python) | 是 (PowerShell)
- Attachment
可以由操作调用的单独脚本文件或 .zip 文件的名称。指定与您在 Attachments
请求参数中指定的文档附件文件的 Name
相同的值。有关更多信息,请参阅 AWS Systems Manager API 参考中的附件。如果您使用附件提供脚本,还必须在您的运行手册的顶级元素部分中定义一个 files
部分。有关更多信息,请参阅架构版本 0.3:
要为 Python 调用文件,请在 Handler
中使用 filename.method_name
格式。
Python 脚本的附件可以是 .py 文件或包含该脚本的 .zip 文件。 PowerShell 脚本必须存储在 .zip 文件中。
当在附件中包含 Python 库时,我们建议在每个模块目录中添加一个空__init__.py
文件。这允许您从脚本内容的附件中的库导入模块。例如:from library import
module
。
类型:字符串
Output