通过使用 Run Command 命令使用参数 - AWS Systems Manager

通过使用 Run Command 命令使用参数

您可以在 Run Command(AWS Systems Manager 的一项功能)中使用参数。有关更多信息,请参阅 AWS Systems Manager Run Command

运行 String 参数(控制台)

以下过程将指导您完成运行使用 String 参数的命令的过程。

要使用 Parameter Store 运行 String 参数,请执行以下步骤:
  1. 访问 https://console.aws.amazon.com/systems-manager/,打开 AWS Systems Manager 控制台。

  2. 在导航窗格中,选择 Run Command

  3. 选择 Run command(运行命令)

  4. Command document 列表中,请选择 AWS-RunPowerShellScript (Windows) 或 AWS-RunShellScript (Linux)。

  5. 对于命令参数,请输入 echo {{ssm:parameter-name}}。例如:echo {{ssm:/Test/helloWorld}}

  6. Targets(目标)部分中,通过指定标签、手动选择实例或边缘设备或指定资源组,选择要在其上运行此操作的托管式节点。

    提示

    如果未列出您希望看到的托管式节点,请参阅 排除托管式节点可用性的问题 以获取故障排除技巧。

  7. 对于 Other parameters(其他参数)

    • 对于 Comment(注释),请输入有关此命令的信息。

    • 对于 Timeout (seconds) (超时 (秒)),请指定在整个命令执行失败之前系统等待的秒数。

  8. 对于 Rate control(速率控制)

    • 对于 Concurrency(并发),请指定要同时运行该命令的托管式节点的数量或百分比。

      注意

      如果您通过指定应用于托管式节点的标签或指定 AWS Resource Groups 来选择目标,但不确定有多少个托管式节点已被设为目标,则可通过指定百分比来限制可同时运行该文档的目标的数量。

    • 对于 Error threshold(错误阈值),请指定当命令在一定数量或百分比的节点上失败后,何时在其他托管式节点上停止运行该命令。例如,如果您指定三个错误,Systems Manager 将在收到第四个错误时停止发送该命令。仍在处理该命令的托管式节点也可能发送错误。

  9. (可选)对于 输出选项,要将命令输出保存到文件,请选中 将命令输出写入 S3 存储桶 框。在输入框中输入存储桶和前缀(文件夹)名称。

    注意

    授予将数据写入 S3 存储桶的能力的 S3 权限,是分配给实例的实例配置文件(适用于 EC2 实例)或 IAM 服务角色(混合激活的计算机)的权限,而不是执行此任务的 IAM 用户的权限。有关更多信息,请参阅配置 Systems Manager 所需的实例权限为混合环境创建 IAM 服务角色。此外,如果指定的 S3 存储桶位于不同的 AWS 账户 中,请确保与该托管式节点关联的实例配置文件或 IAM 服务角色具有写入该存储桶的所需权限。

  10. SNS 通知部分,如果需要发送有关命令执行状态的通知,请选中 Enable SNS notifications(启用 SNS 通知)复选框。

    有关为 Run Command 配置 Amazon SNS 通知的更多信息,请参阅 使用 Amazon SNS 通知监控 Systems Manager 状态更改

  11. 选择 Run(运行)

  12. Command ID(命令 ID)页面上的 Targets and outputs(目标和输出)区域中,选择在其中运行命令的节点 ID 旁边的按钮,然后选择 View output(查看输出)。验证命令输出是否是您为参数提供的值,例如 This is my first parameter

运行参数 (AWS CLI)

示例 1:简单命令

以下示例命令包含一个名为 DNS-IP 的 Systems Manager 参数。此参数的值即为节点的 IP 地址。此示例使用 AWS Command Line Interface (AWS CLI) 命令重复参数值。

Linux & macOS
aws ssm send-command \ --document-name "AWS-RunShellScript" \ --document-version "1" \ --targets "Key=instanceids,Values=i-02573cafcfEXAMPLE" \ --parameters "commands='echo {{ssm:DNS-IP}}'" \ --timeout-seconds 600 \ --max-concurrency "50" \ --max-errors "0" \ --region us-east-2
Windows
aws ssm send-command ^ --document-name "AWS-RunPowerShellScript" ^ --document-version "1" ^ --targets "Key=instanceids,Values=i-02573cafcfEXAMPLE" ^ --parameters "commands='echo {{ssm:DNS-IP}}'" ^ --timeout-seconds 600 ^ --max-concurrency "50" ^ --max-errors "0" ^ --region us-east-2

此命令会返回如下信息。

