AWS::SecretsManager
转换
本主题介绍如何使用 AWS::SecretsManager
转换和 AWS::SecretsManager::RotationSchedule 资源类型来指定用于执行密钥轮换的 Lambda 函数。
AWS::SecretsManager
转换是一个 CloudFormation 宏,如果您在堆栈模板中引用了该宏,则会在您使用更改集创建或更新堆栈时自动生成一个执行密钥轮换的 Lambda 函数。Lambda 函数放置在处理后模板的嵌套堆栈中。该函数使用一个来自 AWS Secrets Manager 轮换 Lambda 函数
使用量
要使用 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-23
Resources:...
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 安全博客中的 Introducing an enhanced version of the AWS Secrets Manager transform: 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 resource
Properties: SecretId: !Reflogical name of AWS::SecretsManager::Secret resource
HostedRotationLambda: RotationType: MySQLSingleUser RotationLambdaName:name of Lambda function to be created
VpcSecurityGroupIds: !GetAttlogical 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
相关资源
有关可用于设置密钥轮换的完整 CloudFormation 模板示例,请参阅 AWS::SecretsManager::RotationSchedule
资源的示例部分。
有关使用宏的一般注意事项,请参阅注意事项。