sondehub-infra/recovered.tf

243 lines
7.8 KiB
Terraform
Raw Permalink Normal View History

2021-06-06 01:19:58 +00:00
resource "aws_iam_role" "recovered" {
path = "/service-role/"
name = "recovered"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
EOF
max_session_duration = 3600
}
resource "aws_iam_role_policy" "recovered" {
name = "recovered"
role = aws_iam_role.recovered.name
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "logs:CreateLogGroup",
"Resource": "arn:aws:logs:us-east-1:${data.aws_caller_identity.current.account_id}:*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"arn:aws:logs:us-east-1:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/*"
]
},
{
"Effect": "Allow",
"Action": "es:*",
"Resource": "*"
},
{
"Action": [
"secretsmanager:GetSecretValue"
],
"Effect": "Allow",
"Resource": ["${aws_secretsmanager_secret.mqtt.arn}", "${aws_secretsmanager_secret.radiosondy.arn}"]
2021-06-06 01:19:58 +00:00
}
]
}
EOF
}
resource "aws_lambda_function" "recovered_get" {
function_name = "recovered_get"
2021-12-20 06:02:02 +00:00
handler = "recovered.get"
2021-12-20 08:30:41 +00:00
s3_bucket = aws_s3_bucket_object.lambda.bucket
s3_key = aws_s3_bucket_object.lambda.key
2021-12-20 06:02:02 +00:00
source_code_hash = data.archive_file.lambda.output_base64sha256
2021-06-06 01:19:58 +00:00
publish = true
memory_size = 128
role = aws_iam_role.recovered.arn
2021-10-04 08:08:54 +00:00
runtime = "python3.9"
2021-06-06 01:19:58 +00:00
timeout = 30
reserved_concurrent_executions = 100
environment {
variables = {
2021-10-08 11:17:11 +00:00
"ES" = aws_route53_record.es.fqdn
2021-06-06 01:19:58 +00:00
}
}
tags = {
Name = "recovered_get"
}
2021-06-06 01:19:58 +00:00
}
2021-12-20 01:10:24 +00:00
resource "aws_lambda_function" "recovered_stats" {
function_name = "recovered_stats"
2021-12-20 06:02:02 +00:00
handler = "recovered.stats"
2021-12-20 08:30:41 +00:00
s3_bucket = aws_s3_bucket_object.lambda.bucket
s3_key = aws_s3_bucket_object.lambda.key
2021-12-20 06:02:02 +00:00
source_code_hash = data.archive_file.lambda.output_base64sha256
2021-12-20 01:10:24 +00:00
publish = true
memory_size = 128
role = aws_iam_role.recovered.arn
runtime = "python3.9"
timeout = 30
reserved_concurrent_executions = 100
environment {
variables = {
"ES" = aws_route53_record.es.fqdn
}
}
tags = {
Name = "recovered_stats"
}
2021-12-20 01:10:24 +00:00
}
2021-06-06 01:19:58 +00:00
resource "aws_lambda_function" "recovered_put" {
function_name = "recovered_put"
2021-12-20 06:02:02 +00:00
handler = "recovered.put"
2021-12-20 08:30:41 +00:00
s3_bucket = aws_s3_bucket_object.lambda.bucket
s3_key = aws_s3_bucket_object.lambda.key
2021-12-20 06:02:02 +00:00
source_code_hash = data.archive_file.lambda.output_base64sha256
2021-06-06 01:19:58 +00:00
publish = true
memory_size = 128
role = aws_iam_role.recovered.arn
2021-10-04 08:08:54 +00:00
runtime = "python3.9"
2021-06-06 01:19:58 +00:00
timeout = 30
reserved_concurrent_executions = 100
environment {
variables = {
2021-10-08 11:17:11 +00:00
"ES" = aws_route53_record.es.fqdn
2021-06-06 01:19:58 +00:00
}
}
tags = {
Name = "recovered_put"
}
2021-06-06 01:19:58 +00:00
}
resource "aws_lambda_permission" "recovered_get" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.recovered_get.arn
principal = "apigateway.amazonaws.com"
2021-10-08 11:17:11 +00:00
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.main.id}/*/*/recovered"
2021-06-06 01:19:58 +00:00
}
2021-12-20 01:10:24 +00:00
resource "aws_lambda_permission" "recovered_stats" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.recovered_stats.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}/*/*/recovered/stats"
}
2021-06-06 01:19:58 +00:00
resource "aws_lambda_permission" "recovered_put" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.recovered_put.arn
principal = "apigateway.amazonaws.com"
2021-10-08 11:17:11 +00:00
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.main.id}/*/*/recovered"
2021-06-06 01:19:58 +00:00
}
resource "aws_apigatewayv2_integration" "recovered_get" {
2021-10-08 11:17:11 +00:00
api_id = aws_apigatewayv2_api.main.id
2021-06-06 01:19:58 +00:00
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.recovered_get.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
2021-12-20 01:10:24 +00:00
resource "aws_apigatewayv2_integration" "recovered_stats" {
api_id = aws_apigatewayv2_api.main.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.recovered_stats.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
2021-06-06 01:19:58 +00:00
resource "aws_apigatewayv2_integration" "recovered_put" {
2021-10-08 11:17:11 +00:00
api_id = aws_apigatewayv2_api.main.id
2021-06-06 01:19:58 +00:00
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.recovered_put.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
resource "aws_apigatewayv2_route" "recovered_get" {
2021-10-08 11:17:11 +00:00
api_id = aws_apigatewayv2_api.main.id
2021-06-06 01:19:58 +00:00
api_key_required = false
authorization_type = "NONE"
route_key = "GET /recovered"
target = "integrations/${aws_apigatewayv2_integration.recovered_get.id}"
}
2021-12-20 01:10:24 +00:00
resource "aws_apigatewayv2_route" "recovered_stats" {
api_id = aws_apigatewayv2_api.main.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /recovered/stats"
target = "integrations/${aws_apigatewayv2_integration.recovered_stats.id}"
}
2021-06-06 01:19:58 +00:00
resource "aws_apigatewayv2_route" "recovered_put" {
2021-10-08 11:17:11 +00:00
api_id = aws_apigatewayv2_api.main.id
2021-06-06 01:19:58 +00:00
api_key_required = false
authorization_type = "NONE"
route_key = "PUT /recovered"
target = "integrations/${aws_apigatewayv2_integration.recovered_put.id}"
2022-01-26 06:44:10 +00:00
}
resource "aws_lambda_function" "recovery_ingest" {
2022-01-29 03:52:34 +00:00
function_name = "recovery_ingest"
handler = "recovery_ingest.handler"
s3_bucket = aws_s3_bucket_object.lambda.bucket
s3_key = aws_s3_bucket_object.lambda.key
source_code_hash = data.archive_file.lambda.output_base64sha256
publish = true
memory_size = 128
role = aws_iam_role.recovered.arn
runtime = "python3.9"
timeout = 300
2022-01-26 06:44:10 +00:00
tags = {
Name = "recovered_get"
}
environment {
variables = {}
2022-01-26 06:44:10 +00:00
}
}
resource "aws_cloudwatch_event_rule" "recovery_ingest" {
name = "recovery_ingest"
description = "recovery_ingest"
schedule_expression = "cron(*/5 * * * ? *)"
}
resource "aws_cloudwatch_event_target" "recovery_ingest" {
rule = aws_cloudwatch_event_rule.recovery_ingest.name
target_id = "recovery_ingest"
arn = aws_lambda_function.recovery_ingest.arn
}
resource "aws_lambda_permission" "recovery_ingest" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.recovery_ingest.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.recovery_ingest.arn
2021-06-06 01:19:58 +00:00
}