2021-03-19 17:38:54 +00:00
|
|
|
resource "aws_iam_role" "lambda_role" {
|
2022-10-09 16:42:18 +00:00
|
|
|
name = "${local.service_longname}_lambda-role"
|
2022-09-23 05:11:41 +00:00
|
|
|
assume_role_policy = <<EOF
|
2021-03-19 17:38:54 +00:00
|
|
|
{
|
|
|
|
"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
|
|
|
|
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_lambda_function" "lambda_func" {
|
2022-10-09 16:42:18 +00:00
|
|
|
function_name = "${local.service_longname}_function"
|
2022-09-23 05:11:41 +00:00
|
|
|
role = aws_iam_role.lambda_role.arn
|
|
|
|
handler = "src.base.handler"
|
|
|
|
runtime = "python3.8"
|
2021-03-19 17:38:54 +00:00
|
|
|
|
2022-09-23 05:11:41 +00:00
|
|
|
s3_bucket = var.artifacts_bucket_name
|
|
|
|
s3_key = var.lambda_archive_name
|
2022-10-09 16:42:18 +00:00
|
|
|
|
|
|
|
tags = local.common_tags
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_api_gateway_rest_api" "gateway" {
|
2022-10-09 16:42:18 +00:00
|
|
|
name = "${local.service_longname}_gateway"
|
2022-09-23 05:11:41 +00:00
|
|
|
description = "Lambda Boilerplate"
|
2022-10-09 16:42:18 +00:00
|
|
|
|
|
|
|
tags = local.common_tags
|
|
|
|
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_api_gateway_resource" "lambda_proxy" {
|
2022-09-23 05:11:41 +00:00
|
|
|
rest_api_id = aws_api_gateway_rest_api.gateway.id
|
|
|
|
parent_id = aws_api_gateway_rest_api.gateway.root_resource_id
|
|
|
|
path_part = "{proxy+}"
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_api_gateway_method" "lambda_proxy" {
|
2022-09-23 05:11:41 +00:00
|
|
|
rest_api_id = aws_api_gateway_rest_api.gateway.id
|
|
|
|
resource_id = aws_api_gateway_resource.lambda_proxy.id
|
|
|
|
http_method = "ANY"
|
|
|
|
authorization = "NONE"
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_api_gateway_integration" "lambda" {
|
2022-09-23 05:11:41 +00:00
|
|
|
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
|
2021-03-19 17:38:54 +00:00
|
|
|
|
2022-09-23 05:11:41 +00:00
|
|
|
integration_http_method = "POST"
|
|
|
|
type = "AWS_PROXY"
|
|
|
|
uri = aws_lambda_function.lambda_func.invoke_arn
|
2022-10-09 16:42:18 +00:00
|
|
|
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_api_gateway_deployment" "lambda" {
|
2022-09-23 05:11:41 +00:00
|
|
|
depends_on = [
|
|
|
|
aws_api_gateway_integration.lambda
|
|
|
|
]
|
2021-03-19 17:38:54 +00:00
|
|
|
|
2022-09-23 05:11:41 +00:00
|
|
|
rest_api_id = aws_api_gateway_rest_api.gateway.id
|
|
|
|
stage_name = "test"
|
2022-10-09 16:42:18 +00:00
|
|
|
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_lambda_permission" "apigw" {
|
2022-09-23 05:11:41 +00:00
|
|
|
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
|
|
|
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output "base_url" {
|
2022-09-23 05:11:41 +00:00
|
|
|
value = aws_api_gateway_deployment.lambda.invoke_url
|
2021-03-19 17:38:54 +00:00
|
|
|
}
|