sondehub-infra/predictor.tf

700 lines
18 KiB
Terraform
Raw Normal View History

2021-09-13 04:42:34 +00:00
resource "aws_iam_role" "predict_updater" {
path = "/service-role/"
name = "predict-updater"
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" "predict_updater" {
name = "predict_updater"
role = aws_iam_role.predict_updater.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": "*"
},
{
"Effect": "Allow",
"Action": "sqs:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}
EOF
}
resource "aws_lambda_function" "predict_updater" {
function_name = "predict_updater"
2021-12-20 06:02:02 +00:00
handler = "predict_updater.predict"
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-09-13 04:42:34 +00:00
publish = true
memory_size = 1024
2021-09-13 04:42:34 +00:00
role = aws_iam_role.predict_updater.arn
2021-10-04 08:08:54 +00:00
runtime = "python3.9"
architectures = ["arm64"]
timeout = 300
reserved_concurrent_executions = 1
2021-09-13 04:42:34 +00:00
environment {
variables = {
2021-10-08 11:17:11 +00:00
"ES" = aws_route53_record.es.fqdn
2021-09-13 04:42:34 +00:00
}
}
tags = {
Name = "predict_updater"
}
2021-09-13 04:42:34 +00:00
}
resource "aws_cloudwatch_event_rule" "predict_updater" {
name = "predict_updater"
description = "predict_updater"
schedule_expression = "rate(1 minute)"
}
resource "aws_cloudwatch_event_target" "predict_updater" {
rule = aws_cloudwatch_event_rule.predict_updater.name
target_id = "SendToLambda"
arn = aws_lambda_function.predict_updater.arn
}
resource "aws_lambda_permission" "predict_updater" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.predict_updater.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.predict_updater.arn
2021-10-08 23:32:22 +00:00
}
resource "aws_apigatewayv2_route" "predictions" {
api_id = aws_apigatewayv2_api.main.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /predictions"
target = "integrations/${aws_apigatewayv2_integration.predictions.id}"
}
2021-10-09 04:56:26 +00:00
resource "aws_apigatewayv2_route" "reverse_predictions" {
api_id = aws_apigatewayv2_api.main.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /predictions/reverse"
target = "integrations/${aws_apigatewayv2_integration.reverse_predictions.id}"
}
2021-10-08 23:32:22 +00:00
resource "aws_apigatewayv2_integration" "predictions" {
api_id = aws_apigatewayv2_api.main.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.predictions.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
2021-10-09 04:56:26 +00:00
resource "aws_apigatewayv2_integration" "reverse_predictions" {
api_id = aws_apigatewayv2_api.main.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.reverse_predictions.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
2021-10-08 23:32:22 +00:00
resource "aws_lambda_function" "predictions" {
function_name = "predictions"
2021-12-20 06:02:02 +00:00
handler = "predict.predict"
2021-12-20 08:30:41 +00:00
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
2021-10-08 23:32:22 +00:00
publish = true
memory_size = 128
role = aws_iam_role.basic_lambda_role.arn
runtime = "python3.9"
timeout = 30
architectures = ["arm64"]
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
tags = {
Name = "predictions"
}
2021-10-08 23:32:22 +00:00
}
resource "aws_lambda_permission" "predictions" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.predictions.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}/*/*/predictions"
}
2021-10-09 04:56:26 +00:00
resource "aws_lambda_function" "reverse_predictions" {
function_name = "reverse-predictions"
2021-12-20 07:13:24 +00:00
handler = "reverse_predict.predict"
2021-12-20 08:30:41 +00:00
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
2021-10-09 04:56:26 +00:00
publish = true
memory_size = 128
role = aws_iam_role.basic_lambda_role.arn
runtime = "python3.9"
timeout = 30
architectures = ["arm64"]
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
tags = {
Name = "reverse_predictions"
}
2021-10-09 04:56:26 +00:00
}
resource "aws_lambda_permission" "reverse_predictions" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.reverse_predictions.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}/*/*/predictions/reverse"
}
2021-10-11 07:52:37 +00:00
resource "aws_ecs_task_definition" "tawhiri" {
family = "tawhiri"
container_definitions = jsonencode(
[
{
2021-11-12 11:47:43 +00:00
command = [
2021-11-12 10:08:18 +00:00
"/root/.local/bin/gunicorn",
"-b",
"0.0.0.0:8000",
"--workers=20",
"--timeout=30",
"--keep-alive=65",
"--threads=1",
"tawhiri.api:app"
]
dependsOn = [
{
containerName = "downloader"
2021-12-12 11:22:00 +00:00
condition = "SUCCESS"
}
]
2021-10-11 07:52:37 +00:00
cpu = 0
environment = []
essential = true
image = "${data.aws_caller_identity.current.account_id}.dkr.ecr.us-east-1.amazonaws.com/tawhiri:latest"
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = "/ecs/tawhiri"
awslogs-region = "us-east-1"
awslogs-stream-prefix = "ecs"
}
}
mountPoints = [
{
containerPath = "/srv"
sourceVolume = "srv"
},
2021-11-12 10:08:18 +00:00
{
containerPath = "/srv/tawhiri-datasets"
sourceVolume = "downloader"
}
2021-10-11 07:52:37 +00:00
]
name = "tawhiri"
portMappings = [
{
containerPort = 8000
hostPort = 8000
protocol = "tcp"
},
]
volumesFrom = []
},
{
2021-12-12 11:22:00 +00:00
cpu = 0
2021-10-11 07:52:37 +00:00
environment = [
{
name = "TZ"
value = "UTC"
2021-11-12 10:08:18 +00:00
}
2021-10-11 07:52:37 +00:00
]
essential = false
2021-11-12 11:47:43 +00:00
image = "${data.aws_caller_identity.current.account_id}.dkr.ecr.us-east-1.amazonaws.com/tawhiri-downloader:latest"
2021-10-11 07:52:37 +00:00
logConfiguration = {
logDriver = "awslogs"
options = {
2021-11-12 10:08:18 +00:00
awslogs-group = "/ecs/tawhiri"
2021-10-11 07:52:37 +00:00
awslogs-region = "us-east-1"
awslogs-stream-prefix = "ecs"
}
}
mountPoints = [
{
2021-11-12 10:08:18 +00:00
containerPath = "/srv/tawhiri-datasets"
sourceVolume = "downloader"
2021-10-11 07:52:37 +00:00
},
]
2021-11-12 11:47:43 +00:00
name = "downloader"
2021-11-12 10:08:18 +00:00
volumesFrom = []
2021-10-11 07:52:37 +00:00
},
]
)
2021-11-12 10:08:18 +00:00
cpu = "512"
2021-10-27 23:05:13 +00:00
execution_role_arn = aws_iam_role.ecs_execution.arn
2021-11-12 10:08:18 +00:00
memory = "1024"
2021-10-11 07:52:37 +00:00
network_mode = "awsvpc"
requires_compatibilities = [
"FARGATE",
]
tags = {}
2021-10-27 23:05:13 +00:00
task_role_arn = aws_iam_role.ecs_execution.arn
2021-11-12 11:47:43 +00:00
2021-10-11 07:52:37 +00:00
volume {
name = "downloader"
}
2021-10-11 07:52:37 +00:00
volume {
name = "srv"
efs_volume_configuration {
file_system_id = aws_efs_file_system.tawhiri.id
2021-11-29 11:27:01 +00:00
root_directory = "/srv"
transit_encryption = "DISABLED"
2021-12-12 11:22:00 +00:00
2021-10-11 07:52:37 +00:00
authorization_config {
iam = "DISABLED"
}
}
}
2021-11-29 11:27:01 +00:00
lifecycle {
ignore_changes = [volume] # terraform has a bug that doesn't correctly deal with root_directory because I don't know why - so we ignore it
}
2021-10-11 07:52:37 +00:00
}
resource "aws_ecs_task_definition" "tawhiri_ruaumoko" {
family = "tawhiri-ruaumoko"
container_definitions = jsonencode(
[
{
cpu = 0
entryPoint = [
"/root/.local/bin/ruaumoko-download",
]
environment = []
essential = true
image = "${data.aws_caller_identity.current.account_id}.dkr.ecr.us-east-1.amazonaws.com/tawhiri:latest"
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = "/ecs/tawhiri-ruaumoko"
awslogs-region = "us-east-1"
awslogs-stream-prefix = "ecs"
}
}
mountPoints = [
{
containerPath = "/srv"
sourceVolume = "srv"
},
]
name = "ruaumoko"
portMappings = []
volumesFrom = []
},
]
)
cpu = "1024"
execution_role_arn = "arn:aws:iam::143841941773:role/ecsTaskExecutionRole"
memory = "2048"
network_mode = "awsvpc"
requires_compatibilities = [
"FARGATE",
]
tags = {}
task_role_arn = "arn:aws:iam::143841941773:role/ecsTaskExecutionRole"
volume {
name = "srv"
efs_volume_configuration {
file_system_id = aws_efs_file_system.tawhiri.id
root_directory = "srv"
transit_encryption = "DISABLED"
2021-10-11 07:52:37 +00:00
authorization_config {
iam = "DISABLED"
}
}
}
}
resource "aws_efs_file_system" "tawhiri" {
tags = {
Name = "Tawhiri"
}
lifecycle_policy {
transition_to_ia = "AFTER_7_DAYS"
}
}
resource "aws_ecr_repository" "tawhiri" {
name = "tawhiri"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
}
resource "aws_ecr_repository" "tawhiri_downloader" {
name = "tawhiri-downloader"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
}
2021-10-27 23:05:13 +00:00
resource "aws_ecs_cluster" "tawhiri" {
name = "Tawhiri"
capacity_providers = ["FARGATE", "FARGATE_SPOT"]
}
resource "aws_lb_target_group" "tawhiri" {
name = "tawhiri"
port = 8000
protocol = "HTTP"
vpc_id = aws_vpc.main.id
target_type = "ip"
health_check {
enabled = true
2021-11-12 10:08:18 +00:00
healthy_threshold = 2
interval = 10
matcher = "200"
path = "/api/datasetcheck"
2021-10-27 23:05:13 +00:00
port = "traffic-port"
protocol = "HTTP"
timeout = 5
unhealthy_threshold = 2
}
}
resource "aws_ecs_service" "tawhiri" {
name = "tawhiri"
cluster = aws_ecs_cluster.tawhiri.id
task_definition = aws_ecs_task_definition.tawhiri.arn
enable_ecs_managed_tags = true
2021-11-12 10:08:18 +00:00
health_check_grace_period_seconds = 600
2021-10-27 23:05:13 +00:00
iam_role = "aws-service-role"
launch_type = "FARGATE"
platform_version = "LATEST"
desired_count = 1
load_balancer {
container_name = "tawhiri"
container_port = 8000
target_group_arn = aws_lb_target_group.tawhiri.arn
}
lifecycle {
2021-12-11 05:17:41 +00:00
ignore_changes = [desired_count, task_definition]
2021-10-27 23:05:13 +00:00
}
network_configuration {
assign_public_ip = true
security_groups = [
aws_security_group.tawhiri_efs.id,
aws_security_group.tawhiri.id
]
subnets = [aws_subnet.public["us-east-1b"].id]
}
}
resource "aws_appautoscaling_target" "tawhiri" {
service_namespace = "ecs"
scalable_dimension = "ecs:service:DesiredCount"
resource_id = "service/Tawhiri/tawhiri"
min_capacity = 1
max_capacity = 5
}
resource "aws_appautoscaling_policy" "tawhiri" {
name = "cpu"
service_namespace = aws_appautoscaling_target.tawhiri.service_namespace
scalable_dimension = aws_appautoscaling_target.tawhiri.scalable_dimension
resource_id = aws_appautoscaling_target.tawhiri.resource_id
policy_type = "TargetTrackingScaling"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 80
scale_in_cooldown = 120
scale_out_cooldown = 120
}
}
resource "aws_security_group" "tawhiri_efs" {
name = "tawhiri-efs"
ingress = [
{
from_port = 2049
to_port = 2049
protocol = "tcp"
cidr_blocks = []
ipv6_cidr_blocks = []
description = ""
prefix_list_ids = []
self = true
security_groups = [aws_vpc.main.default_security_group_id]
}
]
2021-11-12 11:47:43 +00:00
egress = [
2021-10-27 23:05:13 +00:00
{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
description = ""
prefix_list_ids = []
self = false
security_groups = []
}
]
vpc_id = aws_vpc.main.id
lifecycle {
ignore_changes = [description, name]
}
}
resource "aws_security_group" "tawhiri" {
name = "tawhiri"
ingress = [
{
from_port = 8000
to_port = 8000
protocol = "tcp"
cidr_blocks = []
ipv6_cidr_blocks = []
description = ""
prefix_list_ids = []
self = true
security_groups = [aws_security_group.tawhiri_alb.id, aws_security_group.lb.id]
}
]
2021-11-12 11:47:43 +00:00
egress = [
2021-10-27 23:05:13 +00:00
{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
description = ""
prefix_list_ids = []
self = false
security_groups = []
}
]
vpc_id = aws_vpc.main.id
lifecycle {
ignore_changes = [description, name]
}
}
resource "aws_security_group" "tawhiri_alb" {
name = "tawhiri-alb"
egress = [
{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
description = ""
prefix_list_ids = []
self = false
security_groups = []
}
]
ingress = [
{
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
description = ""
prefix_list_ids = []
self = false
security_groups = []
}
]
vpc_id = aws_vpc.main.id
lifecycle {
ignore_changes = [description, name]
}
2021-11-12 10:08:18 +00:00
}
resource "aws_route53_record" "tawhiri_A" {
2021-11-12 11:47:43 +00:00
name = "tawhiri"
type = "A"
alias {
name = "dualstack.${aws_lb.ws.dns_name}."
zone_id = aws_lb.ws.zone_id
evaluate_target_health = true
}
zone_id = aws_route53_zone.Route53HostedZone.zone_id
2021-11-12 10:08:18 +00:00
}
resource "aws_route53_record" "tawhiri_AAAA" {
2021-11-12 11:47:43 +00:00
name = "tawhiri"
type = "AAAA"
alias {
name = "dualstack.${aws_lb.ws.dns_name}."
zone_id = aws_lb.ws.zone_id
evaluate_target_health = true
}
zone_id = aws_route53_zone.Route53HostedZone.zone_id
}
resource "aws_iam_role" "predictor_update_trigger_lambda" {
path = "/service-role/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
2021-11-29 10:39:59 +00:00
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
max_session_duration = 3600
}
resource "aws_iam_role_policy" "predictor_update_trigger_lambda" {
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ecs:UpdateService",
"Resource": "*"
2021-11-29 10:39:59 +00:00
},
{
"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/*"
]
}
]
}
EOF
role = aws_iam_role.predictor_update_trigger_lambda.name
}
resource "aws_lambda_function" "predictor_update_trigger_lambda" {
function_name = "tawhiri-updater"
2021-12-20 07:13:24 +00:00
handler = "tawhiri_updater.handler"
2021-12-20 08:30:41 +00:00
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.predictor_update_trigger_lambda.arn
runtime = "python3.9"
timeout = 3
tags = {
Name = "tawhiri-updater"
}
}
resource "aws_lambda_permission" "predictor_update_trigger_lambda" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.predictor_update_trigger_lambda.function_name
principal = "sns.amazonaws.com"
source_arn = "arn:aws:sns:us-east-1:123901341784:NewGFSObject"
}
resource "aws_sns_topic_subscription" "predictor_update_trigger_lambda" {
topic_arn = "arn:aws:sns:us-east-1:123901341784:NewGFSObject"
2021-12-12 11:22:00 +00:00
protocol = "lambda"
endpoint = aws_lambda_function.predictor_update_trigger_lambda.arn
}
# sns subscription