How to Deploy AWS Lambda Using CDK: Simple Guide
To deploy a Lambda function using
AWS CDK, define a lambda.Function in your CDK stack with the runtime, handler, and code location. Then run cdk deploy to create the Lambda in your AWS account.Syntax
Use the lambda.Function construct from the AWS CDK library to define your Lambda function. You specify the runtime environment, the handler method, and the source code location.
- runtime: The language runtime, e.g., Node.js, Python.
- handler: The method AWS Lambda calls to start execution.
- code: The source code, usually from a local directory or inline.
javascript
const lambda = require('aws-cdk-lib/aws-lambda'); new lambda.Function(this, 'MyFunction', { runtime: lambda.Runtime.NODEJS_18_X, // runtime environment handler: 'index.handler', // file and exported function code: lambda.Code.fromAsset('lambda') // path to code });
Example
This example shows a complete CDK stack in JavaScript that deploys a simple Lambda function using Node.js 18 runtime. The Lambda code is stored in a local folder named lambda with an index.js file exporting a handler.
javascript
const cdk = require('aws-cdk-lib'); const lambda = require('aws-cdk-lib/aws-lambda'); class LambdaStack extends cdk.Stack { constructor(scope, id, props) { super(scope, id, props); new lambda.Function(this, 'MyLambdaFunction', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'index.handler', code: lambda.Code.fromAsset('lambda') }); } } const app = new cdk.App(); new LambdaStack(app, 'LambdaStack'); app.synth();
Output
Deploying stack LambdaStack...
✅ LambdaStack
Outputs:
(No outputs defined)
Common Pitfalls
Common mistakes when deploying Lambda with CDK include:
- Incorrect
handlerstring format (should befilename.exportedFunction). - Wrong
runtimeversion causing incompatibility. - Missing or incorrect code path in
code.fromAsset(). - Not running
cdk synthorcdk deployafter changes.
javascript
/* Wrong handler example */ new lambda.Function(this, 'BadHandler', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'app.main', // 'app' file missing or wrong export code: lambda.Code.fromAsset('lambda') }); /* Correct handler example */ new lambda.Function(this, 'GoodHandler', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'index.handler', // matches index.js export code: lambda.Code.fromAsset('lambda') });
Quick Reference
Remember these key points when deploying Lambda with CDK:
- Use
lambda.Functionconstruct. - Set
runtimeto your language version. - Specify
handlerasfile.exportedFunction. - Provide code with
lambda.Code.fromAsset()pointing to your code folder. - Run
cdk deployto create or update the Lambda.
Key Takeaways
Define your Lambda function using the lambda.Function construct with runtime, handler, and code.
Ensure the handler string matches your code file and exported function exactly.
Use lambda.Code.fromAsset() to point to your local Lambda code directory.
Run cdk deploy to create or update the Lambda function in AWS.
Check runtime compatibility and code path to avoid deployment errors.