Publish Event SNS
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
- gotcha The `publish-event-sns` library (v0.0.3) has minimal public documentation. The provided import paths and quickstart are based on assumptions of a thin wrapper around `boto3`. If the library's internal API differs, direct `boto3` usage for `sns.publish` will be necessary.
- breaking AWS IAM permissions are crucial. The AWS user or role publishing messages must have `sns:Publish` permission for the target SNS topic. Without it, `boto3.client('sns').publish` (and by extension, likely `publish-event-sns`) calls will result in an `AuthorizationError` or similar access denied exceptions.
- gotcha Messages published to SNS have a size limit of 256 KB (262,144 bytes). For SMS, the limit is 140-160 characters depending on encoding. Attempting to publish larger messages will result in an error.
- gotcha SNS FIFO topics require `MessageGroupId` and optionally `MessageDeduplicationId` parameters. Publishing to a FIFO topic without these will result in an error. Standard topics do not require these.
- gotcha If the publishing environment (e.g., AWS Lambda, EC2 instance) is within a private VPC, ensure it has outbound internet access via a NAT Gateway or a VPC endpoint for SNS to reach the SNS service. Otherwise, publish calls may time out.
Install
-
pip install publish-event-sns
Imports
- publish_event
from publish_event_sns import publish_event
Quickstart
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.")