這是新的 CloudFormation 範本參考指南。請更新您的書籤和連結。如需 CloudFormation 入門的說明,請參閱 AWS CloudFormation 使用者指南。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS::SecretsManager 轉換
本主題說明如何使用 AWS::SecretsManager 轉換和 AWS::SecretsManager::RotationSchedule 資源類型來指定 Lambda 函式,以執行秘密輪換。
AWS::SecretsManager 轉換是一種 CloudFormation 巨集,當在堆疊範本中參考它時,如果使用變更集來建立或更新堆疊,會為秘密輪換自動產生 Lambda 函式。Lambda 函式放置在已處理範本的巢狀堆疊中。它使用來自 AWS Secrets Manager Rotation Lambda Functions
用量
若要使用 AWS::SecretsManager 轉換,必須在 CloudFormation 範本的頂層宣告轉換。您無法將 AWS::SecretsManager 作為內嵌於任何其他範本區段的轉換使用。
宣告必須使用常值字串 AWS::SecretsManager-2020-07-23 或 AWS::SecretsManager-2024-09-16 作為其值。您不能使用參數或函數來指定轉換值。
語法
若要在您的 CloudFormation 範本中宣告此轉換,請使用下列語法:
JSON
{ "Transform":"AWS::SecretsManager-2020-07-23", "Resources":{...} }
YAML
Transform: AWS::SecretsManager-2020-07-23Resources:...
AWS::SecretsManager 轉換是沒有其他參數的獨立宣告。反之,可以在堆疊範本中設定 AWS::SecretsManager::RotationSchedule 資源的 HostedRotationLambda 屬性。HostedRotationLambda 屬性會指定 Lambda 函式,以執行秘密輪換。
AWS::SecretsManager-2024-09-16 中的新功能
最新版本的 AWS::SecretsManager 轉換 (AWS::SecretsManager-2024-09-16) 引入了下列增強功能:
-
自動 Lambda 升級 – 當您更新 CloudFormation 堆疊時,Lambda 函式現在會自動更新其執行時期組態和內部相依性。這可確保您使用最安全且可靠的程式碼版本,以管理 Secrets Manager 中的秘密輪換。
-
支援其他屬性 – 新轉換在與
HostedRotationLambda屬性搭配使用時,支援AWS::SecretsManager::RotationSchedule資源類型的其他資源屬性,包括DependsOn屬性。注意
這兩個版本都支援
DeletionPolicy和UpdateReplacePolicy屬性。
若要進一步了解此新版本的 AWS::SecretsManager 轉換,請參閱 AWS 安全部落格上的介紹 AWS Secrets Manager 轉換的增強版本:AWS::SecretsManager-2024-09-16
範例
下列範例示範如何在範本中使用 AWS::SecretsManager 轉換 (AWS::SecretsManager-2024-09-16) 和 AWS::SecretsManager::RotationSchedule 資源。在此範例中,CloudFormation 將自動為 MySQL 單一使用者秘密輪換產生 Lambda 函式。
秘密設定為每天午夜自動輪換 (UTC)。輪換程序可能最多需要 2 小時才能完成。更新輪換排程不會立即開始輪換。
JSON
{ "AWSTemplateFormatVersion":"2010-09-09", "Transform":"AWS::SecretsManager-2024-09-16", "Resources":{..."MySecretRotationSchedule":{ "Type":"AWS::SecretsManager::RotationSchedule", "DependsOn":"logical name of AWS::SecretsManager::SecretTargetAttachment resource", "Properties":{ "SecretId":{ "Ref":"logical name of AWS::SecretsManager::Secret resource" }, "HostedRotationLambda":{ "RotationType":"MySQLSingleUser", "RotationLambdaName":"name of Lambda function to be created", "VpcSecurityGroupIds":{ "Fn::GetAtt":[ "logical name of AWS::EC2::SecurityGroup resource", "GroupId" ] }, "VpcSubnetIds":{ "Fn::Join":[ ",", [ { "Ref":"logical name of primary subnet" }, { "Ref":"logical name of secondary subnet" } ] ] } }, "RotationRules":{ "ScheduleExpression":"cron(0 0 * * ? *)", "Duration":"2h" }, "RotateImmediatelyOnUpdate":false } } } }
YAML
AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::SecretsManager-2024-09-16 Resources:...MySecretRotationSchedule: Type: AWS::SecretsManager::RotationSchedule DependsOn:logical name of AWS::SecretsManager::SecretTargetAttachment resourceProperties: SecretId: !Reflogical name of AWS::SecretsManager::Secret resourceHostedRotationLambda: RotationType: MySQLSingleUser RotationLambdaName:name of Lambda function to be createdVpcSecurityGroupIds: !GetAttlogical name of AWS::EC2::SecurityGroup resource.GroupId VpcSubnetIds: Fn::Join: - "," - - Ref:logical name of primary subnet- Ref:logical name of secondary subnetRotationRules: ScheduleExpression: cron(0 0 * * ? *) Duration: 2h RotateImmediatelyOnUpdate: false
相關資源
如需可用來設定秘密輪換的完整 CloudFormation 範本範例,請參閱 AWS::SecretsManager::RotationSchedule 資源的範例一節。
如需有關使用巨集的一般資訊,請參閱《AWS CloudFormation 使用者指南》中的使用範本巨集在 CloudFormation 範本上執行自訂處理。