Create and Integrate AWS API Gateway With AWS Lambda.

Rajan Sahu
5 min readJul 14, 2022

The main aim of this article is to access the AWS Lambda function with API Gateway.

What is AWS Lambda?

  • AWS Lambda is a Function-as-a-Service offering from Amazon Web Services.
  • Using this we can interact with most of the services of AWS.
  • We can write Lambda functions in multiple languages(Python, Java, Go, Node, etc).

And for this article, we will use Python while writing the Lambda functions.

What is AWS API Gateway?

  • Amazon API Gateway is a managed service that allows developers to define the HTTP endpoints of a REST API or a WebSocket API and connect those endpoints with the corresponding backend business logic.
  • It also handles authentication, access control, monitoring, and tracing of API requests.

Why do we need to integrate?

  • To access the AWS Lambda function over the HTTP methods.

Now let's start creating the AWS Lambda Function first(चलिये शुरू करते हैं ! )

Step 1:

  • Go to the AWS console and search for the Lambda function
  • Click on lambda and the top right-hand side click on Create function

And select the Author from scratch

  • In the basic information fill up the relevant details like function name and choose run time from the drop-down.
  • We are going to choose Python as a run-time environment.

Fill in the basic information as we did as below

  • In Author Name we Gave demolambda
  • Choose a run-time language, we choose Python 3.9
  • And we will keep the rest of the things as is.

After that Hit the Create Function Button

Now, let's start writing Scripts.

import jsondata ={ "Student_details":
[
{"roll_no": 1,"firstname": "Sonal", "lastname": "Sharma", "address":"Mumbai"},
{"roll_no": 2,"firstname": "Sumit", "lastname": "Agarwal", "address":"Assam"},
{"roll_no": 3,"firstname": "Mohit", "lastname": "Gupta", "address":"Goa"},
{"roll_no": 4,"firstname": "Rinku", "lastname": "Das", "address":"Gorakhpur"}
]
}
def lambda_handler(event, context):
if event['httpMethod'] =="GET":
return {
'statusCode': 200,
'body': json.dumps(data)
}
if event['httpMethod']=="POST":
new_record = json.loads(event['body'])
data['Student_details'].append(new_record)

return {
'statusCode':201,
'body': json.dumps('Created Successfully')
}

In the above script,

  • We are creating a lambda function that will return
  • All the records will be listed if the HTTP method is GET.
  • Or a record will be inserted if the HTTP method is POST.

Now let's create and integrate API Gateway with the Lambda function.

Step 1:

  • Again go to the AWS console and search for AWS API Gateway.
  • Click on it and a page will appear as below.

Step 2:

  • In Choose the protocol select the REST radio button
  • And in Create new API select the New API radio button
  • Give a name to the API name as we did in the above image.

Step 3:

  • Now, Clicked on create API you will redirect a page as shown in the below image.
  • Now click on the Actions button and create a Resource as we did in the image.
  • We gave the resource name Demo.

step 4:

  • Again, click on the Actions button and now we will create two HTTP methods GET and POST.
  • After creating GET and POST methods,
  • Now, click on the GET method, and will do GET-Setup as done in the below image.
  • In Integration Type, choose the Lambda Function radio button.
  • Click on Use Lambda Proxy Integration and Use Default Timeout(3 seconds) check box.
  • In Lambda Function we need to mention the Lambda function that we created earlier.

What is the difference between with and without Lambda Proxy Integrations?

  • This is a simple, but powerful integration. All the request to the API gateway URL is forwarded straight to the AWS Lambda function and the response is sent from Lambda. i.e No modifications to the request(query params, body, variables) and response(status code, message) are done by the API gateway.
  • This is complex but offers more control over transmission data. The request can be modified before it is sent to lambda and the response can be modified after it is sent from lambda. This can be done by mapping templates that transform the payload, as per the user customizations. API Gateway uses Velocity Template Language (VTL) engine to process body mapping templates for the integration request and integration response.

Step 5:

  • Now, again go to Action and click on Deploy if you earlier created a stage you can use it
  • Or create a new one.
  • Now click on Deploy, and it will redirect to as below page.

Important: Every time you update an API, you must redeploy the API to an existing stage or a new stage. Updating an API includes modifying routes, methods, integrations, authorizers, and anything else other than stage settings.

  • Finally, everything is successfully set up.

Step 6

  • Now, Open your Postman and make a POST request as we did.
  • In request body
{
"roll_no": 5,
"firstname": "Sonam",
"lastname": "Devi",
"address": "Delhi"
}

Now, it is your turn to create and check Both GET and POST methods.

  • If you have anything(Doubt, Suggestions) please do let me know in the comment section.

Reference

Thank you for reading. If you find something wrong or better ways to do it, let me know in the comments below.

If you like the post, hit the 👏 button below so that others may find it useful. You can follow me on GitHub and connect with me on Linkedin.

--

--

Rajan Sahu

Backend and Data Engineer by Day; Teacher, Friend and Content-Writer by night.