Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active February 14, 2021 21:10
Show Gist options
  • Save rpivo/8215bb27bf4105bbc31e4109ccba1281 to your computer and use it in GitHub Desktop.
Save rpivo/8215bb27bf4105bbc31e4109ccba1281 to your computer and use it in GitHub Desktop.
Publishing a Message to an AWS SNS Topic From an AWS Lambda Function Using Boto3

Publishing a Message to an AWS SNS Topic From an AWS Lambda Function Using Boto3

import json
import boto3

def lambda_handler(event, context):
    message = {'key': 'value'}

    boto3.client('sns').publish(
        TargetArn='YOUR ARN HERE',
        Message=json.dumps({'default': json.dumps(message)}),
        MessageStructure='json'
    )

Inside a Python AWS Lambda function, you can import boto3, which is the AWS SDK for Python.

You can then create a low-level client representing the SNS service with boto3.client('sns'). With that client, you can call the publish() method to publish a message to an SNS topic that has a given ARN.

References

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