AWS CloudFormation如何实现Terraform ignore_changes等价的属性变更忽略能力
CloudFormation 实现 Terraform
ignore_changes 等价能力的方案 问题场景
首先给出可复现问题的CloudFormation模板示例:
CognitoUserPoolIdentityProviderGoogle: Type: AWS::Cognito::UserPoolIdentityProvider Properties: ProviderName: Google AttributeMapping: email: emailAddress ProviderDetails: client_id: xxxx client_secret: yyyy authorize_scopes: profile email openid phone ProviderType: Google UserPoolId: Ref: CognitoUserPoolUserPool
基于上述模板部署完成后,会出现可复现的配置冲突:
- 运维人员手动将Cognito身份提供商的
client_id、client_secret修改为1111、2222 - 重新触发CloudFormation栈更新时,模板中硬编码的
xxxx、yyyy会强制覆盖线上手动修改的值,导致手动调整完全失效 - 核心诉求:找到CloudFormation全资源通用的等价能力,匹配Terraform中
ignore_changes的核心行为——即指定资源的部分属性变更,不被CloudFormation流程覆盖。
可落地的实现方案
CloudFormation没有内置和Terraform完全同名的ignore_changes参数,但可以通过以下三类全资源通用的方案实现等价效果:
1. 可变属性从硬编码模板中剥离,通过动态引用对齐线上值
- 移除模板中所有可能被手动调整、不希望被覆盖的属性值(本例即
client_id、client_secret) - 改为通过CloudFormation动态引用能力,从外部托管的配置源(如AWS Systems Manager Parameter Store、Secrets Manager)拉取对应值作为模板输入
- 当运维人员手动修改线上资源的这类属性后,同步更新外部配置源中存储的对应值,保持和线上值完全一致
- 此时执行栈更新时,CloudFormation比对模板引用值和线上资源值无差异,就不会触发属性覆盖
调整后的模板示例:
CognitoUserPoolIdentityProviderGoogle: Type: AWS::Cognito::UserPoolIdentityProvider Properties: ProviderName: Google AttributeMapping: email: emailAddress ProviderDetails: # 不再硬编码固定值,引用外部加密存储的动态配置 client_id: '{{resolve:ssm-secure:/cognito/google/client_id:1}}' client_secret: '{{resolve:ssm-secure:/cognito/google/client_secret:1}}' authorize_scopes: profile email openid phone ProviderType: Google UserPoolId: !Ref CognitoUserPoolUserPool
2. 借助栈更新策略拦截指定属性的变更
- 编写CloudFormation栈策略规则,匹配目标资源类型的指定属性路径,标记为禁止更新
- 栈更新流程传入该策略后,当检测到变更请求涉及被保护的属性时,会直接跳过该部分变更,不会覆盖线上已存在的手动修改值
- 该方案不需要改动原有模板的结构,仅通过规则配置即可实现单属性级别的变更豁免,是最贴近Terraform
ignore_changes行为的实现方式
栈策略示例片段:
{ "Statement" : [ { "Effect" : "Deny", "Action" : "Update:Modify", "Principal" : "*", "Resource" : "LogicalResourceId/CognitoUserPoolIdentityProviderGoogle", "Condition" : { "StringLike" : { "ResourcePath" : [ "Properties/ProviderDetails/client_id", "Properties/ProviderDetails/client_secret" ] } } } ] }
3. 将目标资源从主栈管理边界中移除
- 针对完全不需要CloudFormation管控的资源,从主栈模板中删除对应的资源定义
- 这类资源完全脱离栈的管理范围,后续任何栈更新操作都不会触碰该资源的配置,自然不会覆盖手动修改的内容
- 适合变更频率极低、零散的非核心资源,不适合生产环境中需要长期维护的关键资源。
内容的提问来源于stack exchange,提问作者blahblah
相关产品推荐
相关产品推荐

