從 Systems Manager Parameter Store 中取得安全字串值 - AWS CloudFormation

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

從 Systems Manager Parameter Store 中取得安全字串值

在 CloudFormation 中,可以將敏感資料儲存為 AWS Systems Manager 參數存放區中的「安全字串」,以使用密碼或授權金鑰等敏感資料,而無需直接在範本中公開。如需 Parameter Store 的簡介,請參閱《AWS Systems Manager 使用者指南》中的 AWS Systems Manager Parameter Store

若要在範本中使用 Parameter Store 安全字串,請使用 ssm-secure 動態參考。CloudFormation 永遠不會存放實際的安全字串值。相反,它僅存放文字動態參考,其中包含安全字串的純文字參數名稱。

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

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

使用 ssm-secure 動態參考時,需要謹記幾個重要事項:

  • CloudFormation 無法從其他 AWS 帳戶中存取 Parameter Store 值。

  • CloudFormation 在動態參考中不支援使用 Systems Manager 參數標籤或公有參數。

  • cn-north-1cn-northwest-1 區域中,Systems Manager 不支援安全字串。

  • 自訂資源中目前不支援安全值的動態參考,例如 ssm-secure

  • 如果 CloudFormation 需要復原堆疊更新,且之前指定的安全字串參數版本不再可用,則復原操作將失敗。在這種情況下,您有兩個選項:

    • 使用 CONTINUE_UPDATE_ROLLBACK 略過資源。

    • 在 Systems Manager 參數存放區中重新建立安全字串參數,並更新它,直到參數版本達到範本中使用的版本。然後,在不跳過資源的情況下使用 CONTINUE_UPDATE_ROLLBACK

對安全字串支援動態參數模式的資源

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

參考模式

若要在 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 使用者指南》中的使用參數版本

選用。

範例

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

JSON

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

YAML

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