Monday, March 27, 2023

Serverless HTTP API - Using AWS API Gateway, Lambda and Python

Table of Content

Create AWS API Gateway Service

Login to your AWS console – you can search API gateway in search box or if you have already visited it will show in recently visited info…

Click on API Gateway service – you will navigate to Build API gateway page where you can find different of options to create API gateway like

Based on Your requirement you can choose any option – in our case we are building HTTP Rest Api with different routes, so we chose HTTP API Option

Choose HTTP API and click on Build – You will navigate to create API page like below

There are 4 steps -

  1. Create API
  2. Configure Routes
  3. Define Stage
  4. Review and Create

Create API

In this page you have to provide two inputs

  1. Integrations: you have to specifybackend service which perform some task execution once this API triggered like Lambda Function or any other HTTP API like below

    at this moment this part is optional you can specify this at later stage as well

  2. API Name: Mandatory, it will be part of your API list so this should meaningful and unique. May be you can choose name related to your project.

Configure Routes

Click Next – you will navigate to Configure routes page like below

In One API Gateway we can create multiple routes of different types of HTTP methods so at this moment don’t add any things and click Next for navigating to next steps

Define Stages

Stages are nothing but different environment for you project like DEV/QA/STAGE/UAT/Production so you have already created different stage in aws you can integrate/ assign here ..

Also Server less API gateway also come with default stage you can proceed with that and at later stage you can create and change the stage .. Default stage provide auto deploy option ..

Review and Create

Verify All steps and click on create – once it is successful it will navigate to New API gateway main page like below

For every API Gateway AWS provide a unique public URL (End Points) so that this can be consumed through that ..

Like this https://xxxxxxx.execute-api.ap-xxxxxx-1.amazonaws.com

If you have already created different stages with your domain or sub domain name this URL will be look like your domain name

Edit API Gateway:

At this moment if want to do some changes in API gateway, you can changes through Edit Option

Once you click Edit button you will navigate to Edit page

From this page you can only change 3 things

  • API Name
  • API Description
  • And you can enable or disable API

You can also add tags here for this cloud resource

Creating New Route:

If you have multiple APIs gateway in API gateway dashboard page it will look like this

Click on Name link in the API List grid in which you want to add new routes then it will navigate to selected API Dashboard

In this page we have we have Left menu with different options like Develop, Deploy, Monitor. In develop section you can find Routes option from this menu we can navigate to Routes management page where we can add/edit/delete different routes for this API.

Currently we have not created any routes for this API that is why this list is empty so lets up go create new route by clicking on Create Button -

Here we need to decide http method like get/put/post/delete/any and route name.

For this demo lets choose get and name like getProductList (name: should be unique for each route in this api)

Click on Create button – once operation is successful it will navigate to route list page

Now we have successfully created new route under out test API as getProductList. So the public URL for this route will be like this

API Gateway URL/getProductList

Eg :https://xxxxxxxxxxx.execute-api.ap-xxxxxxxx.amazonaws.com/getProductList

In this page if you click on GET Link it will show route details like below

In Details section mainly there are two parts

  1. Authorization - For Securing your API using IAM service or any API based custom policy
  2. Integration – For integrating backing service like lambda function which execute some code/task when this route hits or any other HTTP API can trigger

Also there are two more option Delete and Edit for deleting/updating this route

For this demo till now we have created

  1. API Gateway
  2. One Route (getProductList)

Now let us create a simple lambda function which return dummy product name list. Once this lambda is ready, we will integrate the same lambda in this route (getProductList) through Attach Integration option.

Create AWS Lambada Funtion

Click on search option and find lambda function it will navigate to lambda dashboard

Click on Create Function it will navigate to AWS Lambda creation page –

AWS Lambda function can be created from three different way

  1. Author from Scratch – start with simple hallo world example
  2. Use a blueprint – Create based on some template or sample code and configuration
  3. Container Image – if you have already container image select this option to deploy your function

For this demo let’s choose simple first option “Author from Scratch” and Python 3.9 as a Runtime info

Put Function name as getProductListLambda and let’s leave other options as default

And hit Create Function it will new getProdcutListLambda function and navigate to lambda page like below

Go to code section and past this simple python script and click on deploy

Once Deployed Click on Test button it will open test configuration page where we can define any inputs parameter for calling this aws lambda …

But in our can case we are not going to pass any input parameter so just enter event name and test click on save

Now Testing configuration is saved Click on Test button again it will give you result like below ..Meaning our aws lambda is working fine and now we can integrate with API routes

Integarte Lambada Function with API Gateway

There are two ways to configure lambda with API gateway route

  1. From Lambada Configuration tab

    Click on Configuration it will navigate to general configuration details

    In left menu you will find Triggers option and then click on add trigger

    From Trigger Configuration dropdown select API gateway

    After Selecting this choose Use Existing API option and choose already created “testing-Api-service”

    Select stage as $default and security and open for now

    And finally Click on Add it will integrate with already create API gateway.

  2. Navigate to your API Gateway Route and click on GET method you will see the attached integration option and click on that.

    Then you will see Create and attach an integration click on that you will navigate to integration target page

    From integration type select lambda then Integration details section will show. There you must choose AWS Region where you have already created Lambda function In lambda function dropdown select your lambda function which you want to integration with this API.

    Leave other options as default and hit on create once integration is successful it will look like this

Test AWS Serverless API

Time for Testing API Gate way Route (getProductList):

Final URL Of this route is - https://xxxxxx.execute-api.ap-xxxxxxx-1.amazonaws.com/getProductList Open Browser and paste this URL and hit enter – you will see the repose as below which coming from Aws Lambda using API gate way

Another Way Open Postman and create new Get request and paste this URL and see the result

Popular Articles