How to create your first AWS Lambda function in two easy steps

Mariano Calandra
4 min readAug 23, 2019

At the time of this writing, serverless architectures and AWS Lambda are two of the hottest topics in the industry. We aren’t talking about a new pattern or framework: it’s something completely different, that changed the way we thing software architectures. Due to this shift, many people think that Lambda’s learning curve have to be necessarily steep. This perception is completely wrong and in this article we will prove it through a super-simple explanation and a two steps demo.

Anatomy of a Lambda function

AWS Lambda is the implementation of a Function as a Service (FaaS) paradigm. In this scenario, a function is the smallest unit of deploy that we can trigger in response to some events.

Function

In AWS Lambda, when we talk about a function we are talking of something like this:

function (event) {
// your logic here
return {
status: XXX
body: YYY
};
}

This code is so simple that anyone with a one-day programming experience can understand: there’s a function that accept a parameter named event and it returns a JSON object with two properties (i.e.status and body).
The status property have to be an HTTP code (e.g. 200 OK, 404 Not Found…) and it’s useful when we need to notify the function’s outcome to the caller. The body contains our return value.

Pro-note: the properties in return value are based on a convention. When we will be Lambda masters we’ll be free to return anything we want. :-)

Event

One of the main benefits of AWS Lambda is its event-driven nature, that means: our functions can be executed in response to an event. There are many events that we can listen to and handle using a Lambda function. For instance, we could create a function that sends an SMS every time an EC2 instance is shutted-down or we could create a function that is executed every hour, reads some info from a webservice and saving it into a database.

If we don’t need this behaviour, our function can be invoked on-demand.

Whatever the solution we choose, at runtime,event parameter will contains the information about the event that triggered the function.

Create your first Lambda function

But stop theory now and get our hands dirty. Open the AWS Console, enter into AWS Lambda service page and click “Create Function”.

Fig. 1 — Click “Create function” to start the wizard.

There are three main ways to create a new Lambda function, we will choose the “Author from scratch” option.
Give function a name and a runtime (here I choose NodeJS because I like it, but you are free to choose any runtime you prefer) then click the button “Create function”.

Fig. 2 — Provide a name and a runtime for the function, then click “Create function”.

As far as it sounds crazy, our first AWS Lambda function has been deployed and it is ready to be executed.
The next image show the main page of our function, here we can set some configuration parameters (environment variables, memory, permissions and other topic that can’t be faced in a four minutes tutorial) or we can take a look at function’s monitoring.

Fig. 3 — Our function’s main page.

Testing our Lambda function

As said before, our function has been deployed successfully and it is ready to handle requests but… even more impressive, is the fact that our function, right now, can scale handling thousand of concurrent requests.

To make a test, we need to configure a test event. In the top-right corner of our function’s main page click the “Select a test event” dropdown and then click “Configure test Event”.

Fig. 4 —Test event creation.

The content of test event follows the JSON syntax and in this moment we can write anything we want. Give the event a name and then click “Create”.

In the function’s main page we can now click “Test” (in the top-right corner of the page) and wait for function to execute.

Fig 5 — The output of function’s execution.

The function executed successfully! We can take a look to the result value, the resources used by our function and its execution time (i.e. 75MB and 27.50ms).

Note: We have configured the test event, but we haven’t used it inside the function. If you want to, you can get it through the event variable (in this case event.name).

Summary

In this super-fast introduction we only scratch the surface of AWS Lambda potential. In the next future, we will probably deepen some of this parts, but for now have in mind this important points:

  • a function is the smallest unit of deploy;
  • a function can react to events, enabling event-driven applications;
  • by design, our functions are highly-available and able to scale;
  • it is completely integrated with the other AWS resources;

Last but not least, the pricing schema of AWS Lambda is really peculiar, but we will talk about it in a next episode.

--

--

Mariano Calandra

Mariano daily helps companies succeed using cloud and microservices. • AWS Authorized Instructor • AWS Community Builder • goto.calandra.me/support