fix websockets and add amateur

This commit is contained in:
Michaela Wheeler 2022-01-17 10:19:56 +11:00
parent 1b26448fbd
commit dd28947833
3 changed files with 43 additions and 5 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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