Coder Social home page Coder Social logo

patheard / aws-fastapi-lambda Goto Github PK

View Code? Open in Web Editor NEW
33.0 4.0 5.0 64 KB

AWS API Gateway, Lambda and FastAPI + Mangum

License: MIT License

Makefile 2.16% Python 9.09% Dockerfile 6.67% HCL 82.07%
fastapi mangum aws-lambda aws-api-gateway terraform uvicorn

aws-fastapi-lambda's Introduction

AWS FastAPI Lambda

Creates an API using an AWS API Gateway and Lambda function, based on this walkthrough. The setup uses:

Requests are sent to the API Gateway, which has one /{proxy+} resource. This resource handles all requests using a proxy integration with the Lambda function. Mangum acts as a wrapper, which allows FastAPI to handle the requests and create responses the API gateway can serve. All logs are sent to CloudWatch log groups.

          ┌─── AWS region ─────────────────────────────┐┌─── VPC: three AZs ────┐ 
                                                    
Request  ───►  API Gateway  ───►  Lambda (FastAPI)    
                                                    
                           └───────────│───────────┘ 
           ┌───────│────────────────────│───────────┐ 
                        CloudWatch                
           └────────────────────────────────────────┘ 
          └────────────────────────────────────────────┘

If performance becomes an issue, a CloudFront distribution could be added for API responses that are cacheable.

Dev

cd api
make install
make install-dev
make serve

View the API endpoints.

Deploy

# Create the zip that will be used for the Lambda function
cd api
make zip

# Create the AWS infrastructure
cd ../infra/env/dev
terragrunt run-all plan   # to see all the goodness that will get created
terragrunt run-all apply  # create all the goodness

Notes

Root path

The API gateway proxy integration with the Lambda function does not include a / root path. If you need a root path, you'll need to add this method integration:

resource "aws_api_gateway_method" "api_gateway_root_method" {
  rest_api_id   = aws_api_gateway_rest_api.api_gateway.id
  resource_id   = aws_api_gateway_rest_api.api_gateway.root_resource_id
  http_method   = "ANY"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "api_proxy_integration" {
  rest_api_id             = aws_api_gateway_rest_api.api_gateway.id
  resource_id             = aws_api_gateway_rest_api.api_gateway.root_resource_id
  http_method             = aws_api_gateway_method.api_gateway_root_method.http_method
  integration_http_method = "POST"
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_function.api_lambda.invoke_arn
}

API key

An API key is created and required by the API gateway. You can retrieve the key from the API's usage plan in the AWS console. To use the key:

curl --header "x-api-key: ${API_KEY_VALUE}" \
  https://${API_ID}.execute-api.${API_REGION}.amazonaws.com/dev/hello

aws-fastapi-lambda's People

Contributors

patheard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

aws-fastapi-lambda's Issues

Documentation request

Hello Patheard,

Thank you very much for this work, it brings mangum to an actionable level. I just wonder why there is a sqs module as it is not mentioned in the walkthrough nor in the README. I think the answer is no, but is the sqs necessary to deploy the infra in aws?

Thank you in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.