Skip to content

anish-u/aws-lambda-layer-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AWS Lambda Logging Layer with CDK (TypeScript)

This project demonstrates how to build and deploy a reusable logging layer for AWS Lambda using TypeScript, AWS CDK, and Node.js Lambda Layers. It includes two Lambda functions (createBlog, getBlogs) that use the shared logging logic via a Lambda Layer.


πŸ”§ Features

  • Reusable Lambda Layer for logging and error tracking
  • Higher-order function wrapper (withLogger) for structured logs
  • Type-safe using AWS Lambda types (APIGatewayProxyEvent, etc.)
  • CDK-defined infrastructure (Lambdas, Layer, API Gateway)
  • Clean, scalable project structure

πŸ“ Project Structure

.
β”œβ”€β”€ bin/                            # CDK app entry point
β”‚   └── aws-lambda-layer-logger.ts
β”œβ”€β”€ stacks/                         # CDK Stack
β”‚   └── aws-lambda-layer-logger-stack.ts
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ lambdas/                    # Lambda functions
β”‚   β”‚   β”œβ”€β”€ createBlog.ts
β”‚   β”‚   └── getBlogs.ts
β”‚   └── layers/
β”‚       └── logger/                 # Shared logging Layer
β”‚           β”œβ”€β”€ logger.ts
β”‚           β”œβ”€β”€ withLogger.ts
β”‚           β”œβ”€β”€ package.json
β”‚           └── tsconfig.json
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
└── cdk.json

πŸš€ Usage

1. Install dependencies

npm install

2. Build the logging layer (from src/layers/logger/)

cd src/layers/logger
npm install     # If using dependencies
npm run build   # Compile TypeScript to JS (outputs to dist/)

3. Deploy the stack

# Run this in the root project directory
cdk deploy

✨ Example: Logging Wrapper

export const withLogging = (handler) => async (event, context) => {
  logger.info("Lambda Invoked", { ... });
  try {
    const result = await handler(event, context);
    logger.info("Lambda Succeeded", { statusCode: result.statusCode });
    return result;
  } catch (error) {
    logger.error("Lambda Failed", { error });
    throw error;
  }
};

Use it in your handler like:

export const handler = withLogging(async (event) => { ... });

πŸ§ͺ Endpoints

Method Path Lambda Description
GET /blog getBlogs Returns a list of blogs
POST /blog createBlog Creates a new blog entry

πŸ“– Logs in CloudWatch

Structured logs include context:

{
  "level": "info",
  "msg": "Lambda Invoked",
  "requestId": "abc-123",
  "functionName": "CreateBlogFunction",
  "path": "/blog",
  "httpMethod": "POST"
}

🧠 Why Use a Lambda Layer for Logging?

  • Centralized, DRY logging logic
  • Consistent structure across all functions
  • Easier debugging and log querying
  • Cleaner business logic in handlers

About

Learn how to build a reusable logging system for AWS Lambda using Layers, TypeScript, and CDK. DRY your code and improve serverless observability.

Topics

Resources

Stars

Watchers

Forks

Contributors