Tech

Getting to know AWS Lambda


To fully understand how AWS Lambda is used, first, we need to mention serverless computing given that lambdas are based on that principle.

What does serverless mean ? 

Simply put, serverless is a cloud-based computing solution that enables users to successfully execute code without managing it directly on the server but instead running them on a number of cloud providers ( AWS, Azure, GCP, IBM, etc. ). You can think of it as a reliable solution to building scalable application infrastructure by paying someone to manage your servers or code. Hopefully, this intro was understandable enough to get you the basics of how the serverless principle works.

What are AWS Lambdas ?

Lambdas are a computer-driven cloud-based serverless service that enables you to execute code in the cloud. Whether it is an app authentication, web scrapping, daily deployment automation, DynamoDB data processing, or an S3 bucket data filtering script, lambdas can handle it. In the background, what AWS does is, wrap your code in a container that runs Amazon Linux 2 – a Linux distribution made by AWS. You can set up the time it takes for the function to execute as well as dedicate memory to it. How long a function executes as well as the allocated amount of memory plays a major role in the cost of lambdas. A similar solution to this service on Google Cloud is called a Cloud Function.

Why should you use them ? 

Security

Given that lambdas are actually hosted on AWS Cloud, AWS can handle most of the security aspects out of the box over your function. If you, per se, have an API Gateway endpoint that is triggering a lambda function you might put it behind AWS WAF ( Web Application Firewall ) which can mitigate DDOS attacks, header manipulation, XSS ( cross-site scripting ), IP blacklisting so on, and so forth. Aside from that, we can define a special user with special roles on the AWS IAM console that will have access to only lambdas you specify, so there are no unwanted actions on your developers’ end.

Support for multiple languages

You can write Lambda functions in a majority of languages including

  • Python
  • NodeJS
  • Ruby
  • Go
  • Java 11
  • .NET Core ( C# / Powershell )

Monitoring / Logs

Each function has its own log group and monitoring statistics such as a number of invocations in a certain period of time, which is handled by AWS CloudWatch. We can check the logs anytime, if our IAM user has access to it, to mitigate errors that happen during their execution.

Scalability

You can easily scale multiple lambda functions over multiple regions that will handle modular tasks to create a microservice-like architecture that suits your needs.

Libraries/Dependencies

Given that lambdas are actually running Linux operating system in the background, AWS provided us with a way to install external libraries we may wish to use in our code. For example, installing bcrypt module for NodeJS or pymongo and requests for Python can easily be done by using Lambda layers. You can check out how to create them right here!

Integration with other AWS services

Examples of this would be triggering a lambda function:

  • by inserting a row into a DynamoDB table
  • by inserting data into an S3 Bucket
  • before/after execution of the current lambda function
  • by processing SQS ( Simple Queue Service ) data
  • by dispatching a request to an endpoint made with an API Gateway
  • by scheduling through Amazon EventBridge

How to create them ?

First, go ahead log in to your AWS account and make sure your account has IAM permissions that allow you to create lambdas on a region of your choice. Once logged in you will be presented with the following screen:

The easiest way to get to the Lambda interface is by searching it up on the AWS service search bar located on the top of the webpage:

By clicking on Lambda service, the following screen loads up:

Let’s go ahead and create a simple lambda! Start the process by clicking on the orange “Create function” button. Once a button is clicked, we are presented with:

For the purpose of this blog post, we are going to create a simple “Hello World” example. Let’s name our function “function-1” and for the runtime, we are going to select Python 3.8. Lambda creation by default creates a new execution role for each lambda. If you wish to change that you may create the roles beforehand and make lambda use them by clicking on the “Change default execution role” => “Use an existing role” and thus selecting the role:

To finally create it press on the “Create function” button in the lower right corner of the page. What happens next is on this screen:

Execution logic

We can edit code directly from the browser and execute it. The main logic of lambda is located inside a file called “lambda_function.py” in a function called lambda_handler. In the event argument, there are parameters that are going to be used in a function in the form of key-value pairs. Context argument contains AWS metadata used during lambda’s code execution.

Lambda functions don’t need to have a return statement unless an AWS service that lambda is integrated with needs it. For example, API Gateway requires a status code param and body in the return statement.

Let’s say we want to return a key-value pair passed in an event argument:

Here is the code as well:

import json

def lambda_handler(event, context):
    return event["result"]

Don’t forget to click on “Deploy” to save the changes.

To execute it on the browser, we need to create a test case for it. Due to the fact that we are not doing AWS integrations with other services in this post, we will create a simple “Hello World” test case. Start by clicking the Test arrow on the right side of the button and then “Configure test case”: 



Name your test case however you wish. We will name it “function-test-case”. In the bottom area, we define key-value pairs in a JSON object that the event argument will use in lambda:

JSON:

{ 
   "result" : "value1"
}

Click on the “Save” button on the modal’s bottom to save the test case.

To test this lambda, we click on the “Test” button ( not the arrow this time ). If everything is set up good, this should be the outcome of execution”:

In response, we got the value that we passed through from our test case JSON object to event argument and used it successfully in the return statement.

That’s all folks!

Aside from lambdas, there are other services on AWS cloud infrastructure. One of their integrations, as of 2015, is ElasticSearch. To dig deeper into this topic, our advice for you is to check out this article !

We hope that you enjoyed this post and learned a thing or two along the way. More to come on this topic soon. Stay tuned! 🙂

Share this post

Share this link via

Or copy link