feat(core): embed structured design context in synthesized templates (MetadataContext) - #38381
feat(core): embed structured design context in synthesized templates (MetadataContext)#38381satyakigh wants to merge 7 commits into
Conversation
|
PRs without a linked issue will receive lower priority for review and merging. Please update the description to follow the PR template and include a line like |
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
8da308b to
517cab1
Compare
mrgrain
left a comment
There was a problem hiding this comment.
Hey, since the feature is already stable, the Mixin should go straight into aws-cdk-lib as well. The preview package is being phased out.
b362a0a to
b6106b6
Compare
Sounds good, updated |
|
Draft until RFC 972: Structured design context in synthesized templates is approved. |
…and aspect + mixins to set context on a resource and template
…ib core The mixins-preview package is being phased out and the feature is stable, so the Mixin ships directly in aws-cdk-lib alongside the MetadataContext facade (review feedback). - MetadataContextMixin now lives in core/lib/mixins/ and is exported flat from aws-cdk-lib (a top-level 'mixins' jsii submodule is not possible: JSII5011 name conflict with the Mixins class), with an awslint exclusion for the mixin-namespace rule. - Unit test moved to core/test/mixins/, adapted to core's toCloudFormation convention. - Integ test moved to @aws-cdk-testing/framework-integ test/core/test/ with regenerated snapshot. - aws-cdk-lib README documents the mixin inline; all mixins-preview changes reverted.
22f9fba to
f69068b
Compare
Reason for this change
CloudFormation templates capture what infrastructure exists but not why — rationale, invariants, change-safety, and operational knowledge live in code comments, wikis, and people's heads, and are lost by the time someone (or an automated tool/agent) modifies the deployed template. A structured, advisory
Metadata.Contextschema defines where that knowledge belongs in the template, but CDK users have no way to author it. Since most production templates are CDK-synthesized, CDK needs a first-class authoring surface that emits the schema's frozen wire format.Description of changes
Two composable capabilities, following the feature-placement decision in
docs/AGENTS_CONSTRUCT_DESIGN.md(cross-cutting capability → aspect-backed facade + Mixin, not per-service L2 changes):MetadataContextfacade inaws-cdk-libcore (core/lib/metadata-context.ts, shared rendering/validation incore/lib/private/metadata-context-internal.ts) — mirrors theTags/RemovalPoliciespattern:MetadataContext.of(scope).add(props, options?)— resource-level context (why,must,mutable, sparsemutabilitymap,trust,ops,gaps,deps,failureModes). Cascades at synth time to primary resources (thedefaultChildchain), skipping incidental helper resources; nearest scope wins scalars, list fields union and de-duplicate. Options:applyToAllResources,includeResourceTypes/excludeResourceTypes, aspectpriority. Crosses stack boundaries intoNestedStacks, likeTags.MetadataContext.of(scope).addToTemplate(props)— template-level context (arch,must,refs,owner) merged into the enclosing stack's top-levelMetadata.ContextMutability,ContextTrustSource,ContextTrustConfidence; structsResourceContextProps,TemplateContextProps,ContextTrust,ContextRef. Trust defaults:AUTHORED/MEDIUM. ExplicitcfnResource.addMetadata('Context', ...)always wins over cascaded context.MetadataContextMixininaws-cdk-libcore (core/lib/mixins/metadata-context-mixin.ts) — imperative application to exactly the constructs you target via.with()orMixins.of(scope).apply(); delegates to the facade so both forms share one merge model. Exported flat fromaws-cdk-libbecause jsii forbids a top-levelmixinssubmodule (JSII5011 conflict with theMixinsclass); the awslintmixin-namespacerule is excluded for this one class inawslint.json(file-location and extends-base rules pass as-is).Docs: new "Metadata Context" section in the
aws-cdk-libREADME, covering both the facade and the Mixin.Describe any new or updated permissions being added
None. The change writes only to template
Metadatasections at synthesis time — no IAM policies, roles, grants, trust relationships, or resource policies are created or modified, and no runtime AWS calls are made.Description of how you validated changes
core/test/metadata-context.test.ts(rendering, cascade/merge semantics, primary-resource targeting incl. L2-styledefaultChildtrees and grouping constructs, NestedStack template + cascade behavior, filters, validation errors, wire-format conformance incl. a frozen-enum drift check); 6 tests for the mixin incore/test/mixins/metadata-context-mixin.test.ts. All assertions run against synthesized templates (toCloudFormationper core convention).Checklist
RFC aws/aws-cdk-rfcs#981
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license