Skip to content

Instantly share code, notes, and snippets.

@g-a-d
Last active August 22, 2021 06:55
Show Gist options
  • Save g-a-d/596ff7b21c7fc8245e5453ada91af291 to your computer and use it in GitHub Desktop.
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
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'])
@g-a-d
Copy link
Author

g-a-d commented Aug 31, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment