Skip to content

feat(core): embed structured design context in synthesized templates (MetadataContext) - #38381

Open
satyakigh wants to merge 2 commits into
aws:mainfrom
satyakigh:context-constructs
Open

feat(core): embed structured design context in synthesized templates (MetadataContext)#38381
satyakigh wants to merge 2 commits into
aws:mainfrom
satyakigh:context-constructs

Conversation

@satyakigh

@satyakigh satyakigh commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Issue # (if applicable)

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.Context schema 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):

MetadataContext facade in aws-cdk-lib core (core/lib/metadata-context.ts, shared rendering/validation in core/lib/private/metadata-context-internal.ts) — mirrors the Tags/RemovalPolicies pattern:

  • MetadataContext.of(scope).add(props, options?) — resource-level context (why, must, mutable, sparse mutability map, trust, ops, gaps, deps, failureModes). Cascades at synth time to primary resources (the defaultChild chain), skipping incidental helper resources; nearest scope wins scalars, list fields union and de-duplicate. Options: applyToAllResources, includeResourceTypes/excludeResourceTypes, aspect priority. Crosses stack boundaries into NestedStacks, like Tags.
  • MetadataContext.of(scope).addToTemplate(props) — template-level context (arch, must, refs, owner) merged into the enclosing stack's top-level Metadata.
  • New enums ContextMutability, ContextTrustSource, ContextTrustConfidence; structs ResourceContextProps, TemplateContextProps, ContextTrust, ContextRef. Trust defaults: AUTHORED/MEDIUM. Explicit cfnResource.addMetadata('Context', ...) always wins over cascaded context.

MetadataContextMixin in aws-cdk-lib core (core/lib/mixins/metadata-context-mixin.ts) — imperative application to exactly the constructs you target via .with() or Mixins.of(scope).apply(); delegates to the facade so both forms share one merge model. Exported flat from aws-cdk-lib because jsii forbids a top-level mixins submodule (JSII5011 conflict with the Mixins class); the awslint mixin-namespace rule is excluded for this one class in awslint.json (file-location and extends-base rules pass as-is).

Docs: new "Metadata Context" section in the aws-cdk-lib README, covering both the facade and the Mixin.

Describe any new or updated permissions being added

None. The change writes only to template Metadata sections 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

  • Unit tests: new tests in core/test/metadata-context.test.ts (rendering, cascade/merge semantics, primary-resource targeting incl. L2-style defaultChild trees and grouping constructs, NestedStack template + cascade behavior, filters, validation errors, wire-format conformance incl. a frozen-enum drift check); 6 tests for the mixin in core/test/mixins/metadata-context-mixin.test.ts. All assertions run against synthesized templates (toCloudFormation per core convention).

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@satyakigh
satyakigh requested a review from a team as a code owner July 22, 2026 23:46
@github-actions github-actions Bot added p2 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels Jul 22, 2026
@aws-cdk-automation
aws-cdk-automation requested a review from a team July 22, 2026 23:47
@github-actions

Copy link
Copy Markdown
Contributor

👋 It looks like your PR description follows the template but is missing a valid issue number in the first section.

PRs without a linked issue will receive lower priority for review and merging. Please update the description to include a reference like Closes #123. If no existing issue matches your change, create one first.

@aws-cdk-automation aws-cdk-automation left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@satyakigh satyakigh changed the title feat(core): Add MetadataContext for structured template context feat(core): add MetadataContext for structured template context Jul 22, 2026
@aws-cdk-automation
aws-cdk-automation dismissed their stale review July 22, 2026 23:51

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@satyakigh
satyakigh force-pushed the context-constructs branch from 8da308b to 517cab1 Compare July 22, 2026 23:51

@mrgrain mrgrain left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mergify
mergify Bot dismissed mrgrain’s stale review July 23, 2026 04:10

Pull request has been modified.

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jul 23, 2026
@satyakigh
satyakigh force-pushed the context-constructs branch from b362a0a to b6106b6 Compare July 23, 2026 14:28
@satyakigh

Copy link
Copy Markdown
Contributor Author

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.

Sounds good, updated

@satyakigh
satyakigh force-pushed the context-constructs branch from b6106b6 to 5caa8fa Compare July 26, 2026 19:47
@satyakigh
satyakigh force-pushed the context-constructs branch from 5caa8fa to 553ae1b Compare July 27, 2026 14:44
@satyakigh satyakigh changed the title feat(core): add MetadataContext for structured template context feat(core): embed structured design context in synthesized templates (MetadataContext) Jul 30, 2026
@satyakigh
satyakigh force-pushed the context-constructs branch from 553ae1b to 64700e1 Compare July 31, 2026 04:10
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK p2 pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants