Cdklabs.CdkNag
3.1.0
dotnet add package Cdklabs.CdkNag --version 3.1.0
NuGet\Install-Package Cdklabs.CdkNag -Version 3.1.0
<PackageReference Include="Cdklabs.CdkNag" Version="3.1.0" />
<PackageVersion Include="Cdklabs.CdkNag" Version="3.1.0" />
<PackageReference Include="Cdklabs.CdkNag" />
paket add Cdklabs.CdkNag --version 3.1.0
#r "nuget: Cdklabs.CdkNag, 3.1.0"
#:package Cdklabs.CdkNag@3.1.0
#addin nuget:?package=Cdklabs.CdkNag&version=3.1.0
#tool nuget:?package=Cdklabs.CdkNag&version=3.1.0
cdk-nag
Check CDK applications or CloudFormation templates for best practices using a combination of available rule packs. Inspired by cfn_nag.
Check out this blog post for a guided overview!
Available Rules and Packs
See RULES for more information on all the available packs.
RULES also includes a collection of additional rules that are not currently included in any of the pre-built NagPacks, but are still available for inclusion in custom NagPacks.
Read the NagPack developer docs if you are interested in creating your own pack.
Usage
For a full list of options See NagPackProps in the API.md
<details> <summary>Including in an application</summary>
using Amazon.CDK;
using Cdklabs.CdkNag;
var CdkTestStack;
var app = new App();
new CdkTestStack(app, "CdkNagDemo");
// Simple rule informational messages using the AWS Solutions Rule pack
Validations.Of(app).AddPlugins(new AwsSolutionsChecks(app));
// Multiple rule packs can be run against the same app
Validations.Of(app).AddPlugins(new NIST80053R5Checks(app));
</details>
Acknowledging a Rule
Use CDK's native Validations.of() API to acknowledge (suppress) rule violations on specific constructs.
<details> <summary>Example 1) Acknowledging a rule on a construct</summary>
using Amazon.CDK.AWS.EC2;
using Amazon.CDK;
using Constructs;
class CdkTestStack : Stack
{
public CdkTestStack(Construct scope, string id, StackProps? props=null) : base(scope, id, props)
{
var test = new SecurityGroup(this, "test", new SecurityGroupProps {
Vpc = new Vpc(this, "vpc")
});
test.AddIngressRule(Peer.AnyIpv4(), Port.AllTraffic());
Validations.Of(test).Acknowledge(new Acknowledgment {
Id = "AwsSolutions-EC23",
Reason = "This security group is used for internal testing only."
});
}
}
</details><details> <summary>Example 2) Acknowledging a rule on a stack</summary>
using Amazon.CDK;
using Cdklabs.CdkNag;
var CdkTestStack;
var app = new App();
var stack = new CdkTestStack(app, "CdkNagDemo");
Validations.Of(app).AddPlugins(new AwsSolutionsChecks(app));
Validations.Of(stack).Acknowledge(new Acknowledgment {
Id = "AwsSolutions-EC23",
Reason = "All security groups in this stack are internal only."
});
</details><details> <summary>Example 3) Acknowledging a specific finding</summary>
Certain rules report multiple findings per resource (e.g., IAM wildcard permissions). Each finding has its own ID in the format RuleId[FindingId].
If you received the following errors on synth/deploy:
[Error at /StackName/rUser/DefaultPolicy/Resource] AwsSolutions-IAM5[Action::s3:*]: The IAM entity contains wildcard permissions.
[Error at /StackName/rUser/DefaultPolicy/Resource] AwsSolutions-IAM5[Resource::*]: The IAM entity contains wildcard permissions.
You can acknowledge a specific finding:
using Amazon.CDK.AWS.IAM;
using Amazon.CDK;
using Constructs;
class CdkTestStack : Stack
{
public CdkTestStack(Construct scope, string id, StackProps? props=null) : base(scope, id, props)
{
var user = new User(this, "rUser");
user.AddToPolicy(
new PolicyStatement(new PolicyStatementProps {
Actions = new [] { "s3:*" },
Resources = new [] { "*" }
}));
// Only acknowledge the s3:* action — Resource::* still triggers
Validations.Of(user).Acknowledge(new Acknowledgment {
Id = "AwsSolutions-IAM5[Action::s3:*]",
Reason = "Need s3:* for cross-account replication."
});
}
}
Alternatively, acknowledging the rule by its base ID (e.g., AwsSolutions-IAM5, without a [FindingId] suffix) suppresses all findings for that rule on the construct.
</details>
Rules and Property Overrides
In some cases L2 Constructs do not have a native option to remediate an issue and must be fixed via Raw Overrides. Since raw overrides take place after template synthesis these fixes are not caught by cdk-nag. In this case you should remediate the issue and acknowledge the rule.
<details> <summary>Example) Property Overrides</summary>
using Amazon.CDK.AWS.EC2;
using Amazon.CDK;
using Constructs;
class CdkTestStack : Stack
{
public CdkTestStack(Construct scope, string id, StackProps? props=null) : base(scope, id, props)
{
var instance = new Instance(this, "rInstance", new InstanceProps {
Vpc = new Vpc(this, "rVpc"),
InstanceType = new InstanceType(InstanceClass.T3),
MachineImage = MachineImage.LatestAmazonLinux()
});
var cfnIns = (CfnInstance)instance.Node.DefaultChild;
cfnIns.AddPropertyOverride("DisableApiTermination", true);
Validations.Of(instance).Acknowledge(new Acknowledgment {
Id = "AwsSolutions-EC29",
Reason = "Remediated through property override."
});
}
}
</details>
Audit Trail: CloudFormation Metadata
By default, cdk-nag writes violations to CDK's policy-validation-report.json in the cloud assembly. If you need the v2-compatible cdk_nag metadata block in your synthesized CloudFormation templates (for existing compliance tooling), enable writeSuppressionsToCloudFormation:
using Amazon.CDK;
using Cdklabs.CdkNag;
var app = new App();
// Writes acknowledged rules into CfnResource Metadata as cdk_nag: { rules_to_suppress: [...] }
Validations.Of(app).AddPlugins(new AwsSolutionsChecks(app, new NagPackProps { WriteSuppressionsToCloudFormation = true }));
This registers a WriteNagSuppressionsToCloudFormationAspect that runs during synthesis and copies Validations.of().acknowledge() data into the CloudFormation template Metadata section, preserving the same format as cdk-nag v2.
Using on CloudFormation templates
You can use cdk-nag on existing CloudFormation templates by using the cloudformation-include module.
<details> <summary>Example) CloudFormation template</summary>
Sample App
using Amazon.CDK;
using Cdklabs.CdkNag;
var CdkTestStack;
var app = new App();
new CdkTestStack(app, "CdkNagDemo");
Validations.Of(app).AddPlugins(new AwsSolutionsChecks(app));
Sample Stack with imported template
using Amazon.CDK.CloudFormation.Include;
using Amazon.CDK;
using Constructs;
class CdkTestStack : Stack
{
public CdkTestStack(Construct scope, string id, StackProps? props=null) : base(scope, id, props)
{
var template = new CfnInclude(this, "Template", new CfnIncludeProps {
TemplateFile = "my-template.json"
});
// Acknowledge rules on imported resources
var bucket = template.GetResource("rBucket");
Validations.Of(bucket).Acknowledge(new Acknowledgment {
Id = "AwsSolutions-S1",
Reason = "Logging not required for this bucket."
});
}
}
</details>
Migrating from v2
cdk-nag v3 replaces the custom NagSuppressions API with CDK's native Validations.of().acknowledge() mechanism.
| v2 | v3 |
|---|---|
NagSuppressions.addResourceSuppressions(construct, [{ id, reason }]) |
Validations.of(construct).acknowledge({ id, reason }) |
NagSuppressions.addStackSuppressions(stack, [{ id, reason }]) |
Validations.of(stack).acknowledge({ id, reason }) |
NagSuppressions.addResourceSuppressionsByPath(stack, path, [...]) |
Validations.of(construct).acknowledge({ id, reason }) |
appliesTo: ['Action::s3:*'] |
id: 'AwsSolutions-IAM5[Action::s3:*]' |
{ id: 'CdkNagValidationFailure', reason: '...' } |
Validations.of(construct).acknowledge({ id: 'RuleId', reason: '...' }) |
Note on bulk suppression: As in v2, acknowledging a rule by its base ID (e.g., AwsSolutions-IAM5) without a [FindingId] suffix suppresses all granular findings for that rule on the construct. Use granular IDs like AwsSolutions-IAM5[Action::s3:*] if you only want to acknowledge a specific finding.
Removed APIs:
NagSuppressions(useValidations.of().acknowledge())INagSuppressionIgnoreand all condition classesNagPackSuppressioninterfaceCdkNagValidationFailureconceptlogIgnoresandsuppressionIgnoreConditionprops
Contributing
See CONTRIBUTING for more information.
License
This project is licensed under the Apache-2.0 License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Amazon.CDK.Lib (>= 2.257.0 && < 3.0.0)
- Amazon.JSII.Runtime (>= 1.143.0 && < 2.0.0)
- Constructs (>= 10.5.1 && < 11.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Cdklabs.CdkNag:
| Package | Downloads |
|---|---|
|
Cdklabs.GenerativeAiCdkConstructs
AWS Generative AI CDK Constructs is a library for well-architected generative AI patterns. (Stability: Experimental) |
|
|
Cdklabs.CdkCicdWrapper
This repository contains the infrastructure as code to wrap your AWS CDK project with CI/CD around it. (Stability: Experimental) |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.1.0 | 7,666 | 11/20/2026 |
| 3.0.2 | 53,445 | 8/4/2026 |
| 3.0.1 | 11,733 | 6/15/2026 |
| 3.0.0 | 235 | 6/12/2026 |
| 2.38.2 | 68,047 | 4/27/2026 |
| 2.38.1 | 1,922 | 4/21/2026 |
| 2.38.0 | 210 | 4/21/2026 |
| 2.37.56 | 238 | 4/20/2026 |
| 2.37.55 | 165,540 | 10/17/2025 |
| 2.37.54 | 517 | 10/16/2025 |
| 2.37.53 | 783 | 10/15/2025 |
| 2.37.52 | 496 | 10/14/2025 |
| 2.37.51 | 882 | 10/12/2025 |
| 2.37.50 | 280 | 10/11/2025 |
| 2.37.49 | 366 | 10/10/2025 |
| 2.37.48 | 484 | 10/9/2025 |
| 2.37.47 | 1,104 | 10/8/2025 |
| 2.37.46 | 373 | 10/7/2025 |
| 2.37.45 | 401 | 10/6/2025 |
| 2.37.44 | 359 | 10/5/2025 |