This repository has been archived on 2024-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
lambda-boilerplate/infrastructure/app/main.tf

89 lines
2.2 KiB
Terraform
Raw Normal View History

resource "aws_iam_role" "lambda_role" {
2022-10-09 16:42:18 +00:00
name = "${local.service_longname}_lambda-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
2022-10-09 16:42:18 +00:00
tags = local.common_tags
}
resource "aws_lambda_function" "lambda_func" {
2022-10-09 16:42:18 +00:00
function_name = "${local.service_longname}_function"
role = aws_iam_role.lambda_role.arn
handler = "src.base.handler"
runtime = "python3.8"
s3_bucket = var.artifacts_bucket_name
s3_key = var.lambda_archive_name
2022-10-09 16:42:18 +00:00
tags = local.common_tags
}
resource "aws_api_gateway_rest_api" "gateway" {
2022-10-09 16:42:18 +00:00
name = "${local.service_longname}_gateway"
description = "Lambda Boilerplate"
2022-10-09 16:42:18 +00:00
tags = local.common_tags
}
resource "aws_api_gateway_resource" "lambda_proxy" {
rest_api_id = aws_api_gateway_rest_api.gateway.id
parent_id = aws_api_gateway_rest_api.gateway.root_resource_id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "lambda_proxy" {
rest_api_id = aws_api_gateway_rest_api.gateway.id
resource_id = aws_api_gateway_resource.lambda_proxy.id
http_method = "ANY"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambda" {
rest_api_id = aws_api_gateway_rest_api.gateway.id
resource_id = aws_api_gateway_resource.lambda_proxy.id
http_method = aws_api_gateway_method.lambda_proxy.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_func.invoke_arn
2022-10-09 16:42:18 +00:00
}
resource "aws_api_gateway_deployment" "lambda" {
depends_on = [
aws_api_gateway_integration.lambda
]
rest_api_id = aws_api_gateway_rest_api.gateway.id
stage_name = "test"
2022-10-09 16:42:18 +00:00
}
resource "aws_lambda_permission" "apigw" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_func.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.gateway.execution_arn}/*/*"
2022-10-09 16:42:18 +00:00
}
output "base_url" {
value = aws_api_gateway_deployment.lambda.invoke_url
}