see more blog

An introduction to AWS Lambda and how to configure it for beginners

deep-dive-on-aws-lambda-vti-cloud

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, VTI Cloud 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?

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:

  • Java
  • Python
  • C #
  • Node.js
  • Go
  • PowerShell
  • Ruby

The function also provides runtime API, which can be used to run functions written by other native programming languages.

deep-dive-on-aws-lambda-vti-cloud

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.

Check out our guide to creating an account and using the AWS Free Tier to experience AWS Lambda services here: Experiencing cloud computing with AWS Free Tier account | VTI CLOUD

AWS Lambda Features

Here are some important features that VTI Cloud 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 (VTI Cloud will speak more detail in the next section), 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.

aws lambda processing data

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

real time processing data with aws lambda

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.

lambda pricing

You can see more about AWS Lambda pricing here: https://aws.amazon.com/lambda/pricing

How does AWS Lambda work?

aws lambda features

To summarize how this service works, VTI Cloud aggregates into 02 main stages:

  1. First, you create a function and add basic information to the service, such as the programming language used in the function.

  2. 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

VTI Cloud 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.

aws lambda in management console

  • The Function window will be open, select Create Function.

create function aws lambda

  • 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.

create function 1

  • 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 print('Hello, world!').

  • 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).

region for aws lambda

  • You also need to choose an execution role. VTI Cloud will choose to create a new role option (role creation article for new people to understand). Then, click Create Function.

create role aws lambda

  • Note that depending on the need to create functions in VPC cases that can be customized in the Advanced Settings section

advanced setting create function

  • You’ll receive a success message that the comment has been created. Click the Designer window to shrink it.

successfully create function lambda

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.

upload from zip or s3

  • Here VTI Cloud is example code of the game “Dice”.

function code windows

  • 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.

configure test event

  • Click Test to test the code > Result to “5”.

test event

  • Pulling upwards will have specific information about this test. Details such as request-id, duration, billed duration, resources configured … with log output available.

execution details

  • 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.

You can review the Amazon CloudWatch service basics article here: Amazon CloudWatch basic definitions | VTI CLOUD

monitor aws lambda function

cloudwatch logs insight

  • 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.

cloudwatch logs insights aws lambda function

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, VTI Cloud will show you how to add API Gateway trigger with Lambda function.

aws lambda trigger api gateway

  • Similar to the function creation instructed above, here VTI Cloud creates a function called trigger-with-api-gateway for demo.

  • Click addtrigger to select API Gateway and create a new API.

aws lambda trigger api gateway 1

aws lambda trigger api gateway 2

aws lambda trigger api gateway 2.1

  • Once done, click Add

aws lambda trigger api gateway 3

>> API Endpoint will have the following form: https://RANDOM_STUFF.execute-api.REGION-NAME.amazonaws.com/default/MY_FUNCTION_NAME

That’s it! It’s simple, isn’t it?

Conclusion

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!

About VTI Cloud

VTI Cloud is an Advanced Consulting Partner of AWS Vietnam with a team of over 50+ AWS certified solution engineers. With the desire to support customers in the journey of digital transformation and migration to the AWS cloud, VTI Cloud is proud to be a pioneer in consulting solutions, developing software, and deploying AWS infrastructure to customers in Vietnam and Japan.

Building safe, high-performance, flexible, and cost-effective architectures for customers is VTI Cloud’s leading mission in enterprise technology mission.

In addition, VTI Cloud supports building VIET-AWS community. This group is one of the fast-growing AWS User Groups and officially recognized by Amazon in the Asia Pacific (Vietnam) region.

VIET-AWS is a place to connect and exchange support between Solutions Architect, DevOps, SysOps, and budding students with cloud computing services of Amazon Web Services (AWS). Join VTI Cloud to join VIET-AWS: https://www.facebook.com/groups/vietawscommunity

Related news

what’s up at VTI