Last active
August 22, 2021 06:55
-
-
Save g-a-d/596ff7b21c7fc8245e5453ada91af291 to your computer and use it in GitHub Desktop.
Process either S3 event notifications or SNS messages with python in AWS Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
def lambda_handler(event, context): | |
for record in event['Records']: | |
try: | |
if 'aws:sns' == record['EventSource'] and record['Sns']['Message']: | |
record = json.loads(record['Sns']['Message'])['Records'][0] | |
except KeyError: | |
pass | |
bucket = record['s3']['bucket']['name'] | |
key = urllib.unquote(record['s3']['object']['key']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When configuring AWS S3 events you can either choose to send to an SNS topic or invoke Lambda directly.
The above code snippet supports both types of invocation, so you can easily move from a direct invoke to a fan-out architecture without rearchitecting the code.