Part 3/4: AWS Lambda for Beginners

Part 3/4: AWS Lambda for Beginners

This is a third part of a 4 part mini-article series on Serverless Computing in general, with a detailed developer insights into two offerings: AWS Lambda and Google Cloud Functions. I am working on these articles and will be releasing the articles in the next few days.

First and second part in this article series is here and here. In this third part of the series, we will look at the basics of serverless offerings from Amazon Webservices: the AWS Lambda

AWS Lambda: What is it?

Just like Toyota or BMW or Mercedes is a specific brand implementation of a car, AWS Lambda is a serverless computing platform implementation, designed and developed by Amazon Webservices.

A developer writes some code and deploys it to AWS Lambda platform and expects it to be executed instantly when called. The AWS Lambda platform provisions the required hardware and provides a runtime when that code is invoked without our intervention

You bring the code, they bring the hardware!

A developer's job is to develop the business logic while platform's job is to execute that logic. Platform makes necessary arrangements to get that code executed on demand. As a developer, his/her focus is getting the logic ready while servers, networks, software licensing and costs are left to AWS.

Platform and Lambda Function

The AWS Lambda is a platform which does awful lot of things behind the scenes from provisioning the servers, creating the networks, securing the access, routing the calls to instantiating the code, providing the runtime, executing the code and finally winding down everything after.

The code we write (this is the only responsibility that's vested on us) is termed as a Lambda Function or Function. When we say a function, we are referring to the program of code written in a choice of languages. The function is ideally a stateless, each invocation leading to crate a new instance doing its job afresh. 

We can design the function by persisting the state to the durable store if your architecture dictates. Keep a note, AWS can provide a temporary folder for storage (/tmp) but I highly recommend not to use this storage for anything other than runtime purposes. 

Should you need to design the functions with some sort of state, you may consider flushing that state to a durable store so the next function can pick up if you that is what you want to. 

How does Functions work?

When an event is fired to invoke a specific function, AWS Lambda runtime will create a new container, set the environment, find the respective function code to create an instance of it and finally invoke it with the incoming event data.

The instances will be invoked as and when needed, on-demand, in a completely secured and isolated environment. Usually, one instance is tied up to a single request, meaning it’s highly unlikely the platform maintains a pool of instances. So when a request comes in, an instance will be freshly created but not pulled off from a pool as there isn’t one! 

Having said that, it is an implementation detail and provider may see a benefit of keeping an instance alive for few minutes rather than winding it up instantly after its work is done. 

Languages and Runtimes

AWS Lambda provides handful of programming languages and runtimes for developing our functions. We can surely use Java, C#, Python, Go, Ruby, PowerShell and JavaScript. Platform deploys a container with respective runtimes (JVM for Java, NodeJS for JavaScript, .Net for C# and so on) based on the language the function was developed. We can also expect additional language support in the near future. 

There's a Software Development Kit (SDK) from AWS to develop and access the platform for their respective language along with Command Line Interface (CLI) for scripting.

Pricing

The AWS Lambda function is billed based on few parameters - how many requests were made, how long our function ran and how much memory it had used. For example, should our function was inflight for say 70ms and used about 256MB of memory, then we will be billed exactly for that computing power we borrowed, of course the execution time rounded to nearest 100 ms, here in our case for 70ms, it’s rounded to 100ms.

Free tier include 1 million free requests per month and 400k GB-seconds of compute time. The cost of additional 1million requests is 20 cents (that is, $0.0000002/request) and $0.00001667/Gb-sec compute time.

Function Properties 

When we are ready to deploy a Function to the AWS Lambda platform, we need to set some properties to these functions. Properties such as how much memory this function should be allocated, how long it should run etc. 

Memory

Each function will be given a user-defined memory with maximum allocation up to 3GB with 128MB being the minimum by default. This memory setting is set before we expect the function to be swinging into action, most likely during development/deployment phase. We can tweak the memory in multiples of 64MB. The CPU is allocated automatically in proportion to the memory, not something in our hands that we can alter. The CPU-memory relationship follows the same principles as creating an EC2 instance. 

Timeout

The function also has a timeout set on it, maximum being 15 minutes. The default is 3 seconds. The timeout indicates the maximum amount of time function will be kept alive before killing it by the platform. You can’t expect the function to run relentlessly (unlike Bezos:)) over the weekend doing some shoddy ETL process. You will be charged to the nearest 100 ms during the costing of the Lambda execution. Do keep a note that AWS may increase this setting as per their strategy in future, so always look out for their platform updates. 

Functions Programming Model

The function code adheres to a specific programming model dictated by AWS Lambda.

To understand this model, let's consider an example scenario: Say, we are working for a company MoonX and we like to offer a functionality to our customers to reserve a flight to Moon.

For brevity, let's consider writing the logic in a method reserveFlight, which is defined in a file called moonx.js as we have chosen our runtime to be NodeJS.

// moonx.js
exports.reserveFlight = (event, context, callback) => {

  // the logic of reserving the flight goes here..
  console.log("Flight from London to Moons is successfully reserved!")

  // explicit callback
  callback(null,"Pack your bags, you are going to Moon!");

};

Let's decipher this Lambda function and understand the model's constituents.

Handler

