sondehub-infra/ingestion.tf

118 lines
3.8 KiB
Terraform
Raw Normal View History

2021-10-08 23:32:22 +00:00
data "archive_file" "api_to_iot" {
type = "zip"
source_dir = "sonde-api-to-iot-core/"
output_path = "${path.module}/build/sonde-api-to-iot-core.zip"
}
data "archive_file" "station_api_to_iot" {
type = "zip"
source_file = "station-api-to-iot-core/lambda_function.py"
output_path = "${path.module}/build/station-api-to-iot-core.zip"
}
resource "aws_lambda_function" "upload_telem" {
function_name = "sonde-api-to-iot-core"
handler = "lambda_function.lambda_handler"
filename = "${path.module}/build/sonde-api-to-iot-core.zip"
source_code_hash = data.archive_file.api_to_iot.output_base64sha256
publish = true
memory_size = 128
role = aws_iam_role.basic_lambda_role.arn
runtime = "python3.9"
timeout = 30
architectures = ["arm64"]
environment {
variables = {
"SNS_TOPIC" = aws_sns_topic.sonde_telem.arn
}
}
}
resource "aws_lambda_function" "station" {
function_name = "station-api-to-iot-core"
handler = "lambda_function.lambda_handler"
filename = "${path.module}/build/station-api-to-iot-core.zip"
source_code_hash = data.archive_file.station_api_to_iot.output_base64sha256
publish = true
memory_size = 128
role = aws_iam_role.basic_lambda_role.arn
runtime = "python3.9"
timeout = 10
architectures = ["arm64"]
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
}
resource "aws_lambda_permission" "station" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.station.arn
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.main.id}/*/*/listeners"
}
resource "aws_lambda_permission" "upload_telem" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.upload_telem.arn
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.main.id}/*/*/sondes/telemetry"
}
resource "aws_apigatewayv2_route" "stations" {
api_id = aws_apigatewayv2_api.main.id
api_key_required = false
authorization_type = "NONE"
route_key = "PUT /listeners"
target = "integrations/${aws_apigatewayv2_integration.stations.id}"
}
resource "aws_apigatewayv2_route" "upload_telem" {
api_id = aws_apigatewayv2_api.main.id
api_key_required = false
authorization_type = "NONE"
route_key = "PUT /sondes/telemetry"
target = "integrations/${aws_apigatewayv2_integration.upload_telem.id}"
}
resource "aws_apigatewayv2_integration" "upload_telem" {
api_id = aws_apigatewayv2_api.main.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.upload_telem.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
resource "aws_apigatewayv2_integration" "stations" {
api_id = aws_apigatewayv2_api.main.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.station.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
resource "aws_sns_topic" "sonde_telem" {
name = "sonde-telem"
delivery_policy = <<EOF
{
"http": {
"defaultHealthyRetryPolicy": {
"minDelayTarget": 5,
"maxDelayTarget": 30,
"numRetries": 100,
"numMaxDelayRetries": 0,
"numNoDelayRetries": 3,
"numMinDelayRetries": 0,
"backoffFunction": "linear"
},
"disableSubscriptionOverrides": false
}
}
EOF
}