簡體   English   中英

創建從 Parameter Store 中提取參數的 SSM 復合文檔 - AWS

[英]Creating a SSM Composite Document that pulls Parameters from Parameter Store - AWS

任務很簡單:每當使用標簽 key:value 啟動 EC2 實例時,我都希望它安裝特定的軟件。 每當使用不同的標簽 key:value 啟動 EC2 實例時,我希望它安裝不同的軟件。

我知道我可以在狀態管理器中創建 2 個不同的關聯,使用 runCommand RuneRemoteScript 來安裝基於標簽的軟件,但目標是擁有 1 個可以執行此操作的復合文檔。

任何幫助/指導將不勝感激!

您可以使用 SSM 自動化文檔來實現這一點 - https://docs.aws.amazon.com/systems-manager/latest/userguide/automation-branchdocs.html

但是,您可能需要執行以下操作:

  1. 在狀態管理器中使用AWS-RunDocument
  2. 此文檔應執行 SSM 自動化文檔(您的復合文檔)
  3. 您的復合文檔應如下所示:

I didn't validate this template, and I assume It shouldn't work without a few days of debugging

schemaVersion: '0.3'
parameters:
  InstanceId:
    type: String
mainSteps:
  - name: DescribeEc2
    action: 'aws:executeScript'
    inputs:
      Runtime: python3.7
      Handler: script_handler
      Script: |
        import json
        import boto3


        def script_handler(events):
            ec2_instance = boto3.client('ec2').describe_instances(
                InstanceIds=events["instance_id"],
            )["Reservations"][0]["Instances"][0]

            # thread it like an example,
            # Here you should parse your tags and decide what software you
            # want to install on the provided instance
            
            return json.dumps(
                {
                    "to_be_installed": "result"
                },
                sort_keys=True,
                default=str
            )

      InputPayload:
        instance_id: '{{ InstanceId }}'
    Outputs:
      - Name: result
        Selector: "$.to_be_installed"

  - name: WhatToInstall
    action: aws:branch
    inputs:
      Choices:
      - NextStep: InstallSoft1
        Variable: "{{DescribeEc2.result}}"
        StringEquals: soft_1
      - NextStep: InstallSoft1
        Variable: "{{DescribeEc2.result}}"
        StringEquals: soft_2

  - name: InstallSoft1
    action: aws:runCommand
    inputs:
      DocumentName: AWS-RunShellScript
      InstanceIds:
      - '{{ InstanceId }}'
      Parameters:
        commands:
        ...
  - name: InstallSoft2
    action: aws:runCommand
    inputs:
      DocumentName: AWS-RunShellScript
      InstanceIds:
      - '{{ InstanceId }}'
      Parameters:
        commands:
        ...

Tbh,您會發現這種解決方案有很多麻煩(IAM 和 SSM 特定問題),所以我建議使用Event Bridge -> Lambda Function (決定應該運行哪個文檔/自動化)-> SSM-RunDocument (直接執行在Lambda Function中)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM