AWS Lambda Basics: Writing Serverless Code
Lambda is a serverless event-driven compute service that lets you run simple functions and return the results.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
There are four key capabilities necessary for a service or platform to be serverless:
- No server management
- Flexible scaling
- High availability (fault tolerance)
- No idle capacity
In this post, we will learn the basics of AWS Lambda and how you can use it for different use cases with ease. This will be an introduction post that can provide a foundation for upcoming demos and posts to help you learn AWS.
AWS Lambda is the compute layer where your code is executed.
Lambda is based on functions:
- It is a function-as-a-service (FaaS) offering.
- Each function contains the code you want to execute, Configurations how to execute, and Event-Source (optional) which can detect events and call your functions.
- Lambda is always ON and ready to receive calls. It automatically scales (runs multiple copies of the function in parallel, based on demand).
- Lambda is also stateless.
- Lambda bills in 100ms increments.
- Choose memory; get CPU. (CPU and network capacity increases as more memory is allocated.)
- You can test and measure memory settings to optimize execution cost.
Beware of code-size and other limitations. For more info, see the Lambda docs. You can also visit the AWS Official Lambda Page for updates.
Workloads You Can Consider for Lambda
The following are a few workload types you can consider for Lambda:
- Backend processing
- Event processing
- Stream processing
- Data processing
Things That Can Trigger Lambda Functions
- AWS resource triggers (DynamoDB operations, S3 events, Message Queue operations, and so on)
- AWS endpoints (REST calls)
Why Lambda?
Lambda has a number of benefits, including:
- The ability to simply execute code
- Automatic scaling
- Fault tolerance
- You only pay for what you use
Writing Lambda Code: Options
You can write Lambda code locally (allowing you to develop locally, bundle, and deploy to AWS). You can also write directly in the AWS editor or mix the two options.
Creating Function Package
Typically, you will bundle up the source code/published output into a zip file.
However, in this post, we will write Lambda directly using the AWS web console.
Ways to Debug
- Test events from the web console.
- Use logging and CloudWatch.
Getting Started With Lambda
You can author from scratch, use a blueprint, or use other options to get started with AWS Lambda. The following screenshot shows different blueprints available ranging across various levels of complexity and different mixes of AWS services:
You can check the official documentation on this link to check the developer guides and other information.
Writing a Simple Lambda Function
Well, there are a lot of tutorials online to show how to write Lambda functions, so I will not go into those specifics.
Here is the code, which is based on hello-world NodeJS blueprint which I then modified to return data modeling a library domain:
index.js
This is entry point, a handler function. Inside the function, we are retrieving books from a library and return those to caller.
book-library.js
This is simply an in-memory data store to return books in the library.
book.js
This is the book entity with title and author info.
Deploy and Test the Lambda Function
Once we have written function code, we can deploy and test directly from the web console:
Deploy
Test
Integrating Lambda Function
Based on use case, there are many different AWS services you can integrate with Lambda functions while building solutions. The following are few of those arrangements:
API Gateway + Lambda
API Gateway is a very common component which receives HTTP/web socket requests and can delegate those requests to Lambda functions:
Lambda With S3 and SNS
One use case for this scenario would be an image uploaded to a certain S3 bucket that needs to be resized in different ways.
You could write a function to perform resizing, then configure the S3 bucket to call the Lambda function every time a new object is uploaded to the bucket.
You could do a similar process with a SNS topic. Whenever data is published to a certain topic, a Lambda function is called and then can do something with the data.
Function URLs
A function URL is a dedicated HTTP(S) endpoint for your function. When your function URL is configured, you can use it to invoke your function through a browser, curl, Postman, or any HTTP client. When you configure a function URL from the main function page, it will look like this:
Click Create function URL button and the following window will show up:
Here I am selecting NONE as Auth type, which results in a public URL anyone can call. For our demo purposes, its ok to select this type and click the Create button. The following picture shows that Function URL is created:
And if we click the URL, it opens the browser and you can see that the data is returned.
Lambda function URLs are a new feature (announced in April 2022), and you can learn more about them on this link.
Summary
This was a very basic and short introduction of AWS Lambda and how you can use this compute FaaS offering from AWS to quickly write code for various situations.
In this post, we covered the basics of AWS Lambda. We wrote a simple function which returns some data when called using Function URL. We will be using Lambdas a lot in later posts and will have opportunity to learn more advance use cases of Lambda functions.
Let me know if you have some questions or comments. 'Til next time — happy coding.
Published at DZone with permission of Jawad Hasan Shani. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments