如何在不同的 AWS 服务中引用 Systems Manager 参数?

3 分钟阅读
0

我想使用 AWS Systems Manager 中的一项功能 Parameter Store,将 Systems Manager 参数集成到各种 AWS 服务中。

简短描述

以下示例场景是可以引用 Systems Manager 参数的不同方式:

  • 在 Systems Manager 命令文档中引用常规字符串类型参数
  • 在 Systems Manager 命令文档中引用安全字符串类型参数
  • 在 AWS CloudFormation 模板中引用字符串类型参数
  • 在 Boto3 脚本中引用字符串类型参数
  • 在 Systems Manager 自动化文档中引用字符串类型参数
  • 在 AWS 命令行界面(AWS CLI)中引用字符串类型参数

解决方法

在 Systems Manager 命令文档中引用常规字符串类型参数

在此示例中,您正在 Amazon Elastic Compute Cloud(Amazon EC2)Linux x86(64 位)实例上安装某个版本的 AWS 命令行界面(AWS CLI)。该 AWS CLI 版本号以 /CLI/required-version 形式存储在 Parameter Store 中。

该示例在命令文档中将参数引用为 {{ssm:/CLI/required-version}}

**注意:**您可以使用以下格式 {{ssm:parameter-name}} 在命令文档中引用任何 Systems Manager 参数。

{  
  "mainSteps": [  
    {  
      "action": "aws:runShellScript",  
      "name": "installSoftware",  
      "inputs": {  
        "runCommand": [  
          "echo 'Installing AWS CLI version {{ssm:/CLI/required-version}}'",  
          "curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-{{ssm:/CLI/required-version}}" -o "awscliv2.zip",  
          "unzip awscliv2.zip",  
          "sudo ./aws/install"  
        ]  
      }  
    }  
  ]  
}

在 Systems Manager 命令文档中引用安全字符串类型参数

如果您使用的是 SecureString 参数类型,则必须先通过 AWS CLI 命令解密该参数。然后,就可以在命令文档中使用该参数。

**注意:**如果不先解密参数,则写入的值是元数据值。

下面这个示例在命令文档中引用安全字符串类型参数:

{  
  "mainSteps": [  
    {  
      "action": "aws:runShellScript",  
      "name": "installSoftware",  
      "inputs": {  
        "runCommand": [  
          "secure=$(aws ssm get-parameters --names /CLI/required-version --with-decryption --query Parameters[0].Value)",  
          "curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-$secure" -o "awscliv2.zip",  
          "unzip awscliv2.zip",  
          "sudo ./aws/install"  
        ]  
      }  
    }  
  ]  
}

在 CloudFormation 模板中引用字符串类型参数

您可以使用 CloudFormation 模板来引用字符串类型参数。将值定义为 String 表示该参数返回的值是字符串。有关其他支持的参数类型,请参阅支持的 SSM 参数类型

以下是启动 Amazon EC2 实例的示例,该实例以 InstanceName 的参数名称存储在 Parameter Store 中:

{  
  "Parameters": {  
    "MyInstanceName": {  
      "Type": "AWS::SSM::Parameter::Value<String>",  
      "Default": "InstanceName",  
      "Description": "Name of the EC2 instance"  
    }  
  },  
  "Resources": {  
    "TestInstance": {  
      "Type": "AWS::EC2::Instance",  
      "Properties": {  
        "ImageId": "ami-xxxx",  
        "InstanceType": "t3.xlarge",  
        "Tags": [{  
          "Key": "Name",  
          "Value": { "Ref": "MyInstanceName" }  
        }]  
      }  
    }  
  }  
}

CloudFormation 不支持将模板参数定义为 SecureString Systems Manager 参数类型。但是,您可以使用动态引用在 CloudFormation 模板中仅为支持的资源引用安全字符串参数。

如果您在 CloudFormation 模板中使用的是动态引用来引用安全字符串参数,请查看此示例

在 Boto3 脚本中引用字符串类型参数

您可以使用 Boto3 通过 Name 参数来调用 get_parameter 函数。您也可以包含 WithDecryption 字段来引用加密的参数。

以下示例引用名为 /instance/name 的安全字符串 Systems Manager 参数的值来启动实例。在此示例中,EC2 实例名称存储在 Systems Manager 参数 /instance/name: 中。

**注意:**如果将 Boto3 与字符串类型参数一起使用,则可以删除 WithDecryption 字段。

import boto3  
ec2_client = boto3.client('ec2')  
ssm_client = boto3.client('ssm')  
parameter_name = '/instance/name'  
response = ssm_client.get_parameter(  
    Name=parameter_name,  
    WithDecryption=True  
)  
instance_name = response['Parameter']['Value']  
response = ec2_client.describe_instances(  
    Filters=[  
        {  
            'Name': 'tag:Name',  
            'Values': [instance_name]  
        }  
    ]  
)  
instance_id = response['Reservations'][0]['Instances'][0]['InstanceId']  
ec2_client.start_instances(InstanceIds=[instance_id])  
print("Instance started successfully.")

在 Systems Manager 自动化文档中引用字符串类型参数

在 Systems Manager 自动化文档中,您可以使用 aws:executeAwsApi API 来引用 Systems Manager 参数。

在以下示例中,AMI Id 存储在 Systems Manager 参数中,并被解析为此自动化文档的输入。步骤 1 中的参数值作为输入传递到步骤 2,用于启动 EC2 实例。

**注意:**如果您引用的是 SecureString 参数,则可以将 WithDecryption 字段的值设置为 True。但是,自动化步骤 getparameter 的输出显示参数的解密值。


description: Sample runbook using AWS API operations
schemaVersion: '0.3'
assumeRole: '{{ AutomationAssumeRole }}'
parameters:
  AutomationAssumeRole:
    type: String
    description: (Optional) The ARN of the role that allows Automation to perform the actions on your behalf.
    default: ''
  AMIParameter:
    type: String
    description: SSM Parameter name to get the AMI ID from
mainSteps:
  - name: getparameter
    action: 'aws:executeAwsApi'
    inputs:
      Service: ssm
      Api: GetParameter
      Name: '{{AMIParameter}}'
      WithDecryption: false
    outputs:
      - Name: ImageId
        Selector: Parameter.Value
        Type: String
  - name: launchOneInstance
    action: 'aws:executeAwsApi'
    inputs:
      Service: ec2
      Api: RunInstances
      ImageId: '{{ getparameter.ImageId }}'
      MaxCount: 1
      MinCount: 1
    outputs:
      - Name: InstanceId
        Selector: '$.Instances[0].InstanceId'
        Type: String

在 AWS CLI 中引用字符串类型参数

在 AWS CLI 中引用 Systems Manager 参数与在命令文档中引用参数类似。您可以将字符串类型参数引用为 ssm:parameter-name。如果是安全字符串参数,则必须先解密,然后才能在 AWS CLI 命令中使用。

有关在 AWS CLI 中引用参数的示例,请参阅运行参数(AWS CLI)

AWS 官方
AWS 官方已更新 10 个月前