{
    "Command": {
        "CommandId": "c70a4671-8098-42da-b885-89716EXAMPLE",
        "DocumentName": "AWS-RunShellScript",
        "DocumentVersion": "1",
        "Comment": "",
        "ExpiresAfter": "2023-12-26T15:19:17.771000-05:00",
        "Parameters": {
            "commands": [
                "echo {{ssm:DNS-IP}}"
            ]
        },
        "InstanceIds": [],
        "Targets": [
            {
                "Key": "instanceids",
                "Values": [
                    "i-02573cafcfEXAMPLE"
                ]
            }
        ],
        "RequestedDateTime": "2023-12-26T14:09:17.771000-05:00",
        "Status": "Pending",
        "StatusDetails": "Pending",
        "OutputS3Region": "us-east-2",
        "OutputS3BucketName": "",
        "OutputS3KeyPrefix": "",
        "MaxConcurrency": "50",
        "MaxErrors": "0",
        "TargetCount": 0,
        "CompletedCount": 0,
        "ErrorCount": 0,
        "DeliveryTimedOutCount": 0,
        "ServiceRole": "",
        "NotificationConfig": {
            "NotificationArn": "",
            "NotificationEvents": [],
            "NotificationType": ""
        },
        "CloudWatchOutputConfig": {
            "CloudWatchLogGroupName": "",
            "CloudWatchOutputEnabled": false
        },
        "TimeoutSeconds": 600,
        "AlarmConfiguration": {
            "IgnorePollAlarmFailure": false,
            "Alarms": []
        },
        "TriggeredAlarms": []
    }
}

命令执行完成后,您可以使用以下命令查看有关命令执行的更多信息:

示例 2:解密 SecureString 参数值

下一个示例命令使用名为 SecurePasswordSecureString 参数。在 parameters 中使用的命令可检索和解密 SecureString 参数的值,然后重置本地管理员密码,而无需以明文形式传递密码。

Linux
aws ssm send-command \ --document-name "AWS-RunShellScript" \ --document-version "1" \ --targets "Key=instanceids,Values=i-02573cafcfEXAMPLE" \ --parameters '{"commands":["secure=$(aws ssm get-parameters --names SecurePassword --with-decryption --query Parameters[0].Value --output text --region us-east-2)","echo $secure | passwd myuser --stdin"]}' \ --timeout-seconds 600 \ --max-concurrency "50" \ --max-errors "0" \ --region us-east-2
Windows
aws ssm send-command ^ --document-name "AWS-RunPowerShellScript" ^ --document-version "1" ^ --targets "Key=instanceids,Values=i-02573cafcfEXAMPLE" ^ --parameters "commands=['$secure = (Get-SSMParameterValue -Names SecurePassword -WithDecryption $True).Parameters[0].Value','net user administrator $secure']" ^ --timeout-seconds 600 ^ --max-concurrency "50" ^ --max-errors "0" ^ --region us-east-2

示例 3:在 SSM 文档中引用参数

您还可以在 SSM 文档的 Parameters 部分引用 Systems Manager 参数,如以下示例所示。

{ "schemaVersion":"2.0", "description":"Sample version 2.0 document v2", "parameters":{ "commands" : { "type": "StringList", "default": ["{{ssm:parameter-name}}"] } }, "mainSteps":[ { "action":"aws:runShellScript", "name":"runShellScript", "inputs":{ "runCommand": "{{commands}}" } } ] }

不要将 SSM 文档的 runtimeConfig 部分中使用的本地参数的类似句法与 Parameter Store 参数混淆。本地参数不同于 Systems Manager 参数。您可以通过是否存在 ssm: 前缀区分本地参数和 Systems Manager 参数:

"runtimeConfig":{ "aws:runShellScript":{ "properties":[ { "id":"0.aws:runShellScript", "runCommand":"{{ commands }}", "workingDirectory":"{{ workingDirectory }}", "timeoutSeconds":"{{ executionTimeout }}"
注意

SSM 文档不支持对 SecureString 参数的引用。因此,要通过 Run Command(举例来说)使用 SecureString 的参数,您必须在将参数值传递到 Run Command 之前,检索这些参数值,如以下示例所示。

Linux & macOS
value=$(aws ssm get-parameters --names parameter-name --with-decryption)
aws ssm send-command \ --name AWS-JoinDomain \ --parameters password=$value \ --instance-id instance-id
Windows
aws ssm send-command ^ --name AWS-JoinDomain ^ --parameters password=$value ^ --instance-id instance-id
Powershell
$secure = (Get-SSMParameterValue -Names parameter-name -WithDecryption $True).Parameters[0].Value | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -argumentlist user-name,$secure