在下面(缩写为 CloudFormation 模板)中,我尝试配置 AWS Lambda 函数以从注入其环境的 AWS Secrets Manager 获取值:
Resources:
Function:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
SECRET_KEY: !Sub '{{resolve:secretsmanager:${Secret}:SecretString:KEY}}'
Secret:
Type: AWS::SecretsManager::Secret
Properties:
Name: 'very-secret-thing'
SecretString: '{"KEY":"dummy"}'
使用此模板创建堆栈时,一切都会按预期进行。然后,我去 CloudFormation 之外更改秘密的值,因为我并不真正希望将秘密签入源代码管理。这是完全有可能的,并且文档 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-secretsmanager-secret.html#cfn-secretsmanager-secret-secretstring意味着,只要我避免更改虚拟值,后续的 CloudFormation 堆栈更新就不会触及密钥的值SecretString
在模板中。
因此,在 AWS 控制台中设置实际密钥后,我需要触发 Lambda 函数的重新部署,以便 CloudFormation 解析新的密钥值并在函数的环境中设置。我怎么做?
执行中aws cloudformation deploy
失败并显示消息:无需进行任何更改即可部署。
我怀疑 CloudFormation 正在将模板的“原始”版本与最后部署的版本进行比较,而没有首先解析对 Secrets Manager 的引用。是这样吗?是否有一些技巧可以强制提前解除引用?
Footnote: I am well aware that using Secrets Manager this way will cause the secret value to be visible in the AWS Lambda Console, and that getting the value from Secrets Manager at runtime would be the more secure approach. That just happens to be out-of-scope for what I am hoping to do.
你可以人为地改变别的东西 on the AWS::Serverless::Function
资源以在您进行部署时强制其更新。
例如,说一个时间戳:
Parameters:
DeployTimestamp: { Type: String }
Resources:
Function:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
SECRET_KEY: !Sub '{{resolve:secretsmanager:${Secret}:SecretString:KEY}}'
SECRET_KEY_UPDATED: !Ref DeployTimestamp
假设您从脚本进行部署,那么您会执行类似的操作aws cloudformation deploy --parameter-overrides "DeployTimestamp=$(date)"
每次更改该值。
当然,这样做的缺点是该函数将更新每个部署,即使秘密尚未更新。如果这让你烦恼,你可以变得更漂亮并注射aws secretsmanager describe-secret --query LastChangedDate
作为参数而不是当前时间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)