Skip to content

subhamay-bhattacharyya/1111-api-gateway-py-sam

Repository files navigation

         


Table of Contents


Create a API Gateway Rest API endpoint with Lambda proxy integration using AWS SAM as IaaC and Python as Lambda runtime.

This project demonstrates how to deploy an API Gateway endpoint integrated with an AWS Lambda function using Python. AWS Serverless Application Model (SAM) is used as the Infrastructure as Code (IaC) tool to define and manage the deployment.

This project has been inspired by AWS API Gateway tutorial - Create a REST API with a Lambda proxy integration

🏗 Architecture

Below is the architecture of the application:

  • Amazon API Gateway: Handles HTTP requests and routes them to Lambda.
  • AWS Lambda (Python): Processes requests and returns responses.
  • AWS SAM: Deploys and manages the infrastructure as code.

AWS Architecture

AWS Services Used

Overview

This architecture illustrates a serverless API deployment using AWS API Gateway, AWS Lambda, and a VPC with private subnets.

Architecture Components

1. AWS Cloud Environment

  • The entire setup is hosted within an AWS Cloud region.

2. Virtual Private Cloud (VPC)

  • A VPC (Virtual Private Cloud) is created to host private resources.
  • Network Access Control Lists (NACLs) are configured for security.

3. Private Subnets

  • Two private subnets are deployed in separate Availability Zones (A and B).
  • Each subnet hosts an AWS Lambda function, ensuring redundancy and high availability.

4. Route Tables

  • The architecture uses private route tables with IP ranges:
    • 172.16.0.0
    • 172.16.1.0
    • 172.16.2.0
  • These subnets do not have direct internet access.

5. AWS Lambda Functions

  • Lambda functions are deployed inside private subnets.
  • They handle backend logic and process API requests.

6. API Gateway

  • Amazon API Gateway is the public-facing endpoint for receiving HTTP requests.
  • Routes incoming traffic to the appropriate AWS Lambda function.

7. Interface Endpoints

  • AWS Interface Endpoints are used for private communication between AWS services.
  • Allow Lambda functions to interact with AWS Public Services without requiring an Internet Gateway or NAT Gateway.

8. AWS Public Services

  • Represents AWS-managed services that API Gateway or Lambda functions might access, such as:
    • Amazon S3
    • DynamoDB
    • Other AWS APIs

9. Users

  • End-users send HTTP requests to API Gateway, which invokes the backend Lambda functions securely.

10. AWS Power Tools for Lambda

AWS Powertools for AWS Lambda is a suite of utilities that helps developers build serverless applications faster while following best practices. It provides features like structured logging, tracing, metrics, and various utilities to enhance AWS Lambda functions. AWS Powertools is available for Python, Java, .NET, and TypeScript.

Key Design Considerations

  • Security: Lambda functions reside in private subnets for restricted access.
  • Scalability: API Gateway and Lambda automatically scale based on traffic.
  • Reliability: Multi-AZ deployment ensures fault tolerance.
  • Cost Optimization: Uses AWS Serverless services to reduce infrastructure costs.

Conclusion

This architecture is ideal for secure, scalable, and cost-efficient serverless API deployments using AWS Lambda within a VPC.


🛠 Prerequisites

Before setting up the project, ensure you have:

  • An AWS account

Ensure you have the following installed:


🚀 Installation & Setup

Follow these steps to set up the project locally:

1. Clone the Repository

git clone https://github.com/your-repo/aws-api-gateway-sam.git
cd aws-api-gateway-sam

2. Install Dependencies

pip install -r src/requirements.txt

3. Build the SAM Application

sam build

4. Deploy to AWS

sam deploy --guided

Follow the prompts to provide the necessary configuration, such as stack name and AWS region.

5. Test the API Endpoint

Once deployed, retrieve the API Gateway URL from the output and test it using:

curl https://your-api-id.execute-api.your-region.amazonaws.com/Prod/

Cleanup

To delete the deployed resources:

sam delete
mindmap
  root )AWS Cloud(
    VPC
      Network ACL
         Allow and Deny rules
      Subnets
        Route Table
        Private Subnet
      Security Group
        Security Group Rule
      VPC Endpoints
    Lambda
    API Gateway
      Rest API Lambda Proxy Integration
Loading

Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this project better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request
Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Subhamay Bhattacharyya Linkedin LinkedIn GitHub GitHub Email Badge Project Link: https://github.com/subhamay-bhattacharyya/1111-api-gateway-py-sam

About

✅ Create a REST API with Python Lambda proxy integration using AWS SAM (Serverless Application Model) as IaaC tool.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages