# AWS SNS

SNS topics, publishing notifications and managing subscriptions from Python services.

Zato lets you publish notifications through Amazon SNS directly from your Python services. You create an [AWS connection](https://zato.io/docs/dev/examples/cloud/aws/index.html) in the Dashboard, and the SNS client is available under `conn.sns` - the same client that `boto3.client('sns')` returns, with credentials and configuration managed for you.

## Publishing to a topic {#publishing-to-a-topic}

```python
# -*- coding: utf-8 -*-

# stdlib
import json

# Zato
from zato.server.service import Service

class NotifyOrderShipped(Service):

    input = 'order_id', 'topic_arn'

    def handle(self):

        # Get the connection by its Dashboard name
        conn = self.aws['My AWS']

        # Publish the notification
        message = json.dumps({'order_id': self.request.input.order_id, 'status': 'shipped'})

        response = conn.sns.publish(
            TopicArn=self.request.input.topic_arn,
            Subject='Order shipped',
            Message=message,
        )

        self.response.payload = {'message_id': response['MessageId']}
```

## Creating topics {#creating-topics}

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class CreateOrderTopic(Service):

    def handle(self):

        conn = self.aws['My AWS']

        # Create the topic - the call also succeeds if the topic already exists,
        # returning its ARN.
        response = conn.sns.create_topic(Name='order-events')

        self.response.payload = {'topic_arn': response['TopicArn']}
```

## Managing subscriptions {#managing-subscriptions}

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class SubscribeQueueToTopic(Service):

    input = 'topic_arn', 'queue_arn'

    def handle(self):

        conn = self.aws['My AWS']

        # Deliver every notification from the topic to an SQS queue
        response = conn.sns.subscribe(
            TopicArn=self.request.input.topic_arn,
            Protocol='sqs',
            Endpoint=self.request.input.queue_arn,
        )

        self.response.payload = {'subscription_arn': response['SubscriptionArn']}
```

To see what is already subscribed, call `conn.sns.list_subscriptions_by_topic(TopicArn=...)`, and to remove a subscription, call `conn.sns.unsubscribe(SubscriptionArn=...)`.

## More resources {#more-resources}

- [AWS integrations overview](https://zato.io/docs/dev/examples/cloud/aws/index.html)
- [AWS tutorial](https://zato.io/docs/dev/examples/cloud/aws/tutorial.html)
- [boto3 SNS reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns.html)

## Learn more {#learn-more}

- [Development documentation](https://zato.io/docs/dev/) - The complete reference for writing services
- [Requests and responses](https://zato.io/docs/dev/request-response/) - What a service receives, what it returns and how both are structured
- [Integration examples](https://zato.io/docs/dev/examples/) - Ready-made code for the systems you are likely to connect to
- [IDE and debugging](https://zato.io/docs/dev/ide/) - Write services in the Dashboard or in your own editor
- [Data models](https://zato.io/docs/dev/model/) - Declare inputs and outputs and have them validated for you
- [In-depth API tutorial](https://zato.io/tutorials/main/01.html) - The full platform tutorial, from installation to production patterns
