In this article, we will learn how we can create an application without managing the underlying server using AWS Lambda.
Let’s first understand what is AWS Lambda.
AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS) that allows you to run code without provisioning or managing servers. With Lambda, you can execute your code in response to events such as changes in data, HTTP requests and you only pay for the compute time consumed by your code.
Key features of AWS Lambda :
Serverless Computing: Lambda abstracts away the underlying infrastructure, so you can focus on writing code without worrying about managing servers, scaling, or maintenance tasks.
Event-Driven Architecture: Lambda functions can be triggered by various AWS services such as Amazon S3, Amazon DynamoDB, Amazon API Gateway, Amazon SNS, Amazon SQS, etc., as well as custom events via AWS SDKs.
Auto-Scaling: Lambda automatically scales the execution of your code in response to incoming events. It can handle a few requests per minute to thousands of requests per second.
Pay-Per-Use Pricing: With Lambda, you only pay for the compute time consumed by your code. There is no charge when your code is not running.
Support for Multiple Runtimes: Lambda supports multiple programming languages and runtimes including Node.js, Python, Java, Go, .NET Core, and custom runtimes using the Lambda Runtime API.
Integrated Monitoring and Logging: AWS Lambda integrates with AWS CloudWatch, allowing you to monitor and log your Lambda functions, track performance metrics, and troubleshoot issues.
Stateless Execution: Each invocation of a Lambda function is independent and stateless. If you need to maintain state between invocations, you can use external data stores like Amazon DynamoDB or Amazon S3.
Now let’s get our hands dirty:
1. Navigate to the Lambda Service:
2. Create Lambda Function
Click on create a function button
Creating a function using a blueprint
Function name: hello-world, Create a role: lamba_basic_execution (create an IAM role with the necessary permissions that AWS Lambda can assume to invoke your Lambda function on your behalf)
Lambda function code:
console.log('Loading function');
export const handler = async (event, context) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
console.log('value1 =', event.key1);
console.log('value2 =', event.key2);
console.log('value3 =', event.key3);
return event.key1; // Echo back the first key value
// throw new Error('Something went wrong');
};
Now click on create function
3. Reviewing Lambda settings:
Runtime: You can write your Lambda function code in Java, Node.js, C#, Go, or Python. For this article, we are using Node.js 18.x as the runtime.
Handler: You can specify a handler (a method/function in your code) where AWS Lambda can begin executing your code. AWS Lambda provides event data as input to this handler, which processes the event.
4. Invoke Lambda Function i.e Hello-world
Click on test → configure test event
Now click on Test, the execution result tab will verify if the execution succeeded. it returned with the response hello, world!!
4. Monitoring Metrics
AWS Lambda automatically monitors Lambda functions and generates metrics using Amazon CloudWatch. This service assists in monitoring your code execution by tracking various metrics such as request count, request latency, and error count.
5. Deleting hello-world lambda function
confirming the deletion of our lambda function
In conclusion, exploring AWS Lambda has been an enlightening journey, showcasing the power and simplicity of serverless computing. Learning how to run our applications on Lambda without the hassle of provisioning and configuring servers has been both educational and exciting. With Lambda automatically scaling our applications by executing code in response to triggers, we can confidently deploy and manage workloads of any size without worrying about infrastructure management. Here’s to embracing the efficiency and scalability of serverless computing with AWS Lambda!
Special shoutout to AWS for curating an exceptional step-by-step tutorial on AWS Lambda https://aws.amazon.com/tutorials/run-serverless-code/. This resource provided invaluable insights and hands-on experience with AWS Lambda. I also extend a special thank you to Greg Powell https://www.linkedin.com/in/thoughtfultechy/ for sharing this AWS Labs tutorial on his LinkedIn post.
Happy Learning!!