Lambda from SNS or CLI

One of the really exciting promises of Lambda and the modern AWS services is the idea of an event bus deeply built into the infrastructure. The reality is definitely clunkier.

That said it still feels like TheRightWay(tm) to deploy lambdas is to tie them together using SNS.

However when you’re developing lambdas it’s often much simpler to simply invoke it directly from the CLI.

Which means your lambda needs to deal with polymorphic events.

Here is the pattern I’ve developed.

def handler

.... logic ... 

if 'Records' in event:
        message = extract_sns_message(event, context)
    else:
        # else called directly
        message = event

.... logic ...

def extract_sns_message(event, context):
    for record in event['Records']:
        logging.info('looking at {}'.format(record))
        if 'aws:sns' == record['EventSource']:
            message = json.loads(record['Sns']['Message'])
            return message

Then I can either invoke the lambda via SNS

    message = {'bucket': key.bucket_name, 'key': key.key}

    sns.publish(
        TargetArn=config.input_sns_topic,
        Message=json.dumps({'default': json.dumps(message)}),
        MessageStructure='json'
    )

Or directly

aws lambda invoke --function-name Dynamo_PutPy --region us-west-2 outputfile.txt

Leave a comment