Creating a simple API in aws lambda

There has been quite a buzz around serverless computing this year, and amazon has been at the fore front of this buzz. With The AWS Lambda you can deploy your scripts without worrying about the hosting and hosting costs. Cost effectiveness is one of the main plus point of serverless computing. Instead of having a dedicated server just to run a script- Lambda provides resources and bills you for the time your scripts were running.


Today we will be building just a simple API service using

  1. Lambda
  2. API Gateway

We need to use API gateway because serverless computing provides you with the bare minimum computing resources, anything extra you need to connect manually. Kinda like building blocks for your server. You keep only what you need.

#Lambda Function

Things to keep in mind

  1. We will be working on Python 3.6 environment.
  2. Any library which is not included in the Python 3.6 inbuilt library needs to be uploaded along with the code
  3. The function invoked when an event occurs is def Lambda_handler(event, context):
  4. Lambda functions are event driven. That is, they will execute only when an event occurs for which you have predifined the triggers

So let’s make an example API to send you a gif everytime you send it a get request

For the gifs we will be using gify api
The usual package used requests for requests in Python in not included in the standard library, since this is just a intro to lambda-we will not be using it. Instead we will be using urllib.request.Request which is included in the Python standard library.

#Getting started
  1. Go to aws home and select Lambda
  2. Go to your Lambda dashboard and click on Create a Lambda function
  3. Select a blank function
  4. Give your function a name and select the environment as Python 3.6
  5. As said earler the Lambda handler function will act as the main method.

Using the above code you can check out what happens in event whenever we invoke the api call.
We are making our API only for the GET method. Hence our event["context"]['http-method'] should be equal to GET.
Next, our query string can be found in event["params"]["querystring"]["query"]
With the above information, lets make an event handler.

Now we have the the handler working properly, lets make a request to giphy and send back the first gif that we get.
We will also make a respond function to make it easier to send response from the server.
The final product should look like this:

#API Connection

Now that your function is ready, your work is still not over. You need to create a URL in aws api gateway and connect it to your function.
Go to your console and search for API Gateway
Go to your API Gateway console
Click on create API
Give your API a name and click on Create API
Enter the name
Click on create method
In the method dropdown select GET.
These are the settings which I have used, your’s may vary slightly

Click on save

Now click on the integration request
Click on Use Lambda Proxy integration
Now go to Method Request and define your query string parameter

Then add your query as follows:

Now that we are done with all the settings, let us deploy the API.
Click on action button and select Deploy API

Then click on deploy.

You will get a URL somethig like this
You may now add your your query string to the url and test it out.
You can try it out in your browser

#Wrapping it up

You have thus succesfully deployed a aws Lambda instance and connected it to an API gateway. At the end of this tutorial your api should give you gifs for your query string


If you have any issues, feel free to contact me.

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

About Me

About Me

Hey, I am Thomas Ashish Cherian. What I cannot create, I do not understand.

Social Profiles