繁体   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