This is the name of the Function platform executes when it's called. It is an entry point into the world of serverless execution. In a NodeJS runtime, this is formed by concatenating the name of the file and the function name. Like moonx.reserveFlight in the above case (fully qualified class.methodname in Java's case).

Once the function is found, platform passes few inputs to it - in the form of event, context and callback.

Context

When your function is being executed, you may need access to runtime or platform capabilities like where to store temporary data or memory profile or time remaining for completion of the function etc. These details are grabbed from a Context object which is passed by the platform to the function.

Event

Event is the input to the function. It is the incoming data that our function expects to act on it. Going with our reserveFlight function, it surely expect a Customer's details to book that pretty flight!

We may have a need to kick start a background process, say firing an email campaign or starting an ETL (extract-tranform-load) job which may not require an input but just a signal to kickstart. We can omit the event in such cases.

Callback

The function can expect an optional argument callback. The function logic should wrap a response to be returned to the caller if provided, else a null is returned. We can also use async functionality so we don't explicitly pass in the parameter as shown here:

// moonx.js
exports.reserveFlight = async (event, context) => {

  // the logic of reserving the flight goes here..
  // implicit callback
  return "Flight from London to Mars is reserved!"

};

Lab

Enough with the theory, let's jump into action.

First thing you need to do is to signup for an AWS account. Visit AWS and sign up for a free account. Every new account is entitled with a generous one year free tier. You can learn AWS Lambda using free tier for sure.

(If you are not onboard on AWS yet, get on it - I do have a plan to write few articles introducing AWS - ping me if you are interested!).

Once you complete the initial formalities of creating the account, head to AWS Management Console and search for Lambda function under Compute category.

If this is your first time here, you may find the Management Console a bit baffling or confusing. But trust me, it is a great tool to journey into the amazing world of computing services pain-free! Note, you can use Amazon's Command Line Interface (CLI) or Software Development Kit (SDK) for the same purpose.

Now you are in Lambda service, you may see the following screen asking you to create a function:

Click on the Create Function to go to next step. We will be presented with a new page where we will be writing our first function. We have an option of creating a function in three ways, from scratch, following blueprints or deploying one from a repository, as shown the screenshot below:

Blueprints provide pre-defined templates where we simply create our function based on those templates. The repository has a stash of fully functional serverless applications that are readily available from AWS or AWS Marketplace.

In this exercise, let's do everything from ground up. When you select Author from scratch box, you'll be presented with a form to fill in details, fill in the details duly:

Our goal is to send your friend to Moon, so we write a function reserveFlightToMoon that does exactly that - reserving the ticket for his safe travel. 

For this example, we pick up NodeJS as our runtime, so select NodeJS 8.1.0 as runtime from the dropdown. You have other options like Java, .Net, Python and Go, experiment with what ever you language your prefer.

Each lambda function needs to be associate with a role. Role permits the function to access certain AWS services, in this basic case, our function is accessing a CloudWatch service. This is the service that aggregates the logs from any of the AWS service, including AWS Lambda. We simply select a choice create a new role from one or more templates. Ignore the policy for now. 

As we have chosen NodeJS as the runtime, the console has an inbuilt IDE(Cloud9 editor).  which allows us to write our code on the browser itself.

In the real world, usually you’d write your code part of the bigger project. You have the option of writing code in your chosen IDE on your local machine, zip it up and upload the package to the AWS Lambda platform. The management console let’s your upload the packaged zip file, however, you can also use AWS’s Command Line Interface (CLI) or SDK for programmatic deployments. 

Once done filling this form, go ahead and click on the Create Function button to create the function.

In the next page, a designer of the function is presented to you, as shown here in this screenshot:

The lefthand side has a list of triggers that we can associate our function with. The trigger is a mechanism to invoke the function, for example, if a new user if registered on our website, a trigger will be formed upon this new user's database record. This is also one of the mechanism to integrate Lambda with other AWS services.

We ignore configuring a trigger for now as we use a test event to trigger our function (the console provides the testing functionality to execute our function).

The next bit is to write the logic of sending a customer to moon!

Scroll down to find the inline editor. Write the required code and save it

We are done creating the function. We need to test the function (we don't want the customer to land in neighbour's garden when he is actually expected to land on moon!).

We should create a test event to invoke this function which executes and lets us know if it's working as expected. Follow the screenshot below to do that:

Clicking the Test button will pop-up with a form that we should fill the event information. This is the event that gets passed on to our function when it gets called.

Configuring test event as shown in the following screenshot:

Name your event, change your input data and save it. That's it, we have created our lambda function.

One final step is to send the Customer to moon. Select the test event we have just configured and click on the Test button on the upper right corner of the console.

If all goes well, you should see a screen like this:

That's pretty much it! You now know the basics of AWS Lambda.

In this article, we have learned the basics of AWS Lambda and created a function using management console (and transported a customer to moon). In the last and final article of this mini-series, next week I'll explain Google's offering of serverless compute framework called Cloud Functions.

Now you have a basic understanding of AWS Lambda workings, why don't you give it a try (perhaps send your spouse to Mars?!).

Don't forget to like or comment!

要查看或添加评论,请登录

Madhusudhan Konda的更多文章

社区洞察