diff --git a/cdn.tf b/cdn.tf index 5df456c..5178c50 100644 --- a/cdn.tf +++ b/cdn.tf @@ -14,6 +14,25 @@ resource "aws_lambda_function" "redirect" { source_code_hash = data.archive_file.lambda.output_base64sha256 } +resource "aws_lambda_function" "ham_redirect" { + function_name = "ham-sondehub-redirect" + handler = "redirect_ham.handler" + s3_bucket = aws_s3_bucket_object.lambda.bucket + s3_key = aws_s3_bucket_object.lambda.key + publish = true + memory_size = 128 + role = aws_iam_role.basic_lambda_role.arn + runtime = "python3.9" + timeout = 3 + source_code_hash = data.archive_file.lambda.output_base64sha256 +} + +resource "aws_lambda_permission" "ham_redirect" { + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.ham_redirect.arn + principal = "edgelambda.amazonaws.com" +} + resource "aws_route53_record" "testing_A" { @@ -515,6 +534,31 @@ resource "aws_cloudfront_distribution" "amateur" { smooth_streaming = false target_origin_id = "S3-${local.domain_name}/amateur" viewer_protocol_policy = "redirect-to-https" + lambda_function_association { + event_type = "viewer-request" + lambda_arn = aws_lambda_function.ham_redirect.qualified_arn + } + } + ordered_cache_behavior { + allowed_methods = ["GET", "HEAD", "OPTIONS"] + cached_methods = [ + "HEAD", + "GET" + ] + compress = true + default_ttl = 120 + forwarded_values { + cookies { + forward = "none" + } + query_string = false + } + max_ttl = 120 + min_ttl = 120 + path_pattern = "*.*" + smooth_streaming = false + target_origin_id = "S3-${local.domain_name}/amateur" + viewer_protocol_policy = "redirect-to-https" } comment = "" price_class = "PriceClass_All" diff --git a/lambda/query/__init__.py b/lambda/query/__init__.py index d3d5294..dadbe70 100644 --- a/lambda/query/__init__.py +++ b/lambda/query/__init__.py @@ -122,7 +122,7 @@ def get_telem(event, context): lt = requested_time + timedelta(0, 1) gte = requested_time - timedelta(0, duration) - path = f"telm-{lt.year:2}-{lt.month:02},telm-{gte.year:2}-{gte.month:02}/_search" + path = f"telm-*/_search" payload = { "timeout": "30s", "size": 0, diff --git a/lambda/redirect_ham/__init__.py b/lambda/redirect_ham/__init__.py new file mode 100644 index 0000000..3fc9dfb --- /dev/null +++ b/lambda/redirect_ham/__init__.py @@ -0,0 +1,30 @@ +import re +def redirect(location): + return { + "status": '302', + "statusDescription": 'Found', + "headers": { + "location": [{ + "key": 'Location', + "value": location + }], + }, + } + +def handler(event, context): + request = event["Records"][0]["cf"]["request"] + uri = request["uri"] + if uri.startswith('/aprs/'): + sonde = uri.replace("/aprs/","") + return redirect('https://aprs.fi/#!call=' + sonde + '&timerange=36000&tail=36000') + if uri.startswith('/site/'): + site = uri.replace("/site/", "") + return redirect('https://amateur.sondehub.org/#!site=' + site) + if uri.startswith('/go/donate'): + return redirect('https://www.paypal.com/donate/?hosted_button_id=4V7L43MD5CQ52') + if uri.startswith('/go/status'): + return redirect("https://cloudwatch.amazonaws.com/dashboard.html?dashboard=SondeHub&context=eyJSIjoidXMtZWFzdC0xIiwiRCI6ImN3LWRiLTE0Mzg0MTk0MTc3MyIsIlUiOiJ1cy1lYXN0LTFfZ2NlT3hwUnp0IiwiQyI6IjNuOWV0Y2ZxZm9zdm11aTc0NTYwMWFzajVzIiwiSSI6InVzLWVhc3QtMTo0ODI5YmQ4MC0yZmYzLTQ0MDktYjI1ZS0yOTE4MTM5YTgwM2MiLCJNIjoiUHVibGljIn0%3D") + if uri != '/': + uri = re.sub(r"^\/","", uri) + return redirect('https://amateur.sondehub.org/?sondehub=1#!f=' + sonde + '&mz=9&qm=All&q=' + sonde) + return request \ No newline at end of file diff --git a/lambda/redirect_ham/__main__.py b/lambda/redirect_ham/__main__.py new file mode 100644 index 0000000..26a97de --- /dev/null +++ b/lambda/redirect_ham/__main__.py @@ -0,0 +1,39 @@ +from . import * + +print(handler({ + "Records": [ + { + "cf": { + "config": { + "distributionId": "EXAMPLE" + }, + "request": { + "uri": "/MRZ-S1234", + "querystring": "auth=test&foo=bar", + "method": "GET", + "clientIp": "2001:cdba::3257:9652", + "headers": { + "host": [ + { + "key": "Host", + "value": "d123.cf.net" + } + ], + "user-agent": [ + { + "key": "User-Agent", + "value": "Test Agent" + } + ], + "user-name": [ + { + "key": "User-Name", + "value": "aws-cloudfront" + } + ] + } + } + } + } + ] +}, None)) \ No newline at end of file diff --git a/lambda/sqs_to_elk/__init__.py b/lambda/sqs_to_elk/__init__.py index d370ab1..a26eddf 100644 --- a/lambda/sqs_to_elk/__init__.py +++ b/lambda/sqs_to_elk/__init__.py @@ -2,10 +2,10 @@ import json import es import zlib import base64 - +import datetime def lambda_handler(event, context): - payloads = {} + payloads = [] for record in event['Records']: sns_message = json.loads(record["body"]) try: @@ -16,25 +16,20 @@ def lambda_handler(event, context): incoming_payloads = [decoded] else: incoming_payloads = decoded - for payload in incoming_payloads: - index = payload['datetime'][:7] - - if index not in payloads: # create index if not exists - payloads[index] = [] - - payloads[index].append(payload) - - for index in payloads: - body="" - for payload in payloads[index]: - body += "{\"index\":{}}\n" + json.dumps(payload) + "\n" - body += "\n" + year, week = datetime.datetime.now().isocalendar()[:2] + index = f"{year}-{week}" + payloads += incoming_payloads - result = es.request(body, f"telm-{index}/_doc/_bulk", "POST") - if 'errors' in result and result['errors'] == True: - error_types = [x['index']['error']['type'] for x in result['items'] if 'error' in x['index']] # get all the error types - print(event) - print(result) - error_types = [a for a in error_types if a != 'mapper_parsing_exception'] # filter out mapper failures since they will never succeed - if error_types: - raise RuntimeError \ No newline at end of file + body="" + for payload in payloads: + body += "{\"index\":{}}\n" + json.dumps(payload) + "\n" + body += "\n" + + result = es.request(body, f"telm-{index}/_doc/_bulk", "POST") + if 'errors' in result and result['errors'] == True: + error_types = [x['index']['error']['type'] for x in result['items'] if 'error' in x['index']] # get all the error types + print(event) + print(result) + error_types = [a for a in error_types if a != 'mapper_parsing_exception'] # filter out mapper failures since they will never succeed + if error_types: + raise RuntimeError \ No newline at end of file diff --git a/websockets.tf b/websockets.tf index 0e53f6a..febce7c 100644 --- a/websockets.tf +++ b/websockets.tf @@ -541,7 +541,7 @@ resource "aws_ecs_service" "ws_reader_ec2" { task_definition = aws_ecs_task_definition.ws_reader_ec2.arn enable_ecs_managed_tags = true launch_type = "EC2" - desired_count = 3 + desired_count = 6 placement_constraints { type = "distinctInstance" }