sondehub-infra/sqs_to_elk.tf

140 lines
3.6 KiB
Terraform
Raw Normal View History

2021-05-20 08:43:50 +00:00
resource "aws_iam_role" "sqs_to_elk" {
path = "/service-role/"
name = "sqs-to-elk"
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" "sqs_to_elk" {
name = "sqs_to_elk"
role = aws_iam_role.sqs_to_elk.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": "*"
}
]
}
EOF
}
resource "aws_lambda_function" "sqs_to_elk" {
function_name = "sqs-to-elk"
2021-12-20 07:13:24 +00:00
handler = "sqs_to_elk.lambda_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
2021-12-20 06:02:02 +00:00
source_code_hash = data.archive_file.lambda.output_base64sha256
2021-05-20 08:43:50 +00:00
publish = true
memory_size = 128
role = aws_iam_role.sqs_to_elk.arn
2021-10-04 08:08:54 +00:00
runtime = "python3.9"
2021-05-20 08:43:50 +00:00
timeout = 5
2021-05-23 13:14:15 +00:00
reserved_concurrent_executions = 100
2021-05-20 08:43:50 +00:00
environment {
variables = {
2021-10-08 11:17:11 +00:00
"ES" = aws_route53_record.es.fqdn
2021-05-20 08:43:50 +00:00
}
2021-10-04 08:08:54 +00:00
}
tags = {
Name = "sqs_to_elk"
}
2021-05-20 08:43:50 +00:00
}
resource "aws_lambda_event_source_mapping" "sqs_to_elk" {
event_source_arn = aws_sqs_queue.sqs_to_elk.arn
function_name = aws_lambda_function.sqs_to_elk.arn
batch_size = 20
maximum_batching_window_in_seconds = 15
}
resource "aws_sns_topic_subscription" "sqs_to_elk" {
topic_arn = aws_sns_topic.sonde_telem.arn
protocol = "sqs"
endpoint = aws_sqs_queue.sqs_to_elk.arn
}
resource "aws_sqs_queue" "sqs_to_elk" {
name = "to-elk"
receive_wait_time_seconds = 1
message_retention_seconds = 1209600 # 14 days
2021-12-12 11:22:00 +00:00
redrive_policy = jsonencode(
{
deadLetterTargetArn = "arn:aws:sqs:us-east-1:143841941773:to-elk-dlq"
2022-03-05 23:31:04 +00:00
maxReceiveCount = 100
2021-12-12 11:22:00 +00:00
}
)
2022-03-05 23:31:04 +00:00
visibility_timeout_seconds = 10
2021-05-20 08:43:50 +00:00
}
resource "aws_sqs_queue_policy" "sqs_to_elk" {
queue_url = aws_sqs_queue.sqs_to_elk.id
policy = <<EOF
{
"Version": "2008-10-17",
"Id": "__default_policy_ID",
"Statement": [
{
"Sid": "__owner_statement",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
},
"Action": "SQS:*",
"Resource": "${aws_sqs_queue.sqs_to_elk.arn}"
},
{
"Sid": "to-elk",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "SQS:SendMessage",
"Resource": "${aws_sqs_queue.sqs_to_elk.arn}",
"Condition": {
"ArnLike": {
"aws:SourceArn": "${aws_sns_topic.sonde_telem.arn}"
}
}
}
]
}
EOF
}