Creating API Gateway to trigger Lambda Function

gray laptop computer showing html codes in shallow focus photography
Reading Time: 5 minutes

Hello Readers! I hope you are doing very well. So, In this blog we will create an API gateway to trigger lambda functions. Let’s talk about API gateway and after that we will trigger a lambda function using it.

API Gateway:

Api Gateways are just a web endpoint to your application. You can configure the target to be any aws service you want. So, It’s just an http/https endpoint for your needs. 

For example, you can create an API gateway, and you will get an endpoint address/url. Then you configure the backend service. It can be a lambda function or any AWS service as per your need. So users when they visit the API Gateway url, the lambda function gets triggered and the response is sent back to users. You can have S3, SNS and many services that can be configured as the backend for your api gateway.

So, Let’s get start!

Step1: Firstly for triggering a lambda function. We must have a function. So, let’s create a lambda function. Move to your AWS Console. Search for lambda and click on Create Function.

Lambda Function

Fill here the function name as your wish. I will select here the runtime as python3.8. 

Lambda Function

Click on Create Function.

Step2: I will now change the lambda_function.py because I want this lambda function for two methods. I will use two methods: get and post inside this function. So, Get method will be for getting a person’s details. Post method is for creating a person. And I want some lines to be printed in the logs which I have given inside the print statement. And when we will hit the api the output will be printed which I have given inside the return statement.

So, This is my code. Copy the code from below and paste inside your lambda functions. 

import json
import uuid

GET_RAW_PATH = "/getPerson"
CREATE_RAW_PATH = "/createPerson"

def lambda_handler(event, context):
    print(event)
    if event['rawPath'] == GET_RAW_PATH:
        print('Received getPerson request')
        personId = event['queryStringParameters']['personId']
        print("with param personId=" + personId)
        return { "firstName": "John " + personId, "lastName": "K", "email": "myemail@gmail.com" }
    elif event['rawPath'] == CREATE_RAW_PATH:
        print('Received createPerson request')
        decodedBody = json.loads(event['body'])
        firstname = decodedBody['firstname']
        print('with param firstname=' + firstname)
        return { "personId": str(uuid.uuid1())}
Lambda Function

Click on Deploy.

Step3: Create an API gateway. Move to AWS Console Management. Search for API gateway. Choose an API type. I will choose the HTTP API here. Click on Build.

API Gateway

Specify the backend services that your API will communicate with. These are called integrations. Inside Integrations select lambda. Then choose the region in which your lambda function is created. And Select the lambda function name. After that give the name for your API Gateway as per your wish.

api gateway

Click on Next.

Step4: This section is about routes. API Gateway uses routes to expose integrations to consumers of your API. You can choose GET, POST, PUT, PATCH, HEAD, OPTIONS, and DELETE as your wish. I will create two routes here one will be get and other will be post method. Add the resource path also.

routes

Click on Next.

Step5: Next step is configuring stages. Stages are independently configurable environments that your API can be deploy to. I will leave it as default. Click on Next. And Click on Create.

API Gateway

My API is now created.

Step6: Now, we need to test the API whether it’s working or not. Here I will use postman for hitting the API. Postman is an application use for API testing. If you don’t have the postman then do install that for using it. 

Copy the url from the API gateway that you have created. 

API Gateway

Open the postman and paste the url and choose the method as post and at the end of url add /createPerson.

Inside the input we have to give the person details such as firstname, lastname and email id. 

API hitting

Finally Click on Send. In the output we will get the unique personid. It is the uuid which is randomly generated.

API Gateway

And Inside the AWS CloudWatch we can check the logs for our lambda function.

Cloudwatch logs

So, yaa, it’s working correctly. Now, let’s check the get method. Paste the url and choose the method as get and at the end of URL add /getPerson. Inside the parameters give key as personId and value as personId123.

Let’s check the output by clicking on send.

API Gateway

Yes we are getting the details of the person of personId123. So, We have successfully created an API gateway and we are able to hit the lambda function through this API.

We are all done now!

Conclusion

Thank you for sticking to the end. So, In this blog we have seen how easily we can create an API gateway and hitted a lambda function using this API. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on So that I can improve my future posts to suit your needs.

HAPPY LEARNING!

Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading