CDK Aspect Integration
Enhance your CDK analysis with the CDK Insights Aspect. Get deeper insights, source location mapping, and CDK-specific recommendations.
What is the CDK Aspect?
The CDK Insights Aspect is a CDK construct that integrates directly with your CDK application to provide enhanced analysis capabilities. Unlike basic analysis that only examines the generated CloudFormation templates, the aspect provides:
- Direct access to your CDK constructs and their properties
- Source location mapping (exact file and line numbers)
- CDK-specific best practices and patterns
- Context-aware recommendations
- Reduced false positives through construct understanding
Key Benefits
Source Location Mapping
Pinpoint exactly where issues occur in your CDK code
Enhanced Analysis
Deeper insights into your CDK constructs and patterns
Better Security
More accurate security analysis with construct context
Basic vs Enhanced Analysis
| Feature | Basic Analysis | Enhanced Analysis |
|---|---|---|
| Source Location | Limited (CloudFormation resource only) | โ Exact file path, line, and column |
| Fallback Location | None | โ Parent construct location when direct unavailable |
| Construct Analysis | Basic CloudFormation analysis | โ CDK construct-specific insights |
| Issue Context | Generic recommendations | โ Context-aware suggestions with createdBy path |
| CDK Best Practices | Limited CDK-specific rules | โ Comprehensive CDK patterns |
Installation & Setup
Quickest path: cdk-insights setup
The interactive setup command installs dependencies, inserts the right code into your CDK app, and writes "@aws-cdk/core:stackTrace": true into your cdk.json context so every cdk synth captures creation stacks for accurate source-location attribution โ no CDK_DEBUG=true dance per shell. It detects your aws-cdk-lib version and offers the Validations plugin path (synth-time enforcement) on v2.251.0+ alongside the aspect. Re-run any time; the cdk.json patch is idempotent.
npx cdk-insights setupOr wire it up manually:
Install CDK Insights
Add CDK Insights to your project
npm install --save-dev cdk-insightsImport the Aspect
Import the CDK Insights aspect factory in your app
import { createCdkInsightsAspect } from 'cdk-insights';
import { Aspects } from 'aws-cdk-lib';Add to your App
Apply the aspect to your CDK app
const app = new cdk.App();
// Add CDK Insights aspect for enhanced analysis
Aspects.of(app).add(createCdkInsightsAspect());Run Analysis
Run CDK Insights as usual
npx cdk-insights scanPair with the Validations Plugin (synth-time enforcement)
On aws-cdk-lib โฅ 2.251.0 you can also register CDK Insights as a native CDK Validations plugin. cdk synth will then fail with a CDK Insights report if any rules trigger โ useful for blocking deploys in CI without an extra step.
The plugin and the aspect are complementary, not alternatives: the aspect captures source locations for richer findings, the plugin enforces them at synth time. Most projects want both.
import { App, Validations } from 'aws-cdk-lib';
import { CdkInsightsPolicyValidationPlugin } from 'cdk-insights';
const app = new App();
Validations.of(app).addPlugins(new CdkInsightsPolicyValidationPlugin());Scope it with selectedServices, minimumSeverity, or customRules if you only want a subset of findings to block synth.
The plugin honours the same suppressions as cdk-insights scan โ .cdk-insights.json (ignoreRules / ignorePaths) and inline Validations.of(scope).acknowledge(...) entries โ so findings suppressed in the CLI are also suppressed at synth time.
cdk-nag integration (without blocking deploys)
The CDK Insights aspect captures cdk-nag findings into the cdk-insights findings stream without letting cdk-nag's default Annotations.addError abort your synth. You get the full breadth of cdk-nag rule packs as part of your cdk-insights report, and your CI can decide for itself which severities to fail on (via --failOnCritical).
You do not need to install cdk-nag separately. CDK Insights bundles cdk-nag at runtime, so the AwsSolutions-* rule pack and suppressNagRules work out of the box once you add the aspect. Only install cdk-nag directly if you also want to call NagSuppressions.addResourceSuppressions(...) yourself for per-resource suppressions.
cdk-nag severity is preserved (NagMessageLevel.ERROR โ HIGH, WARN โ MEDIUM), with a curated CRITICAL override list for genuinely deploy-blocking rules.
Suppress common boilerplate noise
cdkBoilerplateSuppressions: true auto-suppresses cdk-nag findings that consistently fire on AWS-recommended boilerplate (managed policies like AwsSolutions-IAM4, current LTS Lambda runtimes via AwsSolutions-L1). The list updates with each cdk-insights release. Anything not in the curated list continues to surface normally.
Aspects.of(app).add(createCdkInsightsAspect({
cdkBoilerplateSuppressions: true,
}));Project-specific suppressions
suppressNagRules takes an array of cdk-nag rule IDs to suppress globally. Two forms โ string shorthand for quick triage, object form (with a written reason) for checked-in code:
Aspects.of(app).add(createCdkInsightsAspect({
cdkBoilerplateSuppressions: true,
suppressNagRules: [
// String shorthand โ generic auto-reason, fine for triage
'AwsSolutions-APIG2',
// Object form โ written justification, recommended for prod
{
id: 'AwsSolutions-COG2',
reason: 'MFA is opt-in by product policy; see RFC-0042.',
},
],
}));Both options compose โ enable both simultaneously. For finer-grained control (per-resource or per-appliesTo-pattern), call NagSuppressions.addResourceSuppressions(...) from cdk-nag directly after constructing your stacks.
Aspects vs. Mixins
aws-cdk-lib ships two visitor-style APIs that both mutate constructs: Aspects (declarative, applied at synth) and Mixins (imperative, applied immediately at .with() call time). They're complementary, not competitors.
For CDK Insights this matters in one direction only: the CDK Insights aspect runs at synth, after every mixin has already been applied. So the aspect always sees the final post-mixin state of your constructs โ there is no ordering footgun, no "did the mixin happen yet?" question. Findings are attributed to applied mixins automatically via the aws:cdk:analytics:mixin manifest stream.
See the CDK Mixins page for how cdk-insights detects mixin-specific footguns at template level.
Output Comparison
Basic Analysis
Enhanced Analysis
Best Practices
Add to All Environments
Include the aspect in all your CDK apps (dev, staging, production) for consistent analysis.
Use in CI/CD Pipelines
Integrate CDK Insights with aspect into your CI/CD pipeline for automated analysis.
Regular Analysis
Run analysis regularly to catch issues early and maintain code quality.
Ready to Enhance Your Analysis?
Add the CDK Insights Aspect to your project for deeper insights and better recommendations.