

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 從 Systems Manager Parameter Store 中取得安全字串值
<a name="dynamic-references-ssm-secure-strings"></a>

在 CloudFormation 中，您可以將敏感資料儲存為 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-secure` 動態參考。CloudFormation 永遠不會存放實際的安全字串值。相反，它僅存放文字動態參考，其中包含安全字串的純文字參數名稱。

在堆疊建立或更新期間，CloudFormation 會視需要存取安全字串值，而不會公開實際值。安全字串僅用於有支援 `ssm-secure` 動態參考模式的資源屬性。如需詳細資訊，請參閱[對安全字串支援動態參數模式的資源](#template-parameters-dynamic-patterns-resources)。

CloudFormation 不會在任何 API 呼叫中傳回安全字串的實際參數值。它只會傳回常值動態參考。使用變更集來比較變更時，CloudFormation 只會比較文字動態參考字串。它不會解析和比較實際安全字串值。

使用 `ssm-secure` 動態參考時，需要謹記幾個重要事項：
+ CloudFormation 無法從其他 AWS 帳戶中存取 Parameter Store 值。
+ CloudFormation 在動態參考中不支援使用 Systems Manager 參數標籤或公有參數。
+ 在 `cn-north-1` 和 `cn-northwest-1` 區域中，Systems Manager 不支援安全字串。
+ 自訂資源中目前不支援安全值的動態參考，例如 `ssm-secure`。
+ 如果 CloudFormation 需要復原堆疊更新，且之前指定的安全字串參數版本不再可用，則復原操作將失敗。在這種情況下，您有兩個選項：
  + 使用 `CONTINUE_UPDATE_ROLLBACK` 略過資源。
  + 在 Systems Manager 參數存放區中重新建立安全字串參數，並更新它，直到參數版本達到範本中使用的版本。然後，在不跳過資源的情況下使用 `CONTINUE_UPDATE_ROLLBACK`。

## 對安全字串支援動態參數模式的資源
<a name="template-parameters-dynamic-patterns-resources"></a>

支援 `ssm-secure` 動態參考模式的資源包括：


| 資源 | 屬性類型 | Properties | 
| --- | --- | --- | 
| [AWS::DirectoryService::MicrosoftAD](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-directoryservice-microsoftad.html) |  | `Password` | 
| [AWS::DirectoryService::SimpleAD](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-directoryservice-simplead.html) |  | `Password` | 
| [AWS::ElastiCache::ReplicationGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-elasticache-replicationgroup.html) |  | `AuthToken` | 
| [AWS::IAM::User](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-iam-user.html) | [LoginProfile](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-iam-user-loginprofile.html) | `Password` | 
| [AWS::KinesisFirehose::DeliveryStream](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-kinesisfirehose-deliverystream.html) | [RedshiftDestinationConfiguration](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-kinesisfirehose-deliverystream-redshiftdestinationconfiguration.html) | `Password` | 
| [AWS::OpsWorks::App](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-opsworks-app.html) | [來源](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-opsworks-app-source.html) | `Password` | 
| [AWS::OpsWorks::Stack](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-opsworks-stack.html) | [CustomCookbooksSource](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-opsworks-stack-source.html) | `Password` | 
| [AWS::OpsWorks::Stack](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-opsworks-stack.html) | [RdsDbInstances](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-opsworks-stack-rdsdbinstance.html) | `DbPassword` | 
| [AWS::RDS::DBCluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-rds-dbcluster.html) |  | `MasterUserPassword` | 
| [AWS::RDS::DBInstance](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-rds-dbinstance.html) |  | `MasterUserPassword` | 
| [AWS::Redshift::Cluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-redshift-cluster.html) |  | `MasterUserPassword` | 

## 參考模式
<a name="dynamic-references-ssm-secure-pattern"></a>

若要在 CloudFormation 範本中參考 Systems Manager Parameter Store 的安全字串值，請使用下列 `ssm-secure` 參考模式。

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

針對 parameter-name 和 version，您的參考必須遵守下列規則表達式模式：

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

`parameter-name`  
 參數存放區中的參數名稱。函數名稱區分大小寫。  
必要.

`version`  
整數，指定要使用的參數版本。如果您沒有指定確切的版本，CloudFormation 會在您建立或更新堆疊時使用最新版本的參數。如需詳細資訊，請參閱《AWS Systems Manager 使用者指南》中的[使用參數版本](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-paramstore-versions.html)。**  
選用。

## 範例
<a name="dynamic-references-ssm-secure-example"></a>

以下範例使用 `ssm-secure` 動態參考，將 IAM 使用者的密碼設為存放在 Parameter Store 的安全字串。依規定，CloudFormation 會使用 `{{IAMUserPassword}}` 參數的 {{`10`}} 版本，用於堆疊和變更集操作。

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

```
  "MyIAMUser": {
    "Type": "AWS::IAM::User",
    "Properties": {
      "UserName": "MyUserName",
      "LoginProfile": {
        "Password": "{{resolve:ssm-secure:{{IAMUserPassword:10}}}}"
      }
    }
  }
```

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

```
  MyIAMUser:
    Type: AWS::IAM::User
    Properties:
      UserName: 'MyUserName'
      LoginProfile:
        Password: '{{resolve:ssm-secure:{{IAMUserPassword:10}}}}'
```