Build your first serverless application using Amazon Web Services Lambda with step-by-step code examples and interactive demos.
Execute code without provisioning servers, paying only for execution time.
Triggers include HTTP requests, file uploads, database changes, and more.
Automatically scales with your application's event load.
Pay only for the compute time consumed ($0.20 per 1M requests).
exports.handler = async (event) => { console.log("EVENT:", JSON.stringify(event, null, 2)); return { statusCode: 200, body: JSON.stringify({ message: "Hello from Lambda!", input: event }) }; };
This is a basic Lambda function that logs incoming events and returns a JSON response. We'll add an API Gateway trigger in the next section to make it accessible via HTTP.
mkdir my-lambda-project
cd my-lambda-project
Create a new directory for your Lambda project. This will organize your source code, configuration files, and dependencies.
touch index.js
Create a main handler file with this basic function:
exports.handler = async (event) => { return { statusCode: 200, body: JSON.stringify({ hello: "world" }) }; };
npm install --save-dev aws-lambda-mock-context
Add this test code to index.js:
const context = require("aws-lambda-mock-context")(); require("./index").handler({}, context);
Run with node index.js
to simulate a Lambda execution.
npm install -g serverless
Create serverless.yml and deploy using:
serverless deploy
The CLI will create a new Lambda, IAM roles, and other required AWS resources.
aws.amazon.com
serverless.com
docs.aws.amazon.com/sam/latest
aws.amazon.com/api-gateway
aws.amazon.com/cloudformation
aws.amazon.com/blogs
Sign up for our AWS Lambda training workshops or access our complete course library with interactive labs.