🔥 What is AWS Lambda?
AWS Lambda is a serverless compute service that lets you run code without managing servers. You simply upload your code, and Lambda automatically handles the infrastructure, scaling, and execution.
🛠️ Benefits of Using Serverless Framework
The Serverless Framework is a powerful open-source tool that simplifies the process of creating and managing AWS Lambda functions.
✅ Key Advantages
- Simplifies Deployment: Easily deploy Lambda functions with simple commands.
-
Infrastructure as Code: Use the
serverless.yml
file to define infrastructure and configuration. - Supports Multiple Providers: AWS, Azure, Google Cloud, etc.
- Built-in Support: Manages API Gateway, DynamoDB, S3, and other AWS services.
🔥 1. Install Serverless Framework
First, install the Serverless CLI globally using npm
:
npm install -g serverless
Verify the installation:
serverless --version
🛠️ 2. Create a New Serverless Project
Generate a new Serverless project with AWS as the provider:
serverless create --template aws-nodejs --path my-lambda-function
-
aws-nodejs
→ AWS Lambda with Node.js runtime. -
my-lambda-function
→ Your project folder name.
Go into the project directory:
cd my-lambda-function
🔧 3. Install Dependencies
Install the necessary dependencies:
npm install
🌐 4. Configure AWS Credentials
If you haven’t already, configure your AWS credentials:
serverless config credentials --provider aws --key --secret
✅ Get your access key and secret key from AWS IAM.
🛠️ 5. Edit serverless.yml
Configuration
Open the serverless.yml
file and define your Lambda function:
service: my-lambda-service
provider:
name: aws
runtime: nodejs18.x
region: us-east-1
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
✅ Explanation:
-
service
: Name of your Lambda service. -
provider
: AWS settings, including the region and runtime. -
functions
: The Lambda function definition. -
events
: Triggering the Lambda through an HTTP request.
🔥 6. Write the Lambda Function
Open the handler.js
file and add the following code:
module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify({
message: "Hello from Lambda!",
input: event,
}),
};
};
✅ This function returns a simple JSON response.
🚀 7. Deploy the Lambda Function
Deploy your Lambda function to AWS:
serverless deploy
Once deployed, you’ll see the API Gateway endpoint:
Service deployed to: https://.execute-api..amazonaws.com/dev/hello
🌐 8. Test the Lambda Function
You can test the function using:
curl https://.execute-api..amazonaws.com/dev/hello
Or:
serverless invoke -f hello
⚙️ 9. Cleaning Up
To remove the Lambda function and free up resources:
serverless remove
✅ Key Takeaways
- Serverless Framework simplifies AWS Lambda deployment.
- Automatic Scaling: No need to manage infrastructure.
- Cost-Efficient: You only pay for what you use.
- Quick Deployment: Easily create and deploy Lambda functions with minimal configuration.
🚀 Next Steps
- Add more Lambda functions.
- Integrate with AWS services like S3, DynamoDB, and API Gateway.
- Optimize and secure your serverless functions.