From d8465066c8c9d8957c733f8c7dbb5ab835dda7a6 Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 30 Aug 2021 17:34:32 +0100 Subject: [PATCH] Add code to publish messages to sns topic --- api-lambda/requirements.txt | 8 ++++++++ api-lambda/src/request_handler.py | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 api-lambda/requirements.txt create mode 100644 api-lambda/src/request_handler.py diff --git a/api-lambda/requirements.txt b/api-lambda/requirements.txt new file mode 100644 index 0000000..56eedb1 --- /dev/null +++ b/api-lambda/requirements.txt @@ -0,0 +1,8 @@ +boto3==1.18.31 +botocore==1.21.31 +jmespath==0.10.0 +python-dateutil==2.8.2 +s3transfer==0.5.0 +six==1.16.0 +urllib3==1.26.6 +python-decouple==3.4 diff --git a/api-lambda/src/request_handler.py b/api-lambda/src/request_handler.py new file mode 100644 index 0000000..f2f61b1 --- /dev/null +++ b/api-lambda/src/request_handler.py @@ -0,0 +1,25 @@ +import json + +from boto3 import Session +from decouple import config + + +def event_handler(event, context): + session: Session = configure_boto3_session() + client = session.client("sns") + + client.publish( + TargetArn=config("sns_topic_arn"), + Message=json.dumps({'default': json.dumps(event)}), + MessageStructure='json' + ) + + return "Tweet Successfully Sent" + + +def configure_boto3_session() -> Session: + return Session( + aws_access_key_id=config("aws_access_key_id"), + aws_secret_access_key=config("aws_secret_access_key"), + region_name="eu-west-1" + )