透過 Run Command 命令來使用參數 - AWS Systems Manager

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

透過 Run Command 命令來使用參數

您可以使用中的參數Run Command,功能 AWS Systems Manager。如需詳細資訊,請參閱 AWS Systems Manager Run Command

執行 String 參數 (主控台)

以下程序會逐步解說如何執行使用 String 參數的命令。

使用 Parameter Store 執行字串參數
  1. 請在以下位置開啟 AWS Systems Manager 主控台。 https://console.aws.amazon.com/systems-manager/

  2. 在導覽窗格中,選擇 Run Command

  3. 選擇 執行命令

  4. Command document (命令文件) 清單中選擇 AWS-RunPowerShellScript (Windows) 或 AWS-RunShellScript (Linux)。

  5. 對於 Command parameters (命令參數),輸入 echo {{ssm:parameter-name}}。例如:echo {{ssm:/Test/helloWorld}}

  6. Targets (目標) 區段中,透過手動指定標籤、選取執行個體或邊緣裝置,或指定資源群組,選擇您要執行這項操作的受管節點。

    提示

    如果您預期看到的受管節點未列出,請參閱 疑難排解受管節點的可用性 以取得疑難排解秘訣。

  7. 對於 Other parameters (其他參數)

    • Comment (註解) 中,輸入此命令的相關資訊。

    • Timeout (seconds) (逾時 (秒)) 中,指定在命令執行全面失敗之前,系統要等候的秒數。

  8. 對於 Rate control (速率控制):

    • Concurrency (並行) 中,指定可同時執行命令的受管節點數目或百分比。

      注意

      如果透過指定套用至受管節點的標籤或指定 AWS 資源群組選取了目標,且您不確定會以多少個受管節點為目標,則透過指定百分比限制可以同時執行文件之目標的數量。

    • Error threshold (錯誤閾值) 中,指定在特定數目或百分比之節點上的命令失敗之後,停止在其他受管節點上執行命令。例如,如果您指定三個錯誤,則 Systems Manager 會在收到第四個錯誤時停止傳送命令。仍在處理命令的受管節點也可能會傳送錯誤。

  9. (選用) 針對 Output options (輸出選項),若要將命令輸出儲存至檔案,請選取 Write command output to an S3 bucket (將命令輸出寫入至 S3 儲存貯體) 方塊。在方塊中輸入儲存貯體和字首 (資料夾) 名稱。

    注意

    授予能力以將資料寫入至 S3 儲存貯體的 S3 許可,會是指派給執行個體之執行個體設定檔 (適用於 EC2 執行個體) 或 IAM 服務角色 (啟用混合模式的機器) 的許可,而不是執行此任務之 IAM 使用者的許可。如需詳細資訊,請參閱設定 Systems Manager 所需的執行個體許可或為混合式環境建立 IAM 服務角色。此外,如果指定的 S3 儲存貯體位於不同的儲存貯體 AWS 帳戶,請確定與受管節點關聯的執行個體設定檔或 IAM 服務角色具有寫入該儲存貯體的必要許可。

  10. SNS notifications (SNS 通知) 區段中,如果您要傳送有關命令執行狀態的通知,請選取 Enable SNS notifications (啟用 SNS 通知) 核取方塊。

    如需為 Run Command 設定 Amazon SNS 通知的詳細資訊,請參閱 使用 Amazon SNS 通知監控 Systems Manager 狀態變更

  11. 選擇執行

  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 參數值

下一個範例指令使用名為的SecureString參數SecurePasswordparameters 命令會擷取並解密 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 文件的參數部分中參考 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