

# 获取 Systems Manager Parameter Store 中的纯文本值
<a name="dynamic-references-ssm"></a>

在创建 CloudFormation 模板时，可能需要使用存储在 Parameter Store 中的纯文本值。Parameter Store 是 AWS Systems Manager 的一项功能。有关 Parameter Store 的介绍，请参阅《AWS Systems Manager 用户指南》中的 [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)**。

要在模板中使用 Parameter Store 中的明文值，请使用 `ssm` 动态引用。此引用让您能够访问 Parameter Store 中 `String` 或 `StringList` 类型的参数的值。

要验证堆栈操作中将使用哪个版本的 `ssm` 动态引用，请为堆栈操作创建更改集。然后，在**模板**选项卡上查看已处理的模板。有关更多信息，请参阅 [创建 CloudFormation 堆栈的更改集](using-cfn-updating-stacks-changesets-create.md)。

使用 `ssm` 动态引用时，需要注意几个重要的事项：
+ CloudFormation 不支持对动态引用进行偏差检测。对于尚未指定参数版本的 `ssm` 动态引用，建议在 Systems Manager 中更新参数版本时，还要对包含 `ssm` 动态引用的任何堆栈执行堆栈更新操作，以获取最新的参数版本。
+ 要在 CloudFormation 模板的 `Parameters` 部分中使用 `ssm` 动态引用，必须包含版本号。CloudFormation 不允许在这一部分中引用没有版本号的 Parameter Store 值。您也可以在模板中将您的参数定义为某个 Systems Manager 参数类型。执行此操作时，您可以将某个 Systems Manager 参数键指定为参数的默认值。然后，CloudFormation 将从 Parameter Store 中检索最新版本的参数值，而无需您指定版本号。这可以使您的模板更简洁、更易于维护。有关更多信息，请参阅 [使用 CloudFormation 提供的参数类型在运行时指定现有资源](cloudformation-supplied-parameter-types.md)。
+ 对于自定义资源，CloudFormation 在将请求发送到自定义资源之前解析 `ssm` 动态引用。
+ CloudFormation 不支持使用动态引用来引用另一个 AWS 账户共享的参数。
+ CloudFormation 不支持在动态引用中使用 Systems Manager 参数标签。

## 权限
<a name="dynamic-references-ssm-permissions"></a>

要指定存储在 Systems Manager Parameter Store 中的参数，您必须有权为指定的参数调用 [https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParameter.html](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParameter.html)。要了解如何创建提供对特定 Systems Manager 参数的访问权限的 IAM 策略，请参阅《*AWS Systems Manager 用户指南*》中的[使用 IAM 策略限制对 Parameter Store 参数的访问](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-paramstore-access.html)。

## 引用模式
<a name="dynamic-references-ssm-pattern"></a>

要在 CloudFormation 模板中引用 Systems Manager Parameter Store 中存储的明文值，请使用以下 `ssm` 引用模式。

```
{{resolve:ssm:{{parameter-name}}:{{version}}}}
```

您的引用必须遵循以下参数名称和版本的正则表达式模式：

```
{{resolve:ssm:[a-zA-Z0-9_.\-/]+(:\d+)?}}
```

`parameter-name`  
 Parameter Store 中的参数的名称。参数名称区分大小写。  
必需。

`version`  
一个整数，指定要使用的参数的版本。如果您没有指定确切的版本，CloudFormation 将在创建或更新堆栈时使用参数的最新版本。有关更多信息，请参阅《AWS Systems Manager 用户指南》中的[使用参数版本](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-paramstore-versions.html)**。  
可选。

## 示例
<a name="dynamic-references-ssm-examples"></a>

**Topics**
+ [公用 AMI ID 参数](#dynamic-references-ssm-public-ami-example)
+ [自定义 AMI ID 参数](#dynamic-references-ssm-custom-ami-example)

### 公用 AMI ID 参数
<a name="dynamic-references-ssm-public-ami-example"></a>

以下示例创建的 EC2 实例引用了一个公用 AMI 参数。动态引用从公有参数中检索最新版 Amazon Linux 2023 AMI ID。有关公有参数的更多信息，请参阅《AWS Systems Manager 用户指南》**中的[发现 Parameter Store 中的公有参数](https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-finding-public-parameters.html)。

#### JSON
<a name="dynamic-references-ssm-public-ami-example.json"></a>

```
{
    "Resources": {
        "MyInstance": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "ImageId": "{{resolve:ssm:{{/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64}}}}",
                "InstanceType": "t2.micro"
            }
        }
    }
}
```

#### YAML
<a name="dynamic-references-ssm-public-ami-example.yaml"></a>

```
Resources:
  MyInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: '{{resolve:ssm:{{/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64}}}}'
      InstanceType: t2.micro
```

### 自定义 AMI ID 参数
<a name="dynamic-references-ssm-custom-ami-example"></a>

以下示例创建了一个 EC2 启动模板，其中引用了存储在 Parameter Store 中的自定义 AMI ID。每次从启动模板启动实例时，动态引用都会从 `{{golden-ami}}` 参数的版本 {{`2`}} 中检索 AMI ID。

#### JSON
<a name="dynamic-references-ssm-custom-ami-example.json"></a>

```
{
    "Resources": {
        "MyLaunchTemplate": {
            "Type": "AWS::EC2::LaunchTemplate",
            "Properties": {
                "LaunchTemplateName": {
                    "Fn::Sub": "${AWS::StackName}-launch-template"
                },
                "LaunchTemplateData": {
                    "ImageId": "{{resolve:ssm:{{golden-ami:2}}}}",
                    "InstanceType": "t2.micro"
                }
            }
        }
    }
}
```

#### YAML
<a name="dynamic-references-ssm-custom-ami-example.yaml"></a>

```
Resources:
  MyLaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties: 
      LaunchTemplateName: !Sub ${AWS::StackName}-launch-template
      LaunchTemplateData:
        ImageId: '{{resolve:ssm:{{golden-ami:2}}}}'
        InstanceType: t2.micro
```