sondehub-infra/main.tf

1236 lines
37 KiB
Terraform
Raw Normal View History

2021-02-02 07:14:38 +00:00
# TODO
# add sns / sqs, AWS IoT actions
2021-01-30 11:07:34 +00:00
terraform {
backend "s3" {
2021-02-01 06:08:59 +00:00
bucket = "sondehub-terraform"
key = "sondehub-main"
region = "us-east-1"
2021-01-30 11:07:34 +00:00
profile = "sondes"
}
}
provider "aws" {
2021-02-01 06:08:59 +00:00
region = "us-east-1"
profile = "sondes"
2021-01-30 11:07:34 +00:00
}
locals {
2021-02-01 06:08:59 +00:00
domain_name = "v2.sondehub.org"
2021-01-30 11:07:34 +00:00
}
data "aws_caller_identity" "current" {}
2021-02-15 05:23:51 +00:00
data "aws_iot_endpoint" "endpoint" {
endpoint_type = "iot:Data-ATS"
}
2021-01-30 11:07:34 +00:00
resource "aws_iam_role" "IAMRole" {
2021-02-01 06:08:59 +00:00
path = "/"
name = "Cognito_sondesAuth_Role"
assume_role_policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "${aws_cognito_identity_pool.CognitoIdentityPool.id}"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}
}]
}
EOF
2021-02-01 06:08:59 +00:00
max_session_duration = 3600
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_role" "IAMRole2" {
2021-02-01 06:08:59 +00:00
path = "/"
name = "Cognito_sondesUnauth_Role"
assume_role_policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "${aws_cognito_identity_pool.CognitoIdentityPool.id}"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "unauthenticated"
}
}
}]
}
EOF
2021-02-01 06:08:59 +00:00
max_session_duration = 3600
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_role" "IAMRole3" {
2021-02-01 06:08:59 +00:00
path = "/service-role/"
name = "CognitoAccessForAmazonES"
assume_role_policy = <<EOF
{
2021-01-30 11:07:34 +00:00
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "es.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
EOF
2021-02-01 06:08:59 +00:00
max_session_duration = 3600
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_role" "IAMRole4" {
2021-02-01 06:08:59 +00:00
path = "/service-role/"
name = "iot-es"
assume_role_policy = <<EOF
{
2021-01-30 11:07:34 +00:00
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "iot.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
EOF
2021-02-01 06:08:59 +00:00
max_session_duration = 3600
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_role" "IAMRole5" {
2021-02-01 06:08:59 +00:00
path = "/service-role/"
name = "sonde-api-to-iot-core-role-z9zes3f5"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
EOF
max_session_duration = 3600
}
2021-05-20 08:43:50 +00:00
2021-02-01 06:08:59 +00:00
resource "aws_iam_role" "sign_socket" {
name = "sign_socket"
assume_role_policy = <<EOF
{
2021-01-30 11:07:34 +00:00
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
EOF
2021-02-01 06:08:59 +00:00
max_session_duration = 3600
2021-01-30 11:07:34 +00:00
}
2021-02-15 05:23:51 +00:00
resource "aws_iam_role" "history" {
name = "history"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
EOF
max_session_duration = 3600
}
2021-01-30 11:07:34 +00:00
resource "aws_iam_service_linked_role" "IAMServiceLinkedRole" {
2021-02-01 06:08:59 +00:00
aws_service_name = "es.amazonaws.com"
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_service_linked_role" "IAMServiceLinkedRole3" {
2021-02-01 06:08:59 +00:00
aws_service_name = "ops.apigateway.amazonaws.com"
description = "The Service Linked Role is used by Amazon API Gateway."
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_policy" "IAMManagedPolicy" {
2021-02-01 06:08:59 +00:00
name = "AWSLambdaBasicExecutionRole-01b38736-6769-4407-9515-93d653f4db5f"
path = "/service-role/"
policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"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": [
2021-02-15 05:23:51 +00:00
"arn:aws:logs:us-east-1:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/*"
2021-01-30 11:07:34 +00:00
]
}
]
}
EOF
}
2021-05-20 08:43:50 +00:00
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
}
2021-01-30 11:07:34 +00:00
resource "aws_iam_policy" "IAMManagedPolicy2" {
2021-02-01 06:08:59 +00:00
name = "aws-iot-role-es_795847808"
path = "/service-role/"
policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "es:ESHttpPut",
"Resource": [
"arn:aws:es:us-east-1:${data.aws_caller_identity.current.account_id}:domain/sondes-v2/*"
]
}
}
EOF
}
resource "aws_iam_policy" "IAMManagedPolicy4" {
2021-02-01 06:08:59 +00:00
name = "AWSLambdaTracerAccessExecutionRole-56cd4e03-902a-4a40-9cd9-c9449709d80d"
path = "/service-role/"
policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": [
"xray:PutTraceSegments",
"xray:PutTelemetryRecords"
],
"Resource": [
"*"
]
}
}
EOF
}
resource "aws_iam_role_policy" "IAMPolicy" {
2021-02-01 06:08:59 +00:00
policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": [
2021-01-31 08:50:17 +00:00
{
"Effect": "Allow",
"Action": "es:*",
2021-02-01 06:08:59 +00:00
"Resource": "arn:aws:es:us-east-1:${data.aws_caller_identity.current.account_id}:domain/sondes-v2"
2021-01-31 08:50:17 +00:00
},
2021-01-30 11:07:34 +00:00
{
"Effect": "Allow",
2021-01-31 08:50:17 +00:00
"Action": "es:*",
2021-02-01 06:08:59 +00:00
"Resource": "arn:aws:es:us-east-1:${data.aws_caller_identity.current.account_id}:domain/sondes-v2/*"
2021-01-30 11:07:34 +00:00
}
]
}
EOF
2021-02-01 06:08:59 +00:00
role = aws_iam_role.IAMRole.name
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_role_policy" "IAMPolicy2" {
2021-02-01 06:08:59 +00:00
policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*"
],
"Resource": [
"*"
]
}
]
}
EOF
2021-02-01 06:08:59 +00:00
role = aws_iam_role.IAMRole2.name
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_role_policy" "IAMPolicy3" {
2021-02-01 06:08:59 +00:00
policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*",
"cognito-identity:*"
],
"Resource": [
"*"
]
}
]
}
EOF
2021-02-01 06:08:59 +00:00
role = aws_iam_role.IAMRole.name
2021-01-30 11:07:34 +00:00
}
resource "aws_iam_role_policy" "IAMPolicy4" {
2021-02-01 06:08:59 +00:00
policy = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "iot:*",
"Resource": "*"
2021-04-28 11:32:22 +00:00
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
2021-05-23 13:14:15 +00:00
},
{
"Effect": "Allow",
"Action": "sns:*",
"Resource": "*"
2021-01-30 11:07:34 +00:00
}
]
}
EOF
2021-02-01 06:08:59 +00:00
role = aws_iam_role.IAMRole5.name
}
resource "aws_iam_role_policy" "sign_socket" {
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Connect",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "iot:Subscribe",
"Resource": "arn:aws:iot:us-east-1:${data.aws_caller_identity.current.account_id}:topicfilter/sondes/*"
},
{
"Effect": "Allow",
"Action": "iot:Receive",
"Resource": "arn:aws:iot:us-east-1:${data.aws_caller_identity.current.account_id}:topic/sondes/*"
}
]
}
EOF
role = aws_iam_role.sign_socket.name
2021-01-30 11:07:34 +00:00
}
2021-02-15 05:23:51 +00:00
resource "aws_iam_role_policy" "history" {
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::sondehub-open-data/*"
},
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::sondehub-open-data"
},
{
"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": "arn:aws:es:us-east-1:${data.aws_caller_identity.current.account_id}:domain/sondes-v2"
},
{
"Effect": "Allow",
"Action": "es:*",
"Resource": "arn:aws:es:us-east-1:${data.aws_caller_identity.current.account_id}:domain/sondes-v2/*"
2021-02-15 05:23:51 +00:00
}
]
}
EOF
role = aws_iam_role.history.name
}
2021-01-30 11:07:34 +00:00
resource "aws_route53_zone" "Route53HostedZone" {
2021-02-01 06:08:59 +00:00
name = "${local.domain_name}."
2021-01-30 11:07:34 +00:00
}
2021-03-13 11:19:52 +00:00
# resource "aws_route53_record" "Route53RecordSet" {
# name = ""
# type = "A"
# ttl = 300
# records = [
# "127.0.0.1"
# ]
# zone_id = aws_route53_zone.Route53HostedZone.zone_id
# }
2021-01-30 11:07:34 +00:00
resource "aws_route53_record" "cert_validation" {
for_each = {
for dvo in aws_acm_certificate.CertificateManagerCertificate.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = aws_route53_zone.Route53HostedZone.zone_id
}
resource "aws_acm_certificate_validation" "CertificateManagerCertificate" {
certificate_arn = aws_acm_certificate.CertificateManagerCertificate.arn
validation_record_fqdns = [for record in aws_route53_record.cert_validation : record.fqdn]
}
2021-03-13 23:43:05 +00:00
# resource "aws_route53_record" "Route53RecordSet5" {
# name = "api"
# type = "CNAME"
# ttl = 60
# records = [
# "${aws_apigatewayv2_domain_name.ApiGatewayV2DomainName.domain_name_configuration.0.target_domain_name}."
# ]
# zone_id = aws_route53_zone.Route53HostedZone.zone_id
# }
2021-01-30 11:07:34 +00:00
resource "aws_cognito_user_pool_domain" "main" {
2021-02-01 06:08:59 +00:00
domain = "auth.${local.domain_name}"
user_pool_id = aws_cognito_user_pool.CognitoUserPool.id
2021-01-30 11:07:34 +00:00
certificate_arn = aws_acm_certificate_validation.CertificateManagerCertificate.certificate_arn
}
resource "aws_route53_record" "Route53RecordSet6" {
2021-02-01 06:08:59 +00:00
name = "auth"
type = "A"
alias {
name = "${aws_cognito_user_pool_domain.main.cloudfront_distribution_arn}."
zone_id = "Z2FDTNDATAQYW2"
evaluate_target_health = false
}
zone_id = aws_route53_zone.Route53HostedZone.zone_id
2021-01-30 11:07:34 +00:00
}
resource "aws_route53_record" "Route53RecordSet7" {
2021-02-01 06:08:59 +00:00
name = "es"
type = "CNAME"
ttl = 300
records = [
aws_elasticsearch_domain.ElasticsearchDomain.endpoint
]
zone_id = aws_route53_zone.Route53HostedZone.zone_id
2021-01-30 11:07:34 +00:00
}
data "archive_file" "api_to_iot" {
type = "zip"
2021-05-23 13:14:15 +00:00
source_dir = "sonde-api-to-iot-core/"
2021-01-30 11:07:34 +00:00
output_path = "${path.module}/build/sonde-api-to-iot-core.zip"
}
2021-04-01 09:19:02 +00:00
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"
}
2021-02-02 07:14:38 +00:00
data "archive_file" "query" {
type = "zip"
source_file = "query/lambda_function.py"
output_path = "${path.module}/build/query.zip"
}
2021-02-15 05:23:51 +00:00
data "archive_file" "history" {
type = "zip"
source_file = "history/lambda_function.py"
output_path = "${path.module}/build/history.zip"
}
2021-02-02 07:14:38 +00:00
2021-02-01 06:08:59 +00:00
data "archive_file" "sign_socket" {
type = "zip"
source_file = "sign-websocket/lambda_function.py"
output_path = "${path.module}/build/sign_socket.zip"
}
2021-02-22 06:13:30 +00:00
data "archive_file" "predictions" {
type = "zip"
source_file = "predict/lambda_function.py"
output_path = "${path.module}/build/predictions.zip"
}
2021-01-30 11:07:34 +00:00
resource "aws_lambda_function" "LambdaFunction" {
2021-02-01 06:08:59 +00:00
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 = 256
role = aws_iam_role.IAMRole5.arn
runtime = "python3.7"
2021-04-04 00:47:31 +00:00
timeout = 30
2021-02-01 06:08:59 +00:00
environment {
variables = {
"IOT_ENDPOINT" = data.aws_iot_endpoint.endpoint.endpoint_address
2021-05-23 13:14:15 +00:00
"SNS_TOPIC" = aws_sns_topic.sonde_telem.arn
2021-01-30 11:07:34 +00:00
}
2021-02-01 06:08:59 +00:00
}
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
}
2021-04-01 09:19:02 +00:00
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 = 256
role = aws_iam_role.IAMRole5.arn
runtime = "python3.7"
timeout = 10
environment {
variables = {
"IOT_ENDPOINT" = data.aws_iot_endpoint.endpoint.endpoint_address
}
}
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
}
2021-02-02 07:14:38 +00:00
resource "aws_lambda_function" "get_sondes" {
function_name = "query"
handler = "lambda_function.get_sondes"
filename = "${path.module}/build/query.zip"
source_code_hash = data.archive_file.query.output_base64sha256
publish = true
memory_size = 256
role = aws_iam_role.IAMRole5.arn
runtime = "python3.7"
2021-04-10 12:04:42 +00:00
timeout = 30
2021-05-20 08:43:50 +00:00
2021-02-02 07:14:38 +00:00
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
}
2021-02-22 06:13:30 +00:00
resource "aws_lambda_function" "listeners" {
function_name = "listeners"
handler = "lambda_function.get_listeners"
filename = "${path.module}/build/query.zip"
source_code_hash = data.archive_file.query.output_base64sha256
publish = true
memory_size = 256
role = aws_iam_role.IAMRole5.arn
runtime = "python3.7"
2021-04-10 12:04:42 +00:00
timeout = 30
2021-05-20 08:43:50 +00:00
2021-02-22 06:13:30 +00:00
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
}
2021-05-20 08:43:50 +00:00
2021-02-22 06:13:30 +00:00
resource "aws_lambda_function" "predictions" {
function_name = "predictions"
handler = "lambda_function.predict"
filename = "${path.module}/build/predictions.zip"
source_code_hash = data.archive_file.predictions.output_base64sha256
publish = true
memory_size = 256
role = aws_iam_role.IAMRole5.arn
runtime = "python3.7"
2021-04-10 12:04:42 +00:00
timeout = 30
2021-05-20 08:43:50 +00:00
2021-02-22 06:13:30 +00:00
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
}
2021-02-02 10:44:39 +00:00
resource "aws_lambda_function" "get_telem" {
function_name = "get_telem"
handler = "lambda_function.get_telem"
filename = "${path.module}/build/query.zip"
source_code_hash = data.archive_file.query.output_base64sha256
publish = true
memory_size = 256
role = aws_iam_role.IAMRole5.arn
runtime = "python3.7"
timeout = 30
2021-05-20 08:43:50 +00:00
2021-02-02 10:44:39 +00:00
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
}
2021-07-21 09:54:09 +00:00
resource "aws_lambda_function" "get_listener_telemetry" {
function_name = "get_listener_telemetry"
handler = "lambda_function.get_listener_telemetry"
filename = "${path.module}/build/query.zip"
source_code_hash = data.archive_file.query.output_base64sha256
publish = true
memory_size = 256
role = aws_iam_role.IAMRole5.arn
runtime = "python3.7"
timeout = 30
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
}
2021-02-01 06:08:59 +00:00
resource "aws_lambda_function" "sign_socket" {
function_name = "sign-websocket"
handler = "lambda_function.lambda_handler"
filename = "${path.module}/build/sign_socket.zip"
source_code_hash = data.archive_file.sign_socket.output_base64sha256
publish = true
memory_size = 128
role = aws_iam_role.sign_socket.arn
runtime = "python3.7"
timeout = 10
2021-05-20 08:43:50 +00:00
2021-02-01 06:08:59 +00:00
environment {
variables = {
"IOT_ENDPOINT" = data.aws_iot_endpoint.endpoint.endpoint_address
}
}
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
}
2021-02-15 05:23:51 +00:00
resource "aws_lambda_function" "history" {
function_name = "history"
handler = "lambda_function.history"
filename = "${path.module}/build/history.zip"
source_code_hash = data.archive_file.history.output_base64sha256
publish = true
memory_size = 512
role = aws_iam_role.IAMRole5.arn
2021-02-15 05:23:51 +00:00
runtime = "python3.7"
timeout = 30
reserved_concurrent_executions = 4
environment {
variables = {
"ES" = "es.${local.domain_name}"
}
}
2021-05-20 08:43:50 +00:00
2021-02-15 05:23:51 +00:00
layers = [
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:xray-python:1",
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:layer:iot:3"
]
}
2021-02-01 06:08:59 +00:00
resource "aws_lambda_permission" "sign_socket" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.sign_socket.arn
principal = "apigateway.amazonaws.com"
2021-02-22 06:13:30 +00:00
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/sondes/websocket"
2021-01-30 11:07:34 +00:00
}
2021-02-15 05:23:51 +00:00
resource "aws_lambda_permission" "history" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.history.arn
principal = "apigateway.amazonaws.com"
2021-02-22 06:13:30 +00:00
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/sonde/{serial}"
2021-02-15 05:23:51 +00:00
}
2021-02-02 07:14:38 +00:00
resource "aws_lambda_permission" "get_sondes" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.get_sondes.arn
principal = "apigateway.amazonaws.com"
2021-02-22 06:13:30 +00:00
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/sondes"
}
resource "aws_lambda_permission" "listeners" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.listeners.arn
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/listeners"
}
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.ApiGatewayV2Api.id}/*/*/predictions"
2021-02-02 07:14:38 +00:00
}
2021-02-22 06:13:30 +00:00
2021-02-02 10:44:39 +00:00
resource "aws_lambda_permission" "get_telem" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.get_telem.arn
principal = "apigateway.amazonaws.com"
2021-02-22 06:13:30 +00:00
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/sondes/telemetry"
2021-02-02 10:44:39 +00:00
}
2021-07-21 09:54:09 +00:00
resource "aws_lambda_permission" "get_listener_telemetry" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.get_listener_telemetry.arn
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/listeners/telemetry"
}
2021-02-02 10:44:39 +00:00
2021-01-30 11:07:34 +00:00
resource "aws_lambda_permission" "LambdaPermission2" {
2021-02-01 06:08:59 +00:00
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.LambdaFunction.arn
principal = "apigateway.amazonaws.com"
2021-02-22 06:13:30 +00:00
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.ApiGatewayV2Api.id}/*/*/sondes/telemetry"
2021-01-30 11:07:34 +00:00
}
2021-04-01 09:19:02 +00:00
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.ApiGatewayV2Api.id}/*/*/listeners"
}
2021-01-30 11:07:34 +00:00
resource "aws_lambda_layer_version" "LambdaLayerVersion2" {
2021-02-01 06:08:59 +00:00
compatible_runtimes = [
"python3.8"
]
layer_name = "iot"
s3_bucket = "sondehub-lambda-layers"
s3_key = "iot.zip"
source_code_hash = "sHyE9vXk+BzFphPe8evfiL79fcxsSEYVfpbTVi2IwH0="
2021-01-30 11:07:34 +00:00
}
resource "aws_lambda_layer_version" "LambdaLayerVersion4" {
2021-02-01 06:08:59 +00:00
compatible_runtimes = [
"python3.8"
]
layer_name = "xray-python"
s3_bucket = "sondehub-lambda-layers"
s3_key = "xray-python.zip"
source_code_hash = "ta4o2brS2ZRAeWhZjqrm6MhOc3RlYNgkOuD4dxSonEc="
2021-01-30 11:07:34 +00:00
}
resource "aws_s3_bucket" "S3Bucket" {
2021-02-01 06:08:59 +00:00
bucket = "sondehub-lambda-layers"
2021-01-30 11:07:34 +00:00
}
resource "aws_cloudwatch_log_group" "LogsLogGroup" {
2021-02-01 06:08:59 +00:00
name = "/aws/lambda/sonde-api-to-iot-core"
retention_in_days = 30
2021-01-30 11:07:34 +00:00
}
resource "aws_apigatewayv2_api" "ApiGatewayV2Api" {
2021-02-01 06:08:59 +00:00
name = "sondehub-v2"
disable_execute_api_endpoint = true
api_key_selection_expression = "$request.header.x-api-key"
protocol_type = "HTTP"
route_selection_expression = "$request.method $request.path"
2021-02-22 06:13:30 +00:00
cors_configuration {
allow_credentials = false
allow_headers = [
"*",
]
allow_methods = [
"*",
]
allow_origins = [
"*",
]
expose_headers = []
max_age = 0
}
2021-01-30 11:07:34 +00:00
}
resource "aws_apigatewayv2_stage" "ApiGatewayV2Stage" {
2021-02-22 06:13:30 +00:00
name = "$default"
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
2021-02-01 06:08:59 +00:00
default_route_settings {
detailed_metrics_enabled = false
}
auto_deploy = true
lifecycle {
2021-02-02 07:14:38 +00:00
ignore_changes = [deployment_id]
2021-02-01 06:08:59 +00:00
}
2021-01-30 11:07:34 +00:00
}
resource "aws_apigatewayv2_stage" "ApiGatewayV2Stage2" {
2021-02-01 06:08:59 +00:00
name = "prod"
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
deployment_id = aws_apigatewayv2_deployment.ApiGatewayV2Deployment4.id
default_route_settings {
detailed_metrics_enabled = false
}
2021-01-30 11:07:34 +00:00
}
resource "aws_apigatewayv2_deployment" "ApiGatewayV2Deployment3" {
2021-02-01 06:08:59 +00:00
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
2021-01-30 11:07:34 +00:00
}
resource "aws_apigatewayv2_deployment" "ApiGatewayV2Deployment4" {
2021-02-01 06:08:59 +00:00
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
2021-01-30 11:07:34 +00:00
}
resource "aws_apigatewayv2_route" "ApiGatewayV2Route" {
2021-02-01 06:08:59 +00:00
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "PUT /sondes/telemetry"
target = "integrations/${aws_apigatewayv2_integration.ApiGatewayV2Integration.id}"
}
2021-04-01 09:19:02 +00:00
resource "aws_apigatewayv2_route" "stations" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "PUT /listeners"
target = "integrations/${aws_apigatewayv2_integration.stations.id}"
}
2021-02-01 06:08:59 +00:00
resource "aws_apigatewayv2_route" "sign_socket" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /sondes/websocket"
target = "integrations/${aws_apigatewayv2_integration.sign_socket.id}"
}
2021-02-15 05:23:51 +00:00
resource "aws_apigatewayv2_route" "history" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /sonde/{serial}"
target = "integrations/${aws_apigatewayv2_integration.history.id}"
}
2021-02-01 06:08:59 +00:00
2021-02-02 07:14:38 +00:00
resource "aws_apigatewayv2_route" "get_sondes" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /sondes"
target = "integrations/${aws_apigatewayv2_integration.get_sondes.id}"
}
2021-02-22 06:13:30 +00:00
resource "aws_apigatewayv2_route" "listeners" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /listeners"
target = "integrations/${aws_apigatewayv2_integration.listeners.id}"
}
2021-02-22 06:13:30 +00:00
resource "aws_apigatewayv2_route" "predictions" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /predictions"
target = "integrations/${aws_apigatewayv2_integration.predictions.id}"
}
2021-02-02 10:44:39 +00:00
resource "aws_apigatewayv2_route" "get_telem" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /sondes/telemetry"
target = "integrations/${aws_apigatewayv2_integration.get_telem.id}"
}
2021-07-21 09:54:09 +00:00
resource "aws_apigatewayv2_route" "get_listener_telemetry" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
api_key_required = false
authorization_type = "NONE"
route_key = "GET /listeners/telemetry"
target = "integrations/${aws_apigatewayv2_integration.get_listener_telemetry.id}"
}
2021-02-01 06:08:59 +00:00
resource "aws_apigatewayv2_integration" "sign_socket" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.sign_socket.arn
timeout_milliseconds = 30000
2021-02-02 07:14:38 +00:00
payload_format_version = "2.0"
}
2021-02-15 05:23:51 +00:00
resource "aws_apigatewayv2_integration" "history" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.history.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
2021-02-02 07:14:38 +00:00
resource "aws_apigatewayv2_integration" "get_sondes" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.get_sondes.arn
timeout_milliseconds = 30000
2021-02-02 10:44:39 +00:00
payload_format_version = "2.0"
}
2021-02-22 06:13:30 +00:00
resource "aws_apigatewayv2_integration" "listeners" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.listeners.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
resource "aws_apigatewayv2_integration" "predictions" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.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-02-02 10:44:39 +00:00
resource "aws_apigatewayv2_integration" "get_telem" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.get_telem.arn
timeout_milliseconds = 30000
2021-02-01 06:08:59 +00:00
payload_format_version = "2.0"
2021-01-30 11:07:34 +00:00
}
2021-07-21 09:54:09 +00:00
resource "aws_apigatewayv2_integration" "get_listener_telemetry" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.get_listener_telemetry.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
}
2021-01-30 11:07:34 +00:00
resource "aws_apigatewayv2_integration" "ApiGatewayV2Integration" {
2021-02-01 06:08:59 +00:00
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
connection_type = "INTERNET"
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.LambdaFunction.arn
timeout_milliseconds = 30000
payload_format_version = "2.0"
2021-01-30 11:07:34 +00:00
}
2021-04-01 09:19:02 +00:00
resource "aws_apigatewayv2_integration" "stations" {
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.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"
}
2021-01-30 11:07:34 +00:00
resource "aws_apigatewayv2_api_mapping" "ApiGatewayV2ApiMapping" {
2021-02-01 06:08:59 +00:00
api_id = aws_apigatewayv2_api.ApiGatewayV2Api.id
domain_name = aws_apigatewayv2_domain_name.ApiGatewayV2DomainName.id
stage = "$default"
api_mapping_key = ""
2021-01-30 11:07:34 +00:00
}
resource "aws_apigatewayv2_domain_name" "ApiGatewayV2DomainName" {
2021-03-14 02:43:19 +00:00
domain_name = "api-raw.${local.domain_name}"
2021-02-01 06:08:59 +00:00
domain_name_configuration {
certificate_arn = aws_acm_certificate_validation.CertificateManagerCertificate.certificate_arn
endpoint_type = "REGIONAL"
security_policy = "TLS_1_2"
}
2021-01-30 11:07:34 +00:00
}
resource "aws_acm_certificate" "CertificateManagerCertificate" {
2021-02-01 06:08:59 +00:00
domain_name = local.domain_name
subject_alternative_names = [
"*.${local.domain_name}"
]
validation_method = "DNS"
2021-01-30 11:07:34 +00:00
}
resource "aws_elasticsearch_domain" "ElasticsearchDomain" {
2021-02-01 06:08:59 +00:00
domain_name = "sondes-v2"
elasticsearch_version = "7.9"
cluster_config {
dedicated_master_count = 3
dedicated_master_enabled = false
dedicated_master_type = "t3.small.elasticsearch"
2021-05-20 08:43:50 +00:00
instance_count = 1
instance_type = "r5.xlarge.elasticsearch"
zone_awareness_enabled = false
2021-02-01 06:08:59 +00:00
}
cognito_options {
enabled = true
identity_pool_id = aws_cognito_identity_pool.CognitoIdentityPool.id
role_arn = aws_iam_role.IAMRole3.arn
user_pool_id = aws_cognito_user_pool.CognitoUserPool.id
}
2021-01-30 11:07:34 +00:00
2021-02-01 06:08:59 +00:00
access_policies = <<EOF
2021-01-30 11:07:34 +00:00
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
2021-01-31 08:50:17 +00:00
"AWS": "*"
2021-01-30 11:07:34 +00:00
},
"Action": "es:*",
"Resource": "arn:aws:es:us-east-1:${data.aws_caller_identity.current.account_id}:domain/sondes-v2/*"
}
]
}
EOF
2021-02-01 06:08:59 +00:00
encrypt_at_rest {
enabled = true
kms_key_id = data.aws_kms_key.es.arn
}
node_to_node_encryption {
enabled = true
}
advanced_options = {
"rest.action.multi.allow_explicit_index" = "true"
}
ebs_options {
ebs_enabled = true
volume_type = "gp2"
volume_size = 400
2021-05-20 08:43:50 +00:00
}
log_publishing_options {
cloudwatch_log_group_arn = "arn:aws:logs:us-east-1:143841941773:log-group:/aws/aes/domains/sondes-v2/application-logs"
enabled = true
log_type = "ES_APPLICATION_LOGS"
2021-02-01 06:08:59 +00:00
}
log_publishing_options {
cloudwatch_log_group_arn = "arn:aws:logs:us-east-1:143841941773:log-group:/aws/aes/domains/sondes-v2/index-logs"
enabled = true
log_type = "INDEX_SLOW_LOGS"
}
log_publishing_options {
cloudwatch_log_group_arn = "arn:aws:logs:us-east-1:143841941773:log-group:/aws/aes/domains/sondes-v2/search-logs"
enabled = true
log_type = "SEARCH_SLOW_LOGS"
}
2021-01-30 11:07:34 +00:00
}
data "aws_kms_key" "es" {
key_id = "alias/aws/es"
}
resource "aws_cognito_identity_pool" "CognitoIdentityPool" {
2021-02-01 06:08:59 +00:00
identity_pool_name = "sondes"
allow_unauthenticated_identities = true
supported_login_providers = {
"accounts.google.com" = "575970424139-vkk7scicbdd1igj04riqjh2bbs0oa6vj.apps.googleusercontent.com"
}
cognito_identity_providers {
client_id = aws_cognito_user_pool_client.CognitoUserPoolClient.id
provider_name = aws_cognito_user_pool.CognitoUserPool.endpoint
server_side_token_check = false
}
2021-01-30 11:07:34 +00:00
}
resource "aws_cognito_identity_pool_roles_attachment" "CognitoIdentityPoolRoleAttachment" {
2021-02-01 06:08:59 +00:00
identity_pool_id = aws_cognito_identity_pool.CognitoIdentityPool.id
roles = {
authenticated = aws_iam_role.IAMRole.arn
unauthenticated = aws_iam_role.IAMRole2.arn
}
role_mapping {
ambiguous_role_resolution = "AuthenticatedRole"
identity_provider = "cognito-idp.us-east-1.amazonaws.com/us-east-1_G4H7NMniM:5sngha3l291nb4784iid5hli48"
type = "Token"
}
2021-01-30 11:07:34 +00:00
}
resource "aws_cognito_user_pool" "CognitoUserPool" {
2021-02-01 06:08:59 +00:00
name = "sondes"
password_policy {
temporary_password_validity_days = 7
minimum_length = 8
require_lowercase = true
require_numbers = true
require_symbols = true
require_uppercase = true
}
schema {
attribute_data_type = "String"
developer_only_attribute = false
mutable = true
name = "email"
string_attribute_constraints {
max_length = "2048"
min_length = "0"
2021-01-30 11:07:34 +00:00
}
2021-02-01 06:08:59 +00:00
required = true
}
username_configuration {
case_sensitive = false
}
auto_verified_attributes = [
"email"
]
alias_attributes = [
"email",
"preferred_username"
]
sms_verification_message = "Your verification code is {####}. "
email_verification_message = "Your verification code is {####}. "
email_verification_subject = "Your verification code"
sms_authentication_message = "Your authentication code is {####}. "
mfa_configuration = "OFF"
device_configuration {
challenge_required_on_new_device = false
device_only_remembered_on_user_prompt = false
}
email_configuration {
}
admin_create_user_config {
allow_admin_create_user_only = false
invite_message_template {
email_message = "Your username is {username} and temporary password is {####}. "
email_subject = "Your temporary password"
sms_message = "Your username is {username} and temporary password is {####}. "
2021-01-30 11:07:34 +00:00
}
2021-02-01 06:08:59 +00:00
}
account_recovery_setting {
recovery_mechanism {
name = "verified_email"
priority = 1
2021-01-30 11:07:34 +00:00
}
2021-02-01 06:08:59 +00:00
}
2021-01-30 11:07:34 +00:00
}
resource "aws_cognito_user_pool_client" "CognitoUserPoolClient" {
2021-02-01 06:08:59 +00:00
user_pool_id = aws_cognito_user_pool.CognitoUserPool.id
name = "AWSElasticsearch-sondes-v2-us-east-1-hiwdpmnjbuckpbwfhhx65mweee"
refresh_token_validity = 30
allowed_oauth_flows = ["code"]
allowed_oauth_flows_user_pool_client = true
allowed_oauth_scopes = ["email", "openid", "phone", "profile"]
callback_urls = ["https://es.${local.domain_name}/_plugin/kibana/app/kibana"]
logout_urls = ["https://es.${local.domain_name}/_plugin/kibana/app/kibana"]
supported_identity_providers = ["COGNITO", "Google"]
explicit_auth_flows = ["ALLOW_CUSTOM_AUTH", "ALLOW_REFRESH_TOKEN_AUTH", "ALLOW_USER_SRP_AUTH"]
2021-01-30 11:07:34 +00:00
}