Publish Event SNS

0.0.3 · active · verified Sun Apr 12

This Python library, currently at version 0.0.3, aims to simplify publishing messages to AWS SNS topics with attributes. Given its minimal version and summary, it is likely a lightweight wrapper around the official AWS SDK for Python, `boto3`. It appears to be a niche utility, as detailed documentation and a distinct release cadence are not publicly available beyond its PyPI entry.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to publish a structured message with attributes to an AWS SNS topic. It assumes the `publish-event-sns` library wraps `boto3`'s SNS client. It includes a fallback to direct `boto3` usage if the specific `publish_event_sns` module cannot be imported or its API differs, ensuring the example remains functional for general SNS publishing. AWS credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) and the AWS_REGION should be configured via environment variables or other boto3-supported methods. The SNS_TOPIC_ARN must be replaced with your actual topic ARN.

import os
import json
from publish_event_sns import publish_event # Assumed import based on library name

# Fallback to boto3 if 'publish_event_sns' module is not found or has a different API
try:
    from publish_event_sns import publish_event
except ImportError:
    import boto3
    print("Using boto3 directly as 'publish_event_sns' module not found or API differs.")
    def publish_event(topic_arn, message, message_attributes=None, region_name=None):
        sns_client = boto3.client(
            'sns',
            region_name=region_name or os.environ.get('AWS_REGION', 'us-east-1'),
            aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', ''),
            aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', '')
        )
        response = sns_client.publish(
            TopicArn=topic_arn,
            Message=json.dumps(message),
            MessageAttributes=message_attributes or {}
        )
        return response

# Replace with your actual SNS Topic ARN
SNS_TOPIC_ARN = os.environ.get('SNS_TOPIC_ARN', 'arn:aws:sns:REGION:ACCOUNT_ID:YOUR_TOPIC_NAME')

# Example message and attributes
event_message = {
    'detail-type': 'OrderCreated',
    'source': 'my.application',
    'detail': {
        'orderId': '12345',
        'customerEmail': 'test@example.com'
    }
}

message_attrs = {
    'eventType': {
        'DataType': 'String',
        'StringValue': 'OrderCreated'
    },
    'priority': {
        'DataType': 'Number',
        'StringValue': '1'
    }
}

try:
    response = publish_event(SNS_TOPIC_ARN, event_message, message_attrs, region_name='us-east-1')
    print(f"Message published: {response.get('MessageId')}")
except Exception as e:
    print(f"Error publishing message: {e}")
    print("Ensure AWS credentials and SNS_TOPIC_ARN are correctly configured.")

view raw JSON →