Using AWS Lambda functions with AWS API Gateway

So lately I am getting my hands dirty on AWS (again). It seems that this year was the year of serveless applications for micro-services (next year is going to be the evolution of this model).

In my opinion Firebase started this a long time ago, providing an abstraction layer on top of complex server stacks and providing an easy way to scale from there, but it had a small set of features and some limits.

Lambdas from AWS is the evolution of that idea, basically Amazon is re-inventing (see what I did here? “re-invent”) themselves allowing you to pay only for the resources you use while automating the whole process of scaling a server-side infra-structure.

So what you are going to learn here?

Basically I will walk you through the process of creating your first Lambda function that will use an API Gateway to display its results. Simple, right? So, let’s keep going.

You will need a few things:

  1. An AWS account, create one here (you will need your credit card);
  2. Some knowledge of JavaScript (NodeJS is a bonus);
  3. 20 minutes of your time (maybe less).

In the AWS Service search box search for “Lambda”:

AWS Service Search box

Once you get to the Lambda page select “Create a Function”.

We are going to create our function from scratch. I named my function as “HelloWorld”. I am using the option “choose an existing role” with role “lambda_basic_execution” (roles deserve a whole new post).

Creating Lambda from Scratch

Select “Create Function”.

Our HelloWorld Lambda

Click your Lambda “HelloWorld” function (if not selected already). Scroll down to the Function Code section.

Lambda simple Hello World code

At the top of your browser select the “Test” button.

Test options

You will see the following dialog window:

Test Event Dialog Window

You will need to name your test event. I named mine “TestHello”.

Hit the “Create” button.

Now with your test event created and selected (image below) you can click “Test” again to run a quick test.

Test Event Created and Selected

If you expand the “details” dropdown you will be able to see the “hello from lambda” generated by our default function.

Congrats! You have just created and tested your first Lambda function.

Now things start to get interesting.

Let’s modify our function to generate a JSON response that will be available to everyone using an API created with the AWS API Gateway service.

Modifying our Lambda Function

With our Function selected scroll down to the “Function code” section again.

Enter the following code for the index.js file:

Our new function code

The important part for us here is the “body” section of the response variable. I am only returning our “hello world” message and using the callback function to sending it back to the requester (in our case the API we are about to create).

Hit “Save” at the top of the browser window.

Adding the API Gateway Service

Under Configuration let’s add the “API Gateway” Trigger.

Adding API Gateway Trigger

Scroll down to the “Configure Trigger” options:

I changed the API name to “HelloWorldAPI” (you need to click the “enter value” button to be edit the default) and changed the security to “Open”.

ATTENTION: The API endpoints and information will be accessible publicly by anyone out there. If you need to secure your information you should use IAM or Access Key.

Click the “Add” button at the end of the options.

Click the “Save” button at the top of the browser window.

In your API Gateway settings you will find an invoke URL option now. Click the URL to display your Message on a new browser window.

That is it. You have just created your first API using AWS Lambdas.

Our API response

This is just the tip of the iceberg.

See you next time.