Integrating Amazon API Gateway with AWS Lambda

Amazon Web Services (AWS) offers powerful tools for building serverless applications, and two key components of this ecosystem are AWS Lambda and Amazon API Gateway. This guide will walk you through integrating API Gateway with AWS Lambda to create a fully functional REST API.

What is AWS Lambda?

AWS Lambda is a Function-as-a-Service (FaaS) offering from AWS that allows developers to execute code without provisioning or managing servers. It supports multiple programming languages like Python, Java, Go, and Node.js. For this, we will use Python.

Key features:

  • Automatic scaling.
  • Pay-as-you-go pricing model.
  • Seamless integration with other AWS services.

What is Amazon API Gateway?

Amazon API Gateway is a managed service that enables developers to:

  • Define HTTP or WebSocket API endpoints.
  • Connect these endpoints to backend logic (e.g., AWS Lambda, EC2).
  • Handle authentication, access control, monitoring, and tracing of API requests.

API Gateway simplifies exposing backend services to the internet securely and efficiently.

Related read: Deploying Go App On AWS EC2 Server

Why Integrate AWS Lambda with API Gateway?

Integrating these services allows you to:

  • Trigger Lambda functions via HTTP requests (e.g., GET, POST).
  • Build serverless REST APIs.
  • Leverage API Gateway’s features like throttling, caching, and authorization.

Now, let’s dive into the implementation.

Build Scalable and Efficient Applications with Ease

Step 1: Create an AWS Lambda Function

  1. Go to the AWS Management Console and search for Lambda.
  2. Click the Create function and select Author from scratch.
  3. Fill in the basic information:
    1. Function name: demoLambda
    2. Runtime: Python 3.9 (or your preferred version)
  4. Click the Create function.

Once the function is created, write a basic Python script to handle GET and POST requests.

Example Lambda Function

import json

data = {
"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)
}
elif event['httpMethod'] == "POST":
new_record = json.loads(event['body'])
data['Student_details'].append(new_record)
return {
'statusCode': 201,
'body': json.dumps('Created Successfully')
}
  • GET: Returns a list of student details.
  • POST: Adds a new student record.

Save and deploy the Lambda function.

Step 2: Create an API in API Gateway

  1. In the AWS Management Console, search for API Gateway.
  2. Click Create API and select HTTP API or REST API. For this example, choose REST API.
  3. Under Create new API, select New API and provide a name (e.g., demoAPI).
  4. Click Create API.

Step 3: Create Resources and Methods

  1. In the Resources section of your API, click Actions > Create Resource.
  2. Provide a name for the resource (e.g., demo) and click Create Resource.
  3. Select the newly created resource and click Actions > Create Method.
  4. Choose GET and click the checkmark.
  5. For the integration type, select Lambda Function and enter your Lambda function name (demoLambda).
  6. Enable Lambda Proxy Integration.

Repeat steps 3–5 to create a POST method.

Lambda Proxy vs. Non-Proxy Integration

  • Lambda Proxy Integration: Forwards the entire request to Lambda without modification. Lambda handles the request and response.
  • Non-Proxy Integration: Allows modification of request/response using mapping templates.

Step 4: Deploy the API

  1. Click Actions > Deploy API.
  2. Create a new stage (e.g., dev) and click Deploy.
  3. Copy the Invoke URL provided for the deployed API stage.

Step 5: Test the API

Use a tool like Postman or Curl to test your API.

GET Request

Endpoint: <Invoke URL>/demo

Example Response

{
"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"}
]
}

POST Request

Endpoint: <Invoke URL>/demo

Request Body

{
"roll_no": 5,
"firstname": "Sonam",
"lastname": "Devi",
"address": "Delhi"
}

Response

"Created Successfully"

Advanced Configurations

1. Add Input Validation

  • Use Request Parameters and Request Body validations in API Gateway to ensure only valid data reaches your Lambda function.

2. Enable CORS

  • If your API is called from a web application, enable Cross-Origin Resource Sharing (CORS) in the API Gateway settings.

3. Secure Your API

  • Use IAM Authentication, API Keys, or Custom Authorizers to secure your API.
coma

Conclusion

Integrating Amazon API Gateway with AWS Lambda provides a serverless, cost-effective way to build and deploy APIs. This highly scalable and flexible integration makes it ideal for a wide range of applications, from simple backend services to complex microservice architectures. Experiment with advanced configurations to fully utilize the capabilities of both services!

Keep Reading

Keep Reading

A Deep Dive into Modern Clinical Workflows with AI Agents & CDS Hooks

Register Now
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?