AWS Lambda with Serverless Framework
Build and deploy serverless applications efficiently.
Project Setup
npm install -g serverless
serverless create --template aws-nodejs --path my-service
cd my-service
npm init -y
serverless.yml Configuration
service: my-api
provider:
name: aws
runtime: nodejs18.x
region: us-east-1
environment:
TABLE_NAME: ${self:service}-${sls:stage}
functions:
getUsers:
handler: handlers/users.get
events:
- http:
path: users
method: get
cors: true
createUser:
handler: handlers/users.create
events:
- http:
path: users
method: post
resources:
Resources:
UsersTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:provider.environment.TABLE_NAME}
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
Handler Function
// handlers/users.js
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient();
module.exports.get = async (event) => {
const params = { TableName: process.env.TABLE_NAME };
const result = await dynamo.scan(params).promise();
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(result.Items),
};
};
Cold Start Optimization
// Keep functions warm
functions:
myFunction:
handler: handler.main
warmup:
enabled: true
events:
- schedule: rate(5 minutes)
Serverless architecture reduces operational overhead and scales automatically.
Comments (0)
Leave a Comment
No comments yet. Be the first to share your thoughts!