(deploy): Resource Change Filter for `cdk diff` and `cdk deploy`
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- aws, typescript
调研方向
首先阅读 DiffFormatter 的集成点以及来自 @aws-cdk/cloudformation-diff 的 ResourceDifference 对象,然后跟踪 FullCloudFormationDeployment 如何执行 changeset。将通过 CLI、cdk.json 和环境变量提供的请求输入与现有的选项处理进行比较。当资源过滤器和属性过滤器在 diff 和 deploy 期间得到验证,且不匹配的更改产生指定的失败行为时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
Summary
Add a safety mechanism to cdk diff and cdk deploy that allows specifying one or multiple resource filters. If changes to resources not matching these filters are detected, the operation should abort. This provides a safeguard against unintended infrastructure changes, particularly useful in CI/CD pipelines.
Use Case
In many CDK applications, stacks contain numerous resources, but developers should only be allowed to modify specific resource types during certain deployments. For example:
- A developer pushing Lambda function code updates should only modify
AWS::Lambda::Functionresources - Automated deployments should only affect specific resource properties like
AWS::Lambda::Function.Code.S3Key - CI/CD pipelines should abort if unexpected infrastructure changes (e.g., IAM roles, VPCs, databases) are detected alongside application code changes
Currently, there's no built-in way to validate that a deployment only affects specific resources, leading to potential risks of accidentally deploying unwanted infrastructure changes.
Proposed Solution
Command Line Options
Add new options to cdk diff and cdk deploy:
# Allow only specific resource types
cdk deploy --allow-resource-changes AWS::Lambda::Function,Custom::CDKBucketDeployment
# Allow only specific resource properties
cdk deploy --allow-resource-changes AWS::Lambda::Function.Code.S3Key
# Multiple filters (any match allows the change)
cdk deploy --allow-resource-changes AWS::Lambda::Function.Runtime \
--allow-resource-changes AWS::Lambda::Function.Code
# Combined with other options
cdk diff Stack1 --allow-resource-changes AWS::Lambda::Function --fail
Behavior
- During diff/deploy: After generating the changeset or template diff, analyze all resource changes
- Filter matching: Check each changed resource against the specified filters:
- Resource type filter:
AWS::Lambda::Function- matches any change to Lambda functions - Property-specific filter:
AWS::Lambda::Function.Code.S3Key- matches only changes to the S3Key property of Lambda function code - Wildcard support:
AWS::Lambda::*- matches all Lambda-related resources
- Resource type filter:
- Abort behavior: If any resource change does not match at least one filter, abort the operation with:
- Exit code 1
- Clear error message listing the unmatched changes
- Suggestion to review changes or update filters
Example Scenarios
Scenario 1: Lambda-only deployment
cdk deploy --allow-resource-changes AWS::Lambda::Function
Allowed changes:
- Lambda function runtime update
- Lambda function memory size change
- Lambda function code update
Blocked changes:
- IAM role modifications
- DynamoDB table changes
- VPC configuration updates
Scenario 2: Code-only deployment
cdk deploy --allow-resource-changes AWS::Lambda::Function.Code.S3Key
Allowed changes:
- Lambda function code S3 key update (code deployment only)
Blocked changes:
- Lambda function runtime change
- Lambda function memory size change
- Any other Lambda property changes
- Any changes to other resource types
Scenario 3: Multiple allowed resources
cdk deploy --allow-resource-changes AWS::Lambda::Function \
--allow-resource-changes Custom::CDKBucketDeployment
Allowed changes:
- Any Lambda function changes
- Any CDK bucket deployment changes
Blocked changes:
- All other resource types
Scenario 4: API Gateway definition-only deployment
cdk deploy --allow-resource-changes AWS::ApiGateway::RestApi.Body
Allowed changes:
- API Gateway REST API definition/specification updates (OpenAPI/Swagger changes)
Blocked changes:
- API Gateway stage configuration changes
- API Gateway deployment settings
- Lambda function changes
- IAM role modifications
- Any other resource types
Error Output Example
When unwanted changes are detected:
❌ Deployment aborted: Detected changes to resources outside allowed filters
Allowed resource changes:
• AWS::Lambda::Function.Code.S3Key
Detected changes that violate the filter:
• MyFunction (AWS::Lambda::Function)
- Property: Runtime (changed from nodejs18.x to nodejs20.x)
• MyRole (AWS::IAM::Role)
- Property: Policies (changed)
To proceed with these changes, either:
1. Review and remove the unwanted changes from your CDK code
2. Update your --allow-resource-changes filters to include these resource types
3. Remove the --allow-resource-changes option to deploy all changes
Implementation Considerations
Integration Points
- Diff Stage: After computing the template diff (in
DiffFormatter), apply resource filters before displaying/evaluating - Deploy Stage: Before executing the changeset (in
FullCloudFormationDeployment), validate against filters - Filter Matching Logic:
- Parse filter strings into resource type and optional property path
- Match against
ResourceDifferenceobjects from@aws-cdk/cloudformation-diff - Support dot notation for property paths (e.g.,
Properties.Code.S3Key)
Configuration Options
Support multiple ways to specify filters:
- CLI flags:
--allow-resource-changes(as shown above) - cdk.json:
{
"allowResourceChanges": [
"AWS::Lambda::Function.Code.S3Key",
"Custom::CDKBucketDeployment"
]
}
- Environment variable:
CDK_ALLOW_RESOURCE_CHANGES="AWS::Lambda::Function,Custom::CDKBucketDeployment"
CLI flags should take precedence over configuration file, which takes precedence over environment variables.
Filter Syntax
AWS::Lambda::Function- Match any changes to Lambda functionsAWS::Lambda::Function.Properties.Code- Match changes to the Code propertyAWS::Lambda::Function.Properties.Code.S3Key- Match only S3Key changes within CodeAWS::Lambda::*- Match all Lambda-related resource types (with wildcard support)*.Properties.Tags- Match tag changes on any resource type
Benefits
- Safety: Prevents accidental deployment of unintended infrastructure changes
- CI/CD Integration: Enables fine-grained deployment controls in automated pipelines
- Developer Workflow: Allows limiting developer permissions to specific resource types
- Audit & Compliance: Provides clear validation of what changed in each deployment
- Fail-Fast: Catches issues during diff/deploy rather than after deployment completes
Alternatives Considered
- Manual review: Requires human inspection of every diff, error-prone and time-consuming
- Separate stacks: Splitting resources into multiple stacks creates complexity and cross-stack dependencies
- Custom scripts: External validation scripts are fragile and don't integrate with CDK workflow
- Stack policies: CloudFormation stack policies are resource-level only and don't support property-level granularity
Related Features
- Similar to
--exclusivelyflag (which filters stacks) - Complements
--failflag forcdk diff(which fails on any diff) - Extends security approval prompts (which warn on IAM changes)
- Conceptually similar to CloudFormation Hooks but executed client-side before deployment rather than server-side during deployment
Additional Notes
This feature would be particularly valuable for:
- Serverless applications: Frequent Lambda code deployments with stable infrastructure
- GitOps workflows: Automated deployments with safety constraints
- Multi-team environments: Different teams with different permission boundaries
- Compliance requirements: Enforcing change management policies
- 主要语言
- TypeScript
- 星标
- 110
- 派生
- 138
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 55
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
aws/aws-cdk-cli 的其他 Issue
-
aws-cdk bug needs-triage
难度 2/5 1-3 小时 新手友好度 76/100
aws/aws-cdk-cli#2179 ·
维护者通常 1 天内回复
-
(cli): deploy waits forever when an AWS API call gets no response, because requestTimeout only logs a warning可能已有人在做 @abhiramshaji-cyber 于 27 天前认领。 未关闭aws-cdk bug p2
难度 2/5 1-3 小时 新手友好度 76/100
aws/aws-cdk-cli#2097 ·
维护者通常 1 天内回复
-
(bootstrap) FilePublishingRoleDefaultPolicy permission incorrect when FileAssetsBucketKmsKeyId is an ARN value可能已有人在做 @lemon0333 于 95 天前认领。 未关闭bug p2
难度 2/5 1-3 小时 新手友好度 74/100
aws/aws-cdk-cli#921 · 1 条评论 ·
维护者通常 1 天内回复
-
(garbage-collection): cdk gc takes hours on a large bootstrap bucket可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 3/5 1-2 天 新手友好度 72/100
aws/aws-cdk-cli#2165 ·
维护者通常 1 天内回复
-
(cli): cdk flags --set always reports no template differences before applying flags可能已有人在做 @go-to-k 于 10 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 35/100
aws/aws-cdk-cli#2153 ·
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
GetStream/stream-chat-react#3467 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
vercel-labs/ui#1139 ·
维护者通常 1 天内回复
-
generate_image reference URL guard lets through IPv4-mapped IPv6 hosts and 100.64.0.0/10可能已有人在做 @goranai 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
bot:ai-assisted component:platform-js status:untriaged
难度 2/5 1-3 小时 新手友好度 82/100
midnightntwrk/platform-js#93 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100