如何參考不同 AWS 服務中的 Systems Manager 參數?

3 分的閱讀內容
0

我想要使用 Parameter Store (AWS Systems Manager 的一項功能) 將 Systems Managers 參數整合到各種 AWS 服務中。

簡短描述

下列範例案例是您可以參考 Systems Manager 參數的不同方式:

  • 參考 Systems Manager 命令文件中的常用字串類型參數
  • 參考 Systems Manager 命令文件中的安全字串類型參數
  • 在 AWS CloudFormation 範本中參考字串類型參數
  • 參考 Boto3 指令碼中的字串類型參數
  • 參考 Systems Manager 自動化文件中的字串類型參數
  • 參考 AWS Command Line Interface (AWS CLI) 中的字串類型參數

解決方法

參考 Systems Manager 命令文件中的常用字串類型參數

在此範例中,您將在 Amazon Elastic Compute Cloud (Amazon EC2) Linux x86 (64 位元) 執行個體上安裝某個版本的 AWS Command Line Interface (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 執行個體的範例,其名稱儲存在 Parameter Store 中參數名稱 InstanceName 下方:

{  
  "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 官方已更新 1 年前