Lambda Functions - What are they? How to use them?

Lambda Functions - What are they? How to use them?

What is AWS Lambda?

Lambda is a computer service provided by AWS to run code without provisioning or managing servers. It runs on high-availability compute infrastructure and perform all the administration of compute resources, including server and OS maintenance, capacity provisioning and automatic scaling, code monitoring and logging.

I can run code for virtually any type of application or back-end services as long as it’s supplied in the languages supported by Lambda.

What are the benefits of using AWS Lambda?

  • No servers to manage
  • Better software development
  • 1 million free requests/month
  • Testing features that allow for code validation before putting in production

Lets make your first Lambda Function

Log in to your Amazon Web Services account, this is what your dashboard should look like

Screenshot from 2021-11-28 22-56-44.png

Search for Lambda using the search bar or select it under computing services expanding the services drop down Your screen will look like this

Screenshot from 2021-11-28 22-59-36.png

Click on the Create Function Button, once you’re redirected to the create function page you’ll fill in some basic information about your function

  • The starting point of your function, we’ll be making one from scratch
  • Name your function anything meaningful that describes what it does, for this article we’ll just name it tutorial_function
  • Runtime i.e which programming language you’ll be using. Select the one you’re comfortable in, we’ll be using Node.js 14.x and architecture x86_64

Screenshot from 2021-11-28 23-12-25.png

We don’t need to edit the Advanced settings, Permission and default execution role for the sake of this article.

Press the create Function button and et voila your first lambda function is created. In a few minutes you’ll be redirected to your newly created lambda function

Welcome to your Lambda function console

Screenshot from 2021-11-28 23-30-49.png

Ih you scroll down a bit, you’ll see the code editor

Screenshot from 2021-11-28 23-33-08.png

Now notice the file name and the node js function name

Filename - index.js

JS Function name - handler

This is the method invoked by AWS Lambda everytime it is triggered, you can see this in the runtime settings present further down on the page

Screenshot from 2021-11-28 23-36-22.png

If you change the name of your function or file that contains the function make sure to update the Runtime settings.

Analysing the default function given to us by AWS we can see that an event parameter is passed into the function which will be in the form of a JSON payload. Let’s modify the function to return the data passed in the event as it is

Screenshot from 2021-11-28 23-44-15.png

Remember to deploy the changes before we test them out

Now click on the test button, you’ll be asked to configure a test event. A test event is a way of mimicking a real event that serves as an input for your Lambda function again in the form of a JSON payload.

Choose any event you like to mimic from the templates, we’ll just be using the simple hello-world template. Change the key-value pairs to some meaningful data, I’ll put in my name and my twitter handle.

Screenshot from 2021-11-28 23-51-19.png

Create the test event and press on the Test button again. If everything goes right you should see an execution result similar to this

Screenshot from 2021-11-28 23-52-35.png

That’s it, you just deployed your first Lambda function, simple right? As simple as it may seem, these functions can help you make your project completely serverless.

Thank you for reading and Happy Coding