Lambda Vs EC2
Lambda Vs EC2
Amazon EC2:
● EC2 provides virtual machines (instances) in the cloud, allowing us to run
applications on a variety of operating systems.
● We have full control over the virtual machine, including the choice of the
operating system, software installations, and configurations.
● When implementing APIs on EC2, we typically deploy our application on the EC2
instance and use a web server (e.g., Apache, Nginx) or a framework (e.g., Flask,
Express) to handle HTTP requests and serve the API.
● We need to manage the infrastructure, including scaling, load balancing, and
maintaining the operating system and software.
AWS Lambda:
● Lambda is a serverless computing service where we upload the code, and AWS
automatically manages the execution environment.
● We don't need to worry about server provisioning, scaling, or maintenance.
Lambda automatically scales our application based on the number of requests.
● In Lambda, we create functions that are triggered by events. WE can use API
Gateway to create an HTTP endpoint that triggers our Lambda function.
● Lambda function should be designed to handle individual requests, and it should
be stateless.
Implementation Examples:
Setting up EC2:
● Install Express and a data storage library (e.g., maongodb) using npm install
express mongodb.
MongoDB Connection:
● In the EC2 implementation, we connect directly to the MongoDB instance
running on the EC2 instance or another accessible server.
● In the Lambda implementation, we should consider using a cloud-based
MongoDB service like MongoDB Atlas, as Lambda functions are stateless
and may not maintain persistent connections.
Error Handling:
Dependency Management:
● Ensure that all dependencies are packaged along with our Lambda
function when deploying to AWS.
Testing:
Invoke the Lambda Function through API Gateway:
● Copy the API Gateway endpoint URL.
● Use a tool like curl, Postman, or a browser to send a POST request to the
API Gateway endpoint, providing the required payload.
Check Lambda Logs (Optional):
● In the AWS Lambda console, we can check the CloudWatch logs for our
Lambda function to see any logs or errors generated during the API
Gateway invocation.
View API Gateway Metrics (Optional):
● In the API Gateway console, we can view metrics and monitoring
information related to the performance of our API.
BE Structure:
service:
name: our-service-name
provider:
name: aws
6
o
runtime: nodejs14.x
region: our-aws-region
functions:
getUserAccess:
events:
- http:
path: api/getProfile
method: post
cors: true
function2:
handler: functions/function2.lambdaHandler
function3:
handler: functions/function3.lambdaHandler
function25:
handler: functions/function25.lambdaHandler
plugins:
- serverless-webpack
package:
individually: true
Explanation:
● functions Section: Each Lambda function is defined with a unique name (e.g.,
getprofile, function2, ..., function25). Each function has its own handler property
pointing to the Lambda handler file.
● Lambda Handler Paths: The handler paths (functions/getUserAccess.lambdaHandler,
functions/function2.lambdaHandler, etc.) point to the file and exported handler for
each function. Ensure that the file paths are correct based on our project
structure.
● Configuration for Each Function: Inside each function block, we can add specific
configurations, events, and settings for that function. For instance, the events
section configures the API Gateway for the getUserAccess function.
● Repeat for Each Function: Repeat the pattern for each function.
8
o
The lambdaFunction.js file serves as the entry point for our AWS Lambda
function. It handles the integration with API Gateway, parses incoming events, and
invokes the appropriate controller. Below is a basic example of how might structure this
file will be: