編輯和建立關聯的新版本 - AWS Systems Manager

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

編輯和建立關聯的新版本

您可以編輯 State Manager 關聯以指定新名稱、排程、嚴重性等級或目標。您也可以選擇將命令的輸出寫入 Amazon Simple Storage Service (Amazon S3) 儲存貯體。在您編輯關聯後,State Manager 會建立新的版本。您可以依照下列程序,在編輯後檢視不同版本。

下列程序說明如何使用 Systems Manager 主控台、AWS Command Line Interface (AWS CLI) 和 AWS Tools for PowerShell (Tools for PowerShell) 來編輯和建立關聯的新版本。

重要

如果該文件是從另一個帳戶共用的,則 State Manager 不支援執行使用新版文件的關聯。若是從另一個帳戶共用的,則 State Manager 一律會執行文件的 default 版本,即使 Systems Manager 主控台顯示已處理新版本。如果您想要使用從另一個帳戶共用而來的新版本文件來執行關聯,則必須將文件版本設定為 default

建立關聯 (主控台)

以下程序說明如何使用 Systems Manager 主控台來編輯和建立關聯的新版本。

注意

此程序要求您擁有對現有 Amazon Simple Storage Service (Amazon S3) 儲存貯體的寫入權限。請注意,如果您未曾使用過 Amazon Simple Storage Service (Amazon S3),您將需要支付使用 Amazon Simple Storage Service (Amazon S3) 的費用。如需建立儲存貯體的詳細資訊,請參閱建立儲存貯體

編輯 State Manager 關聯
  1. 開啟位於 https://console.aws.amazon.com/systems-manager/ 的 AWS Systems Manager 主控台。

  2. 在導覽窗格中,選擇 State Manager

    -或-

    如果 AWS Systems Manager 首頁先開啟,選擇選單圖示 ( The menu icon ) 以開啟導覽窗格,然後選擇 State Manager

  3. 選擇您在 建立關聯 (命令列) 中建立的關聯,然後選擇 Edit (編輯)。

  4. Name (名稱) 欄位輸入新名稱。

  5. Specify schedule (指定排程) 區段中,選擇新選項。

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

    注意

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

  7. 選擇 Edit association (編輯關聯)。設定關聯以符合您目前的需求。

  8. Associations (關聯) 頁面中,選擇您編輯過的關聯名稱,然後選擇 Versions (版本) 標籤。系統會列出您所建立和編輯的關聯的每個版本。

  9. 請在 https://console.aws.amazon.com/s3/ 開啟 Amazon Simple Storage Service (Amazon S3) 主控台。

  10. 選擇您指定來存放命令輸出的 Amazon Simple Storage Service (Amazon S3) 儲存貯體名稱,然後選擇以執行該關聯的節點 ID 命名的資料夾。(如果您選擇將輸出存放在儲存貯體中的資料夾,請先打開它。)

  11. 向下切入多個層級,從 awsrunPowerShell 資料夾到 stdout 檔案。

  12. 選擇 Open (開啟)Download (下載) 以檢視主機名稱。

建立關聯 (命令列)

以下程序說明如何使用 AWS CLI (Linux 或 Windows) 或 AWS Tools for PowerShell 來編輯和建立關聯的新版本。

