From cd45b9ef269e5ed30d2e7cec40dcd93eba686cca Mon Sep 17 00:00:00 2001 From: frikky Date: Sat, 4 Sep 2021 21:30:52 +0200 Subject: [PATCH] Added a S3 -> Webhook forwarder --- functions/extensions/aws-lambda/README.md | 2 ++ functions/extensions/aws-lambda/s3_deploy.sh | 16 ++++++++++++ .../extensions/aws-lambda/s3_function.py | 26 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 functions/extensions/aws-lambda/README.md create mode 100644 functions/extensions/aws-lambda/s3_deploy.sh create mode 100644 functions/extensions/aws-lambda/s3_function.py diff --git a/functions/extensions/aws-lambda/README.md b/functions/extensions/aws-lambda/README.md new file mode 100644 index 00000000..f665851a --- /dev/null +++ b/functions/extensions/aws-lambda/README.md @@ -0,0 +1,2 @@ +# AWS Lambda forwarder to Shuffle +This function is made to forward S3 notifications to Shuffle to run a workflow when an object is made or updated. diff --git a/functions/extensions/aws-lambda/s3_deploy.sh b/functions/extensions/aws-lambda/s3_deploy.sh new file mode 100644 index 00000000..cedda90f --- /dev/null +++ b/functions/extensions/aws-lambda/s3_deploy.sh @@ -0,0 +1,16 @@ +#GOOS=linux go build main.go +zip s3_function.zip s3_function.py + +WEBHOOK=https://shuffler.io/api/v1/hooks/webhook_eccf47b1-8f6a-49fc-b2b8-383365a22353 +ROLE=arn:aws:iam::202262580068:role/service-role/shuffle-forwarder +REGION=us-east-1 +BUCKETNAME=helo + +aws lambda create-function \ + --role $ROLE \ + --region $REGION \ + --function-name shuffler-webhook-forwarder-3 \ + --zip-file fileb://s3_function.zip \ + --runtime python3.9 \ + --environment Variables={SHUFFLE_WEBHOOK=$WEBHOOK} \ + --handler lambda_handler diff --git a/functions/extensions/aws-lambda/s3_function.py b/functions/extensions/aws-lambda/s3_function.py new file mode 100644 index 00000000..c183cb89 --- /dev/null +++ b/functions/extensions/aws-lambda/s3_function.py @@ -0,0 +1,26 @@ +import json +import urllib.parse +import requests +import os + +#print('Loading function') + +def lambda_handler(event, context): + #print("Received event: " + json.dumps(event, indent=2)) + + # Get the object from the event and show its content type + bucket = event['Records'][0]['s3']['bucket']['name'] + + print(type(event)) + print("Getting bucket: %s" % bucket) + webhook = os.environ.get("SHUFFLE_WEBHOOK") + if not webhook: + return "No webhook environment defined: SHUFFLE_WEBHOOK" + + ret = requests.post(webhook, json=event["Records"][0]) + if ret.status_code != 200: + return "Bad status code for webhook: %d" % ret.status_code + + print("Status code: %d\nData: %s" % (ret.status_code, ret.text)) + + # response = s3.get_object(Bucket=bucket, Key=key)