Introduction to AWS Lambda
AWS Lambda is a responsive cloud service that inspects the application’s actions and responds by deploying user-defined codes known as functions. Before we learn the topic in depth let us first understand AWS Lambda from two different scenarios.
You have a project in mind. You are formulating the algorithm in your mind. You are assessing the technology stack options you can use to build your project. All is going very well until you realize you need to download the required software, IDEs, tools and procure server space to serve your code. Okay, you would need to set up the basic infrastructure before you start actual coding on your project. Ohh, but you need your application to run seamlessly on computers as well as mobile interfaces. This means more infrastructure to set up before you hit the ground coding.
Too much of an overhead for initial stages of development? Too much of an overhead for managing infrastructure even as your project scales up? Is your project overshooting on the cost estimates? Is your focus divided between all things but coding?
Solution – These are the problems but AWS Lambda is the solution.
AWS Lambda – Serverless Computing
AWS Lambda lets you run your code without provisioning for the servers and other supporting infrastructure. You can code in any type of application or backend service without worrying about the server or the supporting DLLs/software. You only need to focus on your code. Rest everything is taken care by AWS, starting with serving your code, through triggering your code on specific events, to managing more server space when your code scales up.
- AWS bills you based on the time your code is executing, in multiples of 100-millisecond frames. There is no charge for the time your code sits idle on the server. What better motivation to write efficient code!
- The term ‘Serverless’ often denotes the shift to the cloud. With the cloud, you can delegate your operational responsibilities to AWS and focus more on providing a better consumer experience through your application.
- While you do so, AWS handles tasks such as infrastructure management, clustering, patching, security, operating system maintenance, etc.
Why AWS Lambda?
As is human nature, it is always good to evaluate if the technology/tool is worth the benefit. Let us see some of the features of AWS Lambda that makes life easy for developers.
4.5 (1,258 ratings)
1) Bring Your Own Code
2) A Host of other AWS Services
AWS is a one-stop shop for all the infrastructure needed. It has easy integration within various services such as storage (S3), databases (RDS, DynamoDB), servers (EC2), security management (IAM), networking (Route53, CloudFront) and a whole lot more.
3) Custom Code
Need to write a custom back-end logic tailor-made for a single client? Instead of writing the logic at the client side, use AWS Lambda to write custom services and make your code free of client platform updates.
4) Pay Per Use
You need not shell out bucks for the time your code is sitting idle. If your service receives hits only during the day, pay only for the time your code is executed. This is calculated in time-frames of 100 milliseconds.
5) Automatic Scaling
AWS automatically scales up resources when your service receives higher hits. You need not configure anything to procure additional resources. There is no limit to the number of requests that your code can handle. Thus, the performance remains high at all times.
6) Amazon’s Disaster Recovery
With your code and data replicated across multiple availability zones, any facility failure does not impact your application. The requests are automatically routed to another zone.
Let’s Get Started with AWS Lambda Tutorial
Excited? Let’s get started with AWS Lambda. As it is a custom across the programming world, we would start with a simple ‘Hello World’ program.
To get started with AWS Lambda, you need to have an AWS account. It is different from the Amazon account that you use for shopping. If you already have one, log in to the console and skip to the next section. If not, follow the below steps. Don’t worry, it is absolutely free as long as your application does not exceed the free tier limit.
Create an AWS Account
- Open AWS console. Click on Create New AWS Account.
- Fill up your information and update your card details. Amazon won’t charge you anything unless you exceed the free-tier usage limit. The free-tier usage limit is abundant for single user development.
- After completing the sign-up process, visit the link in Step 1 again. This time, log in to the console with your username and password.
Enter Lambda Console
Once you have logged in to the AWS management console, you would see a link to Services in the header pane. Click on it and you would find Lambda inside Compute subsection.
This is how the lambda console looks like at first glance:
You can also browse to the Lambda dashboard and see statistics of your code. It gives you an insight into invocation count, execution duration, success and error ratio, etc. A sample dashboard looks something like the below.
Ok, so the next step is to create a function. All codes in Lambda are termed as functions because, well, they perform a specific task. Notice a Create Function button at the top right of the Lambda console. That button will take us to the below screen:
Creating a AWS Lambda Function
Let’s see how to create AWS Lambda Function by following the below steps.
Now we have three options, to begin with:
- Author from Scratch – You would be given a blank template and can write your code from scratch.
- Use a Blueprint – A template with some libraries included to suit your needs. You only need to work on the logic of your code.
- Browse Serverless App Repository – Ready to use built-in codes performing various functions. These can be readily used in your code as well.
To suit our purpose of understanding Lambda, first, we would go with using a blueprint. Once you are confident enough, you can start constructing your code from scratch and explore the potential of AWS Lambda.
Now click on Use A Blueprint and search for the word hello. The following results pop up. Let’s create our hello world application using NodeJS.
Now we arrive at the configuration screen for our function. Enter a name of your choice and select ‘Create A New Role from AWS Policy Templates’ from the dropdown for Execution Role. Give the role name as ‘lambda_basic_execution’.
Note – An Execution Role defines the permissions that AWS Lambda can assume while executing your code. A basic execution means that lambda will simply execute the code where there is no third-party library involved. In case your code needs to interact with Amazon S3 or RDS or EC2 or any other services, appropriate permissions to access these services must be given to your AWS lambda function.
Click on the Create Function button at the bottom of the screen. The following screen appears on the successful creation of your function.
Note – The ARN or Amazon Resource Name is a unique name of your function. This can be used as the endpoint to trigger your function. The triggers in the designer section are where you add services that can trigger your function. In every service that can trigger your function, your function’s ARN would need to be configured. It’s like a two-way binding of services. The trigger service would know that it needs to hit your function and your function would be configured to receive requests from the trigger services you add. Adding triggers is the next level of lambda tutorials and is beyond the scope of this article.
Below the designer sub-section is the Function Code sub-section. This is your inline IDE facilitated by Amazon. It looks something like this:
- Under the Code Entry Type dropdown, you can select whether you want to edit code inline or upload a zip file from your system or upload a zip file from Amazon S3. The upload options can be used for complex codes which can be easily edited in some better-featured IDE in your local system.
- Runtime is the language/framework which supports your code.
- A Handler is an invocation function inside your AWS lambda function. Your AWS lambda function can have multiple handlers and you can specify which handler to begin execution with. Picture Handlers as methods which are designated to perform specific tasks.
On the top of the page, you will see a strip with some buttons. Click on Select a test event to test our code. This will prompt you to Configure a test event, in case none is created.
Configure your first test event like the one below:
Save the test event and click on Test. You will see the execution results of your function.
- Response – The end result returned by your function. Notice that our function returned key1.
- Request ID – Each request to your function has a unique request id. These are used by Amazon to track requests and generate your billing information.
- Function Logs – The most insightful part of AWS Lambda. These are detailed logs that specify the start and end of execution of your function. Unlike debugging in usual IDEs, AWS Lambda relies heavily on logs for debugging purpose. They are stored in CloudWatch for future references as well.
Congratulations! You have successfully created a Lambda function. But wait, we haven’t invoked it yet. We just tested it from our own environment. Let’s invoke it from the outside world.
Invoking the AWS Lambda Function
We would be creating a REST API to invoke the Lambda function.
Go to Amazon API Gateway. It can also be found under Services -> Networking and Content Delivery. Click on Create API.
Configure your API basic details as below and click on Create API.
Now your API is created, but there is no HTTP method defined in the API. Click on Actions dropdown and Create Method. You will notice a smaller dropdown appear in the left pane. Click on GET from this dropdown. So basically, we are creating an HTTP GET method for our API.
Configure your GET method to invoke a lambda function as illustrated below. You need to specify your Lambda function’s ARN in the Lambda Function field. Click on Save.
Once your GET method is saved, you would arrive at the below Method Execution screen.
Now, your GET method is also created. But we still haven’t supplied the key-value pairs that we did in our test environment. Our AWS Lambda function expects three keys and values. Let’s define them.
Click on Integration Request and scroll down to Mapping Templates. Define your mapping template as below. The content type is application/json. This is simply the format of the parameters that you would be sending in your request body.
The API is now ready to be deployed.
Click on Actions and Deploy API. In the deployment popup, select New Stage and give a meaningful name and description.
You will arrive at the below screen with the REST endpoint of your API configured and ready to use.
Click on the endpoint and the browser will send a request to your Lambda function and display the response.
Congratulations! You have successfully invoked your Lambda function as well. Quite an achievement.
Who can Learn AWS Lambda
It must be evident by now that AWS Lambda function requires a little knowledge about services, deployment of services, requests, and responses. This is basic stuff that any computer programmer must know in order to create web applications. If you do not have sufficient knowledge on this subject, it is recommended to gain this knowledge so that hassle free coding in AWS Lambda is ensured.
AWS also has comprehensive documentation on all its services. Explore the documentation for a deeper understanding of the AWS world.
Career in AWS Lambda
It’s not just Lambda, but the whole Amazon Web Services sector is a booming one today. There is a demand for experts in AWS services. AWS also provides certifications suiting your expertise level. Explore the AWS website for further information on this.
Now that you have understood the basics of AWS Lambda functions, go on to create more functions and integrate with other AWS services. Keep playing and keep learning. Cheers!
This has been a guide to What is AWS Lambda. In this tutorial on AWS Lambda Function we’ve explained how to Configure and Deploy API. You can also go through our other suggested articles to learn more –