{"id":19850996,"url":"https://github.com/patheard/aws-fastapi-lambda","last_synced_at":"2025-05-01T23:30:55.631Z","repository":{"id":47743506,"uuid":"389395434","full_name":"patheard/aws-fastapi-lambda","owner":"patheard","description":"AWS API Gateway, Lambda and FastAPI + Mangum","archived":false,"fork":false,"pushed_at":"2021-08-29T20:43:24.000Z","size":64,"stargazers_count":17,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2023-03-26T17:06:44.292Z","etag":null,"topics":["aws-api-gateway","aws-lambda","fastapi","mangum","terraform","uvicorn"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patheard.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-25T16:43:36.000Z","updated_at":"2023-03-24T07:23:01.000Z","dependencies_parsed_at":"2022-09-23T18:22:48.719Z","dependency_job_id":null,"html_url":"https://github.com/patheard/aws-fastapi-lambda","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patheard%2Faws-fastapi-lambda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patheard%2Faws-fastapi-lambda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patheard%2Faws-fastapi-lambda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patheard%2Faws-fastapi-lambda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patheard","download_url":"https://codeload.github.com/patheard/aws-fastapi-lambda/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224281343,"owners_count":17285654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws-api-gateway","aws-lambda","fastapi","mangum","terraform","uvicorn"],"created_at":"2024-11-12T13:28:29.872Z","updated_at":"2024-11-12T13:28:30.629Z","avatar_url":"https://github.com/patheard.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AWS FastAPI Lambda\nCreates an API using an AWS API Gateway and Lambda function, based on [this walkthrough](https://towardsdatascience.com/fastapi-aws-robust-api-part-1-f67ae47390f9).  The setup uses:\n\n* **API:** [FastAPI](https://fastapi.tiangolo.com/) `+` [Mangum](https://mangum.io/)\n* **Infrastructure:** [Terraform](https://www.terraform.io/) `+` [Terragrunt](https://terragrunt.gruntwork.io/)\n\nRequests are sent to the API Gateway, which has one `/{proxy+}` resource.  This resource handles all requests using a [proxy integration with the Lambda function](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html).  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.\n\n```js\n          ┌─── AWS region ─────────────────────────────┐\n          │                  ┌─── VPC: three AZs ────┐ │\n          │                  │                       │ │\nRequest  ───►  API Gateway  ───►  Lambda (FastAPI)   │ │\n          │                  │                       │ │\n          │         │        └───────────│───────────┘ │\n          │ ┌───────│────────────────────│───────────┐ │\n          │ │       ▼      CloudWatch    ▼           │ │\n          │ └────────────────────────────────────────┘ │\n          └────────────────────────────────────────────┘\n```\n\nIf performance becomes an issue, a CloudFront distribution could be added for API responses that are cacheable.\n\n# Dev\n```sh\ncd api\nmake install\nmake install-dev\nmake serve\n```\nView the [API endpoints](http://localhost:8000/docs).\n\n# Deploy\n```sh\n# Create the zip that will be used for the Lambda function\ncd api\nmake zip\n\n# Create the AWS infrastructure\ncd ../infra/env/dev\nterragrunt run-all plan   # to see all the goodness that will get created\nterragrunt run-all apply  # create all the goodness\n```\n\n# Notes\n## Root path\nThe 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:\n```terraform\nresource \"aws_api_gateway_method\" \"api_gateway_root_method\" {\n  rest_api_id   = aws_api_gateway_rest_api.api_gateway.id\n  resource_id   = aws_api_gateway_rest_api.api_gateway.root_resource_id\n  http_method   = \"ANY\"\n  authorization = \"NONE\"\n}\n\nresource \"aws_api_gateway_integration\" \"api_proxy_integration\" {\n  rest_api_id             = aws_api_gateway_rest_api.api_gateway.id\n  resource_id             = aws_api_gateway_rest_api.api_gateway.root_resource_id\n  http_method             = aws_api_gateway_method.api_gateway_root_method.http_method\n  integration_http_method = \"POST\"\n  type                    = \"AWS_PROXY\"\n  uri                     = aws_lambda_function.api_lambda.invoke_arn\n}\n```\n## API key\nAn 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:\n```sh\ncurl --header \"x-api-key: ${API_KEY_VALUE}\" \\\n  https://${API_ID}.execute-api.${API_REGION}.amazonaws.com/dev/hello\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatheard%2Faws-fastapi-lambda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatheard%2Faws-fastapi-lambda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatheard%2Faws-fastapi-lambda/lists"}