A very brief introduction to Labmda and API Gateway

caortizpacheco
Nov 1 · 4 min read

Amazon API Gateway is one of the core services of AWS, the API Gateway is a fully managed serverless service from AWS and it allows us to create, publish, and run secure APIs at any scale. Starting from this we will see some points for which we should use API Gateway:

  1. We can create API endpoints to integrate frontend applications with its backend, using RESTful APIs over HTTPS.
  2. Being a serverless offering, does not require us to provision any servers.
  3. It allows us to build APIs that are extremely low cost, highly scalable, secure and easy to monitor as well.
  4. It's a fully managed service and can handle thousands of concurrent API calls.
  5. We can use it to create RESTful APIs endpoints that communicate over HTTPS and at the backend.
  6. We can connect the API to different AWS Services.
  7. We can easily add authentication or authorization to our APIs as well.

In this example we are going to use Lambda and API Gateway services, both of them from AWS.

first of all, we are going to create a Lambda function.

Fig 1 AWS Lambda Function; Create function

Open your AWS console and go to Lambda. We are going to create a Lambda function, so click on Create function.

Fig 2 AWS Lambda; Create Function from scratch

For this exercise we are going to create a Author from scratch.

Fig 3 AWS Lambda; Create Function ; Basic Information

Inside Basic Information, write the name of your lambda function, select Create a new role from AWS policy templates and assing a Role name.

Finally click on create function.

Fig 4 AWS Lambda; Function created

Now you have a lambda function created with some default code.

If you want to see the default code, click on geologyRocks and scroll down.

The next step is going to API Gateway to create an API.

Fig 5 AWS API Gateway; Create new API

First go to API Gateway console and create an API. The first steps is to create a REST API, select New API and assing it an API name. Finally click on Create API.

When your API is created you need to:

  1. Create a Resource (path), with the Actions button, and assing it a Resource name.
  2. Create a Method (get, post, delete, etc.), with the Actions button.
  3. Assign the lambda function to our API in the Integration Request section.
Fig 6 AWS API Gateway; API's flow
Fig 7 AWS API Gateway; Integration Request

This screen corresponds to the step number 3 (Assign the lambda function to our API in the Integration Request section). In Integration type Select Lambda Function to integrate a Lambda to our API and in Lambda Function write the name of the Lambda you created.

Now we are going to test our API, click on TEST (Fig 6). We are going to see a view like this:

Fig 8 AWS API Gateway; TEST; click on Test button to see the response body of the get method

Finally click on Actions button (Fig 6) and select Deploy API to deploy our API, assign a stage (prod, test, dev, etc.) and click on Deploy.

You will get the invoke URL, copy this URL in your browser and check if your URL works. Be careful if you see a error message like this {“message”:”Missing Authentication Token”}, you just need to add the resource path (/geologymessage) to your URL

Fig 9 AWS API Gateway; TEST; The image on the left side is with the incomplete invoke URL, the image on the right side is the invoke URL with the resource path (/geologymessage)

If you want to customize your response you need to go to Integration Response (Fig 6) and modify the Mapping Templates, click on application/json and modify the template. $input.body is the data that comes from the response of AWS Lambda.

I hope this little introduction will help you to know how to create an API method get and recieve a customize response.

I would like to know your doubts or comments, please email me at ca.ortiz.pacheco@gmail.com.

caortizpacheco

Written by

Developer fullstack. Passionate about cloud services manly of serverless functions. Willing to teach to grow together. For the benefit of all.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade