An introduction to AWS Lambda
We’ve heard a lot about the term “serverless,” how they work, and the benefits that your application brings. A serverless service in general will not require any server provision to run the application. When you run an application on serverless, users will not have to worry about setting up the operating system, patching, or extending the servers that you will have to consider when running your application on a physical server.
Recently, when it comes to serverless, it’s about AWS Lambda services. This is a provision-free serverless compute offering based on Amazon Web Services functions. In this article, Viet-AWS will introduce AWS Lambda, including how it links to a serverless architecture, how to create it, and when to use it.
What is AWS Lambda?
is a serverless compute service of Amazon Web Services (AWS). AWS Lambda operates on the FaaS (Function-as-a-service) model. With AWS Lambda services, users, especially developers, won’t have to worry about managing and delivering zero administration, just focus on building applications!
By simply uploading code to AWS Lambda, the service runs code, scales about the infrastructure to always ensure application performance and availability.
Code running on AWS Lambda is called lambda function. Currently, lambda function supports the following programming languages:
- C #
The function also provides runtime API, which can be used to run functions written by other native programming languages.
To work with AWS Lambda, there is only one prerequisite; you must have an account on AWS from which you can access the AWS management console.
AWS Lambda Features
Here are some important features that we summarizes:
- AWS Lambda easily scales infrastructure without any additional configurations. This service helps developers reduce operational work related to system infrastructure
- The service offers a variety of options such as Amazon S3 services, Amazon CloudWatch, Amazon DynamoDB, Amazon API Gateway, Amazon Kinesis, Amazon CodeCommit, and more to trigger events.
- No upfront costs required. You pay only for the memory used by the lambda function and the minimum cost for the number of requests so AWS Lambda is quite cost-effective for users.
- AWS Lambda uses AWS IAM to define all security roles and policies due to ensuring the security of users’ applications.
- AWS Lambda also provides fault tolerance for services running code and functions. Users won’t need to worry about the downtime of the app.
Some common AWS Lambda use cases
Here are some use cases where AWS Lambda services have helped users thoroughly solve common problems:
1. HTTP API
Using AWS Lambda, you can integrate or deploy logical backends to the cloud and invoke functions with caution just by calling HTTP.
When integrating Amazon API Gateway with AWS Lambda, not only will costs be minimized, but users will also minimize the effort to operate servers as well as expand services.
2. Data Processing
For example, if an application handles a lot of data stored in Amazon DynamoDB, you can trigger Lambda functions whenever you write, update, or delete items in that table.
These events trigger lambda functions that will process, analyze, and can push this data to other AWS services such as Amazon S3 to store results…
In other words, users can create an entire data processing process by combining different AWS resources.
3. Real-time file processing
Usually, CMS applications for content management always have the function of uploading images. This image will be saved on the Amazon S3 bucket. Aws Lambda can now be used to create an automated task.
For example, after uploading an image, triggers to create another version of the shape with a lower resolution (thumbnails) and be at another bucket.
AWS Lambda will help applications automatically in in-call logic on demand and only if it is really necessary. Reference: GitHub – aws-samples/lambda-refarch-fileprocessing: Serverless Reference Architecture for Real-time File Processing
4. Real-time stream processing
For applications with “tremendous” traffic, the system often uses AWS Lambda and Amazon Kinesis Stream services to process real-time streaming data for application tracking activities, or real-time studies of various data from metrics collect from many data sources such as clickstream websites, Payment transactions, Social media timeline, IT logs or Location-based tracking.
Reference: GitHub – aws-samples/lambda-refarch-streamprocessing: Serverless Reference Architecture for Real-time Stream Processing
The service costs
With AWS Lambda, you pay only for what you use. You are charged based on the number of requests for functions and duration, code execution time.
Duration is calculated from when the code is enforced to the time the execution is complete or terminate, rounded to the nearest 1ms. The cost will be based on the capacity of the layout allocated to the function.
AWS Lambda free usage tier includes 1M free requests and 400,000 GB-s of calculated time per month. Below is the price list for the region Asia Pacific (Singapore) of this service.
You can see more about AWS Lambda pricing here: https://aws.amazon.com/lambda/pricing
How does AWS Lambda work?
To summarize how this service works, Viet-AWS aggregates into 02 main stages:
- First, you create a function and add basic information to the service, such as the programming language used in the function.
- Then you write your code on the lambda editor or upload the source code in a zip file. When lambda code is uploaded, the service handles all capacity expansion, patching, and infrastructure administration.
Let’s dig deeper…
Step 1: How to Create an AWS Lambda Function
We will for example create a simple game using lambda function in Node.js for this article.
- Go to the AWS Management Console table, and in the search bar, type Lambda, click Lambda services.
- The Function window will be open, select Create Function.
- You will get different options to create a function with their explanation. Since I’m creating it from scratch, I’ll choose Author from Scratch.
- Then you need to fill in some necessary information for this lambda function. Enter the function name and select the Node .js that you want to use for this function.
There are 04 methods when creating functions:
- Author from scratch – Starts from a simple command like
- Use a blueprint – Allows you to build a lambda function using “blueprint”. Blueprint in this context means a pre-written piece of code (NodeJs and Python only) for common use cases, such as logging updates made to a DynamoDB table or activating a function when something is uploaded to the S3 bucket.
- Container image – Use the available image container to deploy functions, using this method to take advantage of image containers from the previous project to save time creating new.
- Browse serverless app repository – Allows you to deploy all sample applications from the AWS Serverless Application Repository. These packages can be provided by AWS or by the community.
AWS Lambda is not a global service!
This means you have to change the region to where you want to implement Lambda function (depending on project requirements).
- You also need to choose an execution role. We will choose to create a new role option (role creation article for new people to understand). Then, click Create Function.
- Note that depending on the need to create functions in VPC cases that can be customized in the Advanced Settings section
- You’ll receive a success message that the comment has been created. Click the Designer window to shrink it.
Step 2: Upload code to AWS Lambda Function
- Below is the Function code window. Here will be where you upload your code, be it straight into this window (called AWS internal code editor), or upload as a zip file.
- Here we is example code of the game “Dice”.
- Now click the Test button in the top right corner. You will get a pop-up window to configure the test event, enter the event name and click Create.
- Click Test to test the code > Result to “5”.
- Pulling upwards will have specific information about this test. Details such as request-id, duration, billed duration, resources configured … with log output available.
- In the Monitoring tab, the log and resource parameters of the AWS Lambda service used will be tracked, including Logs Insights generated by the Amazon CloudWatch service.
- If you go inside the cloudwatch-generated log, you can see in detail what happened while running the lambda function, which is tracked by CloudWatch.
How to configure AWS Lambda with other AWS services
To run code in AWS Lambda, you need to enable lambda function with an external AWS service, which can call lambda function. For example, the service can be an S3 bucket.
- Within milliseconds, lambda will be ready to automatically activate your function when an event occurs. AWS Lambda runs your code when the trigger event is in call. the service provides management and monitoring of servers for you.
- If your function requires a lot of processing power, the service will choose the instance type with more processing power and RAM, or if your lambda code is only executed in two seconds, the service will choose the lowest possible version, saving you money and time.
In this article, we will show you how to add API Gateway trigger with Lambda function.
- Similar to the function creation instructed above, here Viet-AWS creates a function called
triggerto select API Gateway and create a new API.
- You can choose between the HTTP API and the REST API. Then fill in the information by default. In the additional settings section, additional options can be selected.
- Enable CORS in HTTP API: Configuring CORS for an HTTP API – Amazon API Gateway
- Enable CORS in REST API: Enabling CORS for a REST API resource – Amazon API Gateway
- Once done, click Add
>> API Endpoint will have the following form:
That’s it! It’s simple, isn’t it?
For a developer, saving time to manage and operate your infrastructure system ensures that your application can scale, high availability is indeed precious. The AWS Lambda serverless service helps developers just focus on developing their applications without having to pay attention to their code-running infrastructure!