編輯 State Manager 關聯
  1. 如果您尚未安裝並設定 AWS CLI 或 AWS Tools for PowerShell,請進行相應的操作。

    如需相關資訊,請參閱安裝或更新 AWS CLI 的最新版本安裝 AWS Tools for PowerShell

  2. 使用以下格式來建立命令,以編輯和建立現有 State Manager 關聯的新版本。將每個範例資源預留位置取代為您自己的資訊。

    重要

    當您呼叫 UpdateAssociation 時,系統會從請求中卸除所有選用參數,並覆寫與這些參數之空值的關聯。這是設計本身所致。您必須在呼叫中指定所有選用參數,即使您未變更參數。這包括 Name 參數。呼叫此 API 動作之前,建議您呼叫 DescribeAssociation API 操作,並記下 UpdateAssociation 呼叫所需的所有選用參數。

    Linux & macOS
    aws ssm update-association \ --name document_name \ --document-version version_of_document_applied \ --instance-id instances_to_apply_association_on \ --parameters (if any) \ --targets target_options \ --schedule "cron_or_rate_expression" \ --schedule-offset "number_between_1_and_6" \ --output-location s3_bucket_to_store_output_details \ --association-name association_name \ --max-errors a_number_of_errors_or_a_percentage_of_target_set \ --max-concurrency a_number_of_instances_or_a_percentage_of_target_set \ --compliance-severity severity_level \ --calendar-names change_calendar_names \ --target-locations aws_region_or_account
    Windows
    aws ssm update-association ^ --name document_name ^ --document-version version_of_document_applied ^ --instance-id instances_to_apply_association_on ^ --parameters (if any) ^ --targets target_options ^ --schedule "cron_or_rate_expression" ^ --schedule-offset "number_between_1_and_6" ^ --output-location s3_bucket_to_store_output_details ^ --association-name association_name ^ --max-errors a_number_of_errors_or_a_percentage_of_target_set ^ --max-concurrency a_number_of_instances_or_a_percentage_of_target_set ^ --compliance-severity severity_level ^ --calendar-names change_calendar_names ^ --target-locations aws_region_or_account
    PowerShell
    Update-SSMAssociation ` -Name document_name ` -DocumentVersion version_of_document_applied ` -InstanceId instances_to_apply_association_on ` -Parameters (if any) ` -Target target_options ` -ScheduleExpression "cron_or_rate_expression" ` -ScheduleOffset "number_between_1_and_6" ` -OutputLocation s3_bucket_to_store_output_details ` -AssociationName association_name ` -MaxError a_number_of_errors_or_a_percentage_of_target_set -MaxConcurrency a_number_of_instances_or_a_percentage_of_target_set ` -ComplianceSeverity severity_level ` -CalendarNames change_calendar_names ` -TargetLocations aws_region_or_account

    以下範例會更新現有關聯,將名稱變更為 TestHostnameAssociation2。新的關聯版本會每個小時執行,並將命令的輸出寫入指定 Amazon Simple Storage Service (Amazon S3) 儲存貯體。

    Linux & macOS
    aws ssm update-association \ --association-id 8dfe3659-4309-493a-8755-01234EXAMPLE \ --association-name TestHostnameAssociation2 \ --parameters commands="echo Association" \ --output-location S3Location='{OutputS3Region=us-east-1,OutputS3BucketName=DOC-EXAMPLE-BUCKET,OutputS3KeyPrefix=logs}' \ --schedule-expression "cron(0 */1 * * ? *)"
    Windows
    aws ssm update-association ^ --association-id 8dfe3659-4309-493a-8755-01234EXAMPLE ^ --association-name TestHostnameAssociation2 ^ --parameters commands="echo Association" ^ --output-location S3Location='{OutputS3Region=us-east-1,OutputS3BucketName=DOC-EXAMPLE-BUCKET,OutputS3KeyPrefix=logs}' ^ --schedule-expression "cron(0 */1 * * ? *)"
    PowerShell
    Update-SSMAssociation ` -AssociationId b85ccafe-9f02-4812-9b81-01234EXAMPLE ` -AssociationName TestHostnameAssociation2 ` -Parameter @{"commands"="echo Association"} ` -S3Location_OutputS3BucketName DOC-EXAMPLE-BUCKET ` -S3Location_OutputS3KeyPrefix logs ` -S3Location_OutputS3Region us-east-1 ` -ScheduleExpression "cron(0 */1 * * ? *)"

    以下範例會更新現有關聯,將名稱變更為 CalendarAssociation。新的關聯會在行事曆開啟時執行,並將命令輸出寫入指定的 Amazon Simple Storage Service (Amazon S3) 儲存貯體。

    Linux & macOS
    aws ssm update-association \ --association-id 8dfe3659-4309-493a-8755-01234EXAMPLE \ --association-name CalendarAssociation \ --parameters commands="echo Association" \ --output-location S3Location='{OutputS3Region=us-east-1,OutputS3BucketName=DOC-EXAMPLE-BUCKET,OutputS3KeyPrefix=logs}' \ --calendar-names "arn:aws:ssm:us-east-1:123456789012:document/testCalendar2"
    Windows
    aws ssm update-association ^ --association-id 8dfe3659-4309-493a-8755-01234EXAMPLE ^ --association-name CalendarAssociation ^ --parameters commands="echo Association" ^ --output-location S3Location='{OutputS3Region=us-east-1,OutputS3BucketName=DOC-EXAMPLE-BUCKET,OutputS3KeyPrefix=logs}' ^ --calendar-names "arn:aws:ssm:us-east-1:123456789012:document/testCalendar2"
    PowerShell
    Update-SSMAssociation ` -AssociationId b85ccafe-9f02-4812-9b81-01234EXAMPLE ` -AssociationName CalendarAssociation ` -AssociationName OneTimeAssociation ` -Parameter @{"commands"="echo Association"} ` -S3Location_OutputS3BucketName DOC-EXAMPLE-BUCKET ` -CalendarNames "arn:aws:ssm:us-east-1:123456789012:document/testCalendar2"

    以下範例會更新現有關聯,將名稱變更為 MultiCalendarAssociation。新的關聯會在行事曆開啟時執行,並將命令輸出寫入指定的 Amazon Simple Storage Service (Amazon S3) 儲存貯體。

    Linux & macOS
    aws ssm update-association \ --association-id 8dfe3659-4309-493a-8755-01234EXAMPLE \ --association-name MultiCalendarAssociation \ --parameters commands="echo Association" \ --output-location S3Location='{OutputS3Region=us-east-1,OutputS3BucketName=DOC-EXAMPLE-BUCKET,OutputS3KeyPrefix=logs}' \ --calendar-names "arn:aws:ssm:us-east-1:123456789012:document/testCalendar1" "arn:aws:ssm:us-east-2:123456789012:document/testCalendar2"
    Windows
    aws ssm update-association ^ --association-id 8dfe3659-4309-493a-8755-01234EXAMPLE ^ --association-name MultiCalendarAssociation ^ --parameters commands="echo Association" ^ --output-location S3Location='{OutputS3Region=us-east-1,OutputS3BucketName=DOC-EXAMPLE-BUCKET,OutputS3KeyPrefix=logs}' ^ --calendar-names "arn:aws:ssm:us-east-1:123456789012:document/testCalendar1" "arn:aws:ssm:us-east-2:123456789012:document/testCalendar2"
    PowerShell
    Update-SSMAssociation ` -AssociationId b85ccafe-9f02-4812-9b81-01234EXAMPLE ` -AssociationName MultiCalendarAssociation ` -Parameter @{"commands"="echo Association"} ` -S3Location_OutputS3BucketName DOC-EXAMPLE-BUCKET ` -CalendarNames "arn:aws:ssm:us-east-1:123456789012:document/testCalendar1" "arn:aws:ssm:us-east-2:123456789012:document/testCalendar2"
  3. 若要檢視關聯的新版本,請執行以下命令。

    Linux & macOS
    aws ssm describe-association \ --association-id b85ccafe-9f02-4812-9b81-01234EXAMPLE
    Windows
    aws ssm describe-association ^ --association-id b85ccafe-9f02-4812-9b81-01234EXAMPLE
    PowerShell
    Get-SSMAssociation ` -AssociationId b85ccafe-9f02-4812-9b81-01234EXAMPLE | Select-Object *

    系統會傳回如下資訊。

    Linux & macOS
    {
        "AssociationDescription": {
            "ScheduleExpression": "cron(0 */1 * * ? *)",
            "OutputLocation": {
                "S3Location": {
                    "OutputS3KeyPrefix": "logs",
                    "OutputS3BucketName": "DOC-EXAMPLE-BUCKET",
                    "OutputS3Region": "us-east-1"
                }
            },
            "Name": "AWS-RunPowerShellScript",
            "Parameters": {
                "commands": [
                    "echo Association"
                ]
            },
            "LastExecutionDate": 1559316400.338,
            "Overview": {
                "Status": "Success",
                "DetailedStatus": "Success",
                "AssociationStatusAggregatedCount": {}
            },
            "AssociationId": "b85ccafe-9f02-4812-9b81-01234EXAMPLE",
            "DocumentVersion": "$DEFAULT",
            "LastSuccessfulExecutionDate": 1559316400.338,
            "LastUpdateAssociationDate": 1559316389.753,
            "Date": 1559314038.532,
            "AssociationVersion": "2",
            "AssociationName": "TestHostnameAssociation2",
            "Targets": [
                {
                    "Values": [
                        "Windows"
                    ],
                    "Key": "tag:Environment"
                }
            ]
        }
    }
    Windows
    {
        "AssociationDescription": {
            "ScheduleExpression": "cron(0 */1 * * ? *)",
            "OutputLocation": {
                "S3Location": {
                    "OutputS3KeyPrefix": "logs",
                    "OutputS3BucketName": "DOC-EXAMPLE-BUCKET",
                    "OutputS3Region": "us-east-1"
                }
            },
            "Name": "AWS-RunPowerShellScript",
            "Parameters": {
                "commands": [
                    "echo Association"
                ]
            },
            "LastExecutionDate": 1559316400.338,
            "Overview": {
                "Status": "Success",
                "DetailedStatus": "Success",
                "AssociationStatusAggregatedCount": {}
            },
            "AssociationId": "b85ccafe-9f02-4812-9b81-01234EXAMPLE",
            "DocumentVersion": "$DEFAULT",
            "LastSuccessfulExecutionDate": 1559316400.338,
            "LastUpdateAssociationDate": 1559316389.753,
            "Date": 1559314038.532,
            "AssociationVersion": "2",
            "AssociationName": "TestHostnameAssociation2",
            "Targets": [
                {
                    "Values": [
                        "Windows"
                    ],
                    "Key": "tag:Environment"
                }
            ]
        }
    }
    PowerShell
    AssociationId                 : b85ccafe-9f02-4812-9b81-01234EXAMPLE
    AssociationName               : TestHostnameAssociation2
    AssociationVersion            : 2
    AutomationTargetParameterName : 
    ComplianceSeverity            : 
    Date                          : 5/31/2019 2:47:18 PM
    DocumentVersion               : $DEFAULT
    InstanceId                    : 
    LastExecutionDate             : 5/31/2019 3:26:40 PM
    LastSuccessfulExecutionDate   : 5/31/2019 3:26:40 PM
    LastUpdateAssociationDate     : 5/31/2019 3:26:29 PM
    MaxConcurrency                : 
    MaxErrors                     : 
    Name                          : AWS-RunPowerShellScript
    OutputLocation                : Amazon.SimpleSystemsManagement.Model.InstanceAssociationOutputLocation
    Overview                      : Amazon.SimpleSystemsManagement.Model.AssociationOverview
    Parameters                    : {[commands, Amazon.Runtime.Internal.Util.AlwaysSendList`1[System.String]]}
    ScheduleExpression            : cron(0 */1 * * ? *)
    Status                        : 
    Targets                       : {tag:Environment}