-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guidance: NagSuppressions on cdk created stacks #1306
Comments
I modified the reproduction code a bit and the following worked for me import * as cdk from 'aws-cdk-lib';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { AwsSolutionsChecks, NagSuppressions } from 'cdk-nag';
export class EdgeStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: cdk.StackProps) {
super(scope, id, props);
new cloudfront.experimental.EdgeFunction(this, 'EdgeOriginResponseFn', {
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'index.handler',
code: lambda.Code.fromInline('foo'),
});
NagSuppressions.addResourceSuppressionsByPath(
this,
`/${this.stackName}/EdgeOriginResponseFn/Fn/ServiceRole/Resource`,
[
{
id: 'AwsSolutions-IAM4',
reason: 'CDK managed resource',
appliesTo: ['Policy::arn:<AWS::Partition>:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'],
},
]
);
}
}
const app = new cdk.App();
new EdgeStack(app, 'test-edge-stack', {env: {region: 'us-east-1'}});
cdk.Aspects.of(app).add(new AwsSolutionsChecks());
app.synth(); |
Are you creating multiple instances of the Stack with different |
Hi @dontirun! Thanks for your quick response. I can see where the problem is. I didn't force the region to be |
As you noted, the cdk creates a separate stack for certain resources when deployed in import * as cdk from 'aws-cdk-lib';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { AwsSolutionsChecks, NagSuppressions } from 'cdk-nag';
export class EdgeStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: cdk.StackProps) {
super(scope, id, props);
new cloudfront.experimental.EdgeFunction(this, 'EdgeOriginResponseFn', {
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'index.handler',
code: lambda.Code.fromInline('foo'),
});
}
}
const app = new cdk.App();
new EdgeStack(app, 'test-edge-stack', { env: { region: 'eu-central-1' } });
cdk.Aspects.of(app).add(new AwsSolutionsChecks());
// Lookup the cdk created edge stack. Replace the application unique identifier (c81a4702a85c4956cd38c09bdc5df69ed818cefb38) with your own
const cdkEdgeStack = app.node.findChild('edge-lambda-stack-c81a4702a85c4956cd38c09bdc5df69ed818cefb38') as cdk.Stack;
NagSuppressions.addResourceSuppressionsByPath(
cdkEdgeStack,
`/${cdkEdgeStack.stackName}/EdgeOriginResponseFn/ServiceRole/Resource`,
[
{
id: 'AwsSolutions-IAM4',
reason: 'CDK managed resource',
appliesTo: ['Policy::arn:<AWS::Partition>:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'],
},
],
);
app.synth(); |
Thanks @dontirun a lot! That solves my problem. |
Using the root application didn't work for me but |
What is the problem?
I have a simple stack:
Trying to deploy it I got:
I've added:
but the path is not found properly. I got error:
I'm not able to suppress this rule.
Reproduction Steps
Full code to reproduce:
What did you expect to happen?
Nag is suppresed.
What actually happened?
Suppression path not found even it is defined.
cdk-nag version
2.27.12
Language
Typescript
Other information
No response
The text was updated successfully, but these errors were encountered: