Suppress cdk-nag findings for custom resource singleton lambda globally
This post describes how to suppress cdk-nag findings that are caused by the custom resource singleton Lambda function globally, so that findings which are out of your own control are not reported.
Overview
When using AWS CDK with custom resources, CDK creates a singleton Lambda function that handles all custom resource operations. This Lambda function can trigger several cdk-nag findings, even if you’re not directly using custom resources in your stack. This article explains how to properly suppress these findings globally.
Understanding the Problem
How CDK Uses Custom Resources
CDK uses a shared Lambda function to handle all custom resources in your stack. Here’s a typical example of creating a custom resource:
Generated CloudFormation Components
When CDK synthesizes this code, it creates several CloudFormation resources:
The custom resource itself
An IAM policy for the custom resource
An IAM role for the singleton Lambda function
The singleton Lambda function
This will be the linked resources if there a two custom resources in the stack.
cdk-nag Findings
This configuration triggers three main findings from cdk-nag:
Why Resource-Level Suppression Isn’t Enough
A simple resource-level suppression like this only handles the AwsSolutions-IAM5 finding:
This doesn’t work for the other findings because the singleton Lambda function is attached to the root of the stack and requires a different suppression approach.
The Solution
Overview
To properly suppress all findings related to the custom resource singleton Lambda, we need to:
Identify all possible paths where the custom resource creates resources
Check if these paths exist in our stack
Apply suppressions only to existing paths
Implementation
Here’s the complete solution:
Benefits
This solution:
Handles all cdk-nag findings related to the singleton Lambda
Works whether you’re directly using custom resources or not
Prevents errors from trying to suppress non-existent paths
Can be implemented centrally in your stack
Additional Resources
If you’re using a custom NagPack, you can find implementation examples here.