Inspector Lambda Code Scanning Cost Control

Amazon Inspector is a powerful tool for analysing Lambda functions for security vulnerabilities. However, running scans every day quickly becomes expensive in a learning environment.



Problem: Constantly Scanning

Inspector will scan your Lambda functions often:

  • As soon as Amazon Inspector discovers an existing Lambda function.
  • When you deploy a new Lambda function to the Lambda service.
  • When you deploy an update to the application code or dependencies of an existing Lambda function or its layers.
  • Whenever Amazon Inspector adds a new common vulnerabilities and exposures (CVE) item to its database, and that CVE is relevant to your function.


This is fantastic for enterprise environments, but it can quickly become a large proportion of a lab's cost.




Solution: Scheduled Exclusion Tags

To balance between security insight and cost control, I built a simple tagging scheduler using EventBridge and Lambda:

  • 1st of the month: Remove Inspector Code Scan and Standard Scan exclusion tags from all functions in the account
  • 3rd of the month: Add Inspector scan exclusion tags to all Lambda functions in the account

You can control whether any function is excluded from this process, and you can set your own schedules if you want to run it weekly.



Code Summary

Node.js code is available here.

The core Lambda function does the following:

  1. Initialises an AWS Lambda client (uses SSO if running locally).
  2. Gets all Lambda functions in the account.
  3. For each function:
    1. Skips if it has a suppression opt-out tag (InspectorSuppressorExclusion).
    2. Adds or removes tags based on scanning instructions in the EventBridge event.
  4. Logs the result (updated, skipped, or errored).


CloudFormation Resources

CloudFormation is available here.

  • Lambda Function
    • Runs the tagging logic.
    • Node.js 22.x, with X-Ray tracing.
  • EventBridge Rules
    • enable-scanning: 1st of each month at 5 am
    • disable-scanning: 3rd of each month at 5 am
  • IAM Role
    • Grants Lambda permission to list, tag, and untag Lambda functions.
  • CloudWatch Log Group
    • Custom log group with a 7-day retention policy.