From dd2894783366c38217d5380c95724ff78de00f4b Mon Sep 17 00:00:00 2001 From: Michaela Wheeler Date: Mon, 17 Jan 2022 10:19:56 +1100 Subject: [PATCH] fix websockets and add amateur --- ham_ingestion.tf | 36 ++++++++++++++++++++++++++++++++++ ingestion.tf | 6 ++++-- lambda/sns_to_mqtt/__init__.py | 6 +++--- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/ham_ingestion.tf b/ham_ingestion.tf index 786f262..276d557 100644 --- a/ham_ingestion.tf +++ b/ham_ingestion.tf @@ -210,4 +210,40 @@ resource "aws_apigatewayv2_integration" "ham_upload_telem" { integration_uri = aws_lambda_function.ham_upload_telem.arn timeout_milliseconds = 30000 payload_format_version = "2.0" +} + +// SNS to MQTT + +resource "aws_lambda_function" "ham_sns_to_mqtt" { + function_name = "ham-sns-to-mqtt" + handler = "sns_to_mqtt.lambda_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.basic_lambda_role.arn + runtime = "python3.9" + timeout = 3 + architectures = ["arm64"] + lifecycle { + ignore_changes = [environment] + } + tags = { + Name = "sns-to-mqtt" + } + +} + +resource "aws_lambda_permission" "ham_sns_to_mqtt" { + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.ham_sns_to_mqtt.arn + principal = "sns.amazonaws.com" + source_arn = aws_sns_topic.ham_telem.arn +} + +resource "aws_sns_topic_subscription" "ham_sns_to_mqtt" { + topic_arn = aws_sns_topic.ham_telem.arn + protocol = "lambda" + endpoint = aws_lambda_function.ham_sns_to_mqtt.arn } \ No newline at end of file diff --git a/ingestion.tf b/ingestion.tf index f662539..94bb274 100644 --- a/ingestion.tf +++ b/ingestion.tf @@ -114,8 +114,10 @@ EOF resource "aws_lambda_function" "sns_to_mqtt" { function_name = "sns-to-mqtt" - handler = "lambda_function.lambda_handler" - filename = "${path.module}/sns-to-mqtt/Archive.zip" # this should get replaced out when we make a proper build chain + handler = "sns_to_mqtt.lambda_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.basic_lambda_role.arn diff --git a/lambda/sns_to_mqtt/__init__.py b/lambda/sns_to_mqtt/__init__.py index 8db3b37..fbb7111 100644 --- a/lambda/sns_to_mqtt/__init__.py +++ b/lambda/sns_to_mqtt/__init__.py @@ -1,5 +1,5 @@ import sys -sys.path.append("vendor") +sys.path.append("sns_to_mqtt/vendor") import json import os import paho.mqtt.client as mqtt @@ -52,11 +52,11 @@ def lambda_handler(event, context): body = json.dumps(payload) - serial = payload['serial'] + serial = payload[os.getenv("MQTT_ID")] while not connected_flag: time.sleep(0.01) # wait until connected client.publish( - topic=f'sondes/{serial}', + topic=f'{os.getenv("MQTT_PREFIX")}/{serial}', payload=body, qos=0, retain=False