Created
February 26, 2023 00:54
-
-
Save alaminopu/bbaf69cbfb0d36e5b9768d9f5c579c3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| provider "aws" { | |
| region = "us-east-1" | |
| } | |
| resource "aws_lambda_function" "my_fastapi_app" { | |
| filename = "my_fastapi_app.zip" | |
| function_name = "my-fastapi-app" | |
| role = aws_iam_role.lambda_exec.arn | |
| handler = "main.app" | |
| runtime = "python3.8" | |
| environment { | |
| variables = { | |
| "PORT" = "9000" | |
| } | |
| } | |
| # Add the following lines to reduce the size of the package | |
| # when uploaded to AWS Lambda | |
| depends_on = [ | |
| null_resource.package | |
| ] | |
| } | |
| resource "aws_apigatewayv2_api" "my_fastapi_app" { | |
| name = "my-fastapi-app" | |
| protocol_type = "HTTP" | |
| } | |
| resource "aws_apigatewayv2_integration" "my_fastapi_app" { | |
| api_id = aws_apigatewayv2_api.my_fastapi_app.id | |
| integration_type = "AWS_PROXY" | |
| integration_uri = aws_lambda_function.my_fastapi_app.invoke_arn | |
| } | |
| resource "aws_apigatewayv2_route" "my_fastapi_app" { | |
| api_id = aws_apigatewayv2_api.my_fastapi_app.id | |
| route_key = "GET /" | |
| target = "integrations/${aws_apigatewayv2_integration.my_fastapi_app.id}" | |
| } | |
| resource "aws_iam_role" "lambda_exec" { | |
| name = "lambda_exec" | |
| assume_role_policy = jsonencode({ | |
| Version = "2012-10-17" | |
| Statement = [ | |
| { | |
| Action = "sts:AssumeRole" | |
| Effect = "Allow" | |
| Principal = { | |
| Service = "lambda.amazonaws.com" | |
| } | |
| } | |
| ] | |
| }) | |
| } | |
| resource "aws_iam_role_policy_attachment" "lambda_exec_basic" { | |
| policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | |
| role = aws_iam_role.lambda_exec.name | |
| } | |
| resource "null_resource" "package" { | |
| triggers = { | |
| always_run = timestamp() | |
| } | |
| provisioner "local-exec" { | |
| command = "pip install -r requirements.txt -t ./" | |
| } | |
| } | |
| data "archive_file" "lambda_package" { | |
| type = "zip" | |
| source_dir = "." | |
| output_path = "my_fastapi_app.zip" | |
| depends_on = [ | |
| null_resource.package | |
| ] | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment