Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Amazon SNS Connector

Select

The Anypoint Connector for Amazon SNS provides connectivity to the Amazon Simple Notification Service (Amazon SNS) API, enabling you to build distributed web-enabled applications. Mule applications can use the Amazon SNS Connector to easily push real-time notification messages to interested subscribers over multiple delivery protocols.

Prerequisites

To be able to use the Amazon SNS Connector, you must have the following:

  • Access to Amazon Web Services - SNS.

  • To access AWS with the connector, you need the credentials in the form of IAM.

  • Anypoint Studio version 7.0 (or higher) or Anypoint Design Center.

Compatibility

Amazon SNS connector is compatible with:

Application/Service Version

Mule Runtime

4.0.0

AWS SDK for Java

1.11.79

Java

1.8.0_x or higher

Anypoint Studio

7.0 or higher

Install the Connector

  1. In Anypoint Studio, click the Exchange icon in the Studio taskbar.

  2. Click Login in Anypoint Exchange.

  3. Search for the connector and click Install.

  4. Follow the prompts to install the connector.

When Studio has an update, a message displays in the lower right corner, which you can click to install the update.

Configuring the Connector Global Element

To use the Amazon SNS connector in your Mule application, configure a global Amazon SNS element that can be used by all the Amazon SNS connectors in the application.

Configuring with Studio Visual Editor

  1. Click the Global Elements tab at the base of the canvas.

  2. In the Global Configuration Elements screen, click Create. Following window would be displayed.

    Global Element Configuration Wizard

  3. In the Choose Global Type wizard, expand Connector Configuration and select Amazon SNS Configuration and click Ok. Following window would be displayed.

    Global Element Properties

    In the image above, the placeholder values refer to a configuration file placed in the src folder of your project.

  4. Configure the parameters according to instructions below.

    Parameter Description

    Name

    Enter a name for the configuration to reference it.

    Access Key

    Alphanumeric text string that uniquely identifies the user who owns the account.

    Secret Key

    Key that plays the role of a password.

    Try Default AWS Credentials Provider Chain

    Checkbox that controls whether temporary credentials should be used.

    Region Endpoint

    Set the topic region endpoint.

    Topic Arn

    Topic ARN to test the connectivity.

    You can either enter your credentials into the global configuration properties, or reference a configuration file that contains these values. For simpler maintenance and better reusability of your project, Mule recommends that you use a configuration file. Keeping these values in a separate file is useful if you need to deploy to different environments, such as production, development, and QA, where your access credentials differ.

  5. Keep the Pooling Profile and the Reconnection tabs with their default entries.

  6. Click Test Connection to confirm that the parameters of your global configuration are accurate, and that Mule is able to successfully connect to your instance of Amazon SNS.

  7. Click OK to save the global connector configurations.

Configuring with XML Editor or Standalone

  1. Ensure that you have included the Amazon SNS namespaces in your configuration file.

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <?xml version="1.0"?>
    
    <mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns:sns="http://www.mulesoft.org/schema/mule/sns"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
    http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/sns
    http://www.mulesoft.org/schema/mule/sns/current/mule-sns.xsd
    http://www.mulesoft.org/schema/mule/http
    http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    
    <!-- Put your flows and configuration elements here -->
    
    </mule>
  2. Create a global Amazon SNS configuration outside and above your flows, using the following global configuration code.

    
                 
              
    1
    2
    3
    
    <sns:config name="Amazon_SNS_configuration" doc:name="Amazon SNS configuration" doc:id="f85b91a1-661e-4bf4-80c7-997107acda08">
    <sns:basic-connection accessKey="${config.accessKey}" secretKey="${config.secretKey}" testTopicArn="${sns.topic.arn}" />
    </sns:config>

    If you or your IAM users forget or lose the secret access key, you can create a new access key.

Using This Connector

Amazon SNS connector is an operation-based connector, which means that when you add the connector to your flow, you need to configure a specific operation for the connector to perform. The connector currently supports the following list of operations:

Operation Description

Add Permission

Adds a statement to a topic’s access control policy

Confirm Subscription

The ConfirmSubscription action verifies an endpoint owner’s intent to receive messages by validating the token sent to the endpoint by an earlier Subscribe action.

Create Platform Application

Creates a platform application object for one of the supported push notification services, such as APNS and GCM, to which devices and mobile apps may register.

Create Platform Endpoint

Creates an endpoint for a device and mobile app on one of the supported push notification services.

Create Topic

The CreateTopic action creates a topic to which notifications can be published.

Delete Endpoint

Deletes the endpoint for a device and mobile app from Amazon SNS.

Delete Platform Application

Deletes a platform application object for one of the supported push notification services, such as APNS and GCM.

Delete Topic

The DeleteTopic action deletes a topic and all its subscriptions.

Get Endpoint Attributes

Retrieves the endpoint attributes for a device on one of the supported push notification services, such as GCM and APNS.

Get Platform Application Attributes

Retrieves the attributes of the platform application object for the supported push notification services, such as APNS and GCM.

Get Subscription Attributes

The GetSubscriptionAttributes action returns all of the properties of a subscription.

Get Topic Attributes

The GetTopicAttributes action returns all of the properties of a topic.

List Endpoints By Platform Application

Lists the endpoints and endpoint attributes for devices in a supported push notification service, such as GCM and APNS.

List Platform Applications

Lists the platform application objects for the supported push notification services, such as APNS and GCM.

List Subscriptions By Topic

The SubscriptionsByTopic action returns a list of the subscriptions to a specific topic.

List Subscriptions

The ListSubscriptions action returns a list of the requester’s subscriptions.

List Topics

The ListTopics action returns a list of the requester’s topics.

Publish

The Publish action sends a message to all of a topic’s subscribed endpoints.

Remove Permission

The RemovePermission action removes a statement from a topic’s access control policy.

Set Endpoint Attributes

Sets the attributes for an endpoint for a device on one of the supported push notification services, such as GCM and APNS.

Set Platform Application Attributes

Sets the attributes of the platform application object for the supported push notification services, such as APNS and GCM.

Set Subscription Attributes

The SubscriptionAttributes action allows a subscription owner to set an attribute of the topic to a new value.

Set Topic Attributes

The TopicAttributes action allows a topic owner to set an attribute of the topic to a new value.

Subscribe

The Subscribe action prepares to subscribe an endpoint by sending the endpoint a confirmation message.

Unsubscribe

The Unsubscribe action deletes a subscription.

Connector Namespace and Schema

When designing your application in Studio, the act of dragging the connector from the palette onto the Anypoint Studio canvas should automatically populate the XML code with the connector namespace and schema location.

Namespace: http://www.mulesoft.org/schema/mule/sns Schema Location: http://www.mulesoft.org/schema/mule/sns/current/mule-sns.xsd

If you are manually coding the Mule application in Studio’s XML editor or other text editor, paste the namespace and schema into your Configuration XML.


          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:sns="http://www.mulesoft.org/schema/mule/sns"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/sns
http://www.mulesoft.org/schema/mule/sns/current/mule-sns.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

<!-- Put your flows and configuration elements here -->

</mule>

Specification of connector dependency

To reference the SNS connector from Mule application, the following XML snippet must be included in your pom.xml file.


          
       
1
2
3
4
5
6
<dependency>
        <groupId>org.mule.connectors</groupId>
    <artifactId>mule-sns-connector</artifactId>
    <version>4.0.0</version>
    <classifier>mule-plugin</classifier>
</dependency>

Use Cases and Demos

Common use cases for the connector:

Use Case Description

Sending Amazon SNS messages to Amazon SQS queues

Amazon SNS works closely with Amazon Simple Queue Service (Amazon SQS).By using Amazon SNS and Amazon SQS together, messages can be delivered to applications that require immediate notification of an event, and also persisted in an Amazon SQS queue for other applications to process at a later time.

Sending Amazon SNS messages to HTTP/HTTPS endpoints

You can use Amazon SNS to send notification messages to one or more HTTP or HTTPS endpoints. When you subscribe an endpoint to a topic, you can publish a notification to the topic and Amazon SNS sends an HTTP POST request delivering the contents of the notification to the subscribed endpoint.

Demo Mule Application Using the Connector

Send messages to Amazon SQS Queue.

When you subscribe an Amazon SQS queue to an Amazon SNS topic, you can publish a message to the topic and Amazon SNS sends an Amazon SQS message to the subscribed queue.

You can subscribe an Amazon SQS queue to an Amazon SNS topic using the AWS Management Console for Amazon SQS, which simplifies the process.

Sending messages to SQS Queue

  1. Create a new Mule Project in Anypoint Studio.

  2. Add the below properties to mule-artifact.properties file to hold your Amazon SNS and SQS credentials and place it in the project’s src/main/app directory.

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    sqs.accesskey=<Access Key>
    sqs.secretkey=<Secret Key>
    sns.topic.arn=<SNS Topic ARN>
    user can select specific region by using the dropdown.
    by default the region will be taken as US_EAST1
    
    sqs.queueName=<SQS Queue Name>
    user can select specific region by using the dropdown.
    sqs.queueUrl=<SQS Queue URL>
  3. Click on a Mule HTTP Connector and select Listener operation, drag it to the beginning of the flow and configure the following parameters:

    sns http config props

    Parameter Value

    Display Name

    HTTP

    Extension Configuration

    If no HTTP element has been created yet, click the plus sign to add a new HTTP Listener Configuration and click OK (Fill 0.0.0.0 for Host and 8081 for port).

    Path

    /

  4. Click on the Amazon SNS Connector and select the operation "Publish" and drag next to the HTTP endpoint component.

  5. Configure the SNS connector by adding a new Amazon SNS Global Element. Click the plus sign next to the Connector Configuration field.

    1. Configure the global element according to the table below:

      Parameter Description Value

      Name

      Enter a name for the configuration to reference it.

      <Configuration_Name>

      Access Key

      Alphanumeric text string that uniquely identifies the user who owns the account.

      ${config.accesskey}

      Secret Key

      Key that plays the role of a password.

      ${config.secretkey}

      Region Endpoint

      Set the queue reqion

      User can select the Region by using dropdown.

      Topic Arn

      Topic ARN to test the connectivity.

      ${sns.topic.arn}

    2. Your configuration should look like this:

      sns use case config

    3. The corresponding XML configuration should be as follows:

      <sns:config name="Amazon_SNS_configuration" doc:name="Amazon SNS configuration" doc:id="f85b91a1-661e-4bf4-80c7-997107acda08">
      <sns:basic-connection accessKey="${config.accessKey}" secretKey="${config.secretKey}" testTopicArn="${sns.topic.arn}" />
      </sns:config>
  6. Click Test Connection to confirm that Mule can connect with the SNS instance. If the connection is successful, click OK to save the configurations. Otherwise, review or correct any incorrect parameters, then test again.

  7. Back in the properties editor of the Amazon SNS connector, configure the remaining parameters:

    Parameter Value

    Basic Settings

    Display Name

    Publish message to topic (or any other name you prefer).

    Connector Configuration

    Amazon_SNS_Configuration (the reference name to the global element you have created).

    Operation

    publish

    General

    Define attributes

    Select to define the Publish attributes

    Topic Arn

    ${sns.topic.arn} (or any other topic arn).

    Message

    Hello World!

    Subject

    Testing publish to queue.

    publish message connector props

  8. Check that your XML looks like this:

    <sns:publish config-ref="Amazon_SNS_configuration" doc:name="Publish" doc:id="b0c5158d-6f3f-44be-9153-3c6d9dd870f2" >
        <sns:publish-details message="Hello World!" subject="Testing publish to queue" topicArn="${sns.topic.arn}"/>
    </sns:publish>
  9. Add a Logger scope after the Amazon SNS connector to print the data that is being processed by the Publish operation in the Mule Console. Configure the Logger according to the table below.

    Parameter Value

    Display Name

    Logger (or any other name you prefer)

    Message

    Message ID: #[payload]

    Level

    INFO

    sns logger

  10. Now let’s add another flow to receive the message published by SNS.

  11. Drag a Flow scope onto the palette.

  12. Drag the Amazon SQS Connector > ReceiveMessages in the left side of the new flow and configure it according to the steps below:

  13. Click the plus sign next to the Connector Configuration field to add a new Amazon SQS Global Element.

    1. Configure the global element according to the table below:

      Parameter Description Value

      Name

      Enter a name for the configuration to reference it.

      <Configuration_Name>

      Access Key

      Alphanumeric text string that uniquely identifies the user who owns the account.

      ${sqs.accesskey}

      Secret Key

      Key that plays the role of a password.

      ${sqs.secretkey}

      Queue Name

      Set the name of the queue.

      ${sqs.queueName}

      Queue URL

      Set the queue URL

      ${sqs.queueUrl}

      Region Endpoint

      Set the queue reqion

      user can select the Region by using dropdown.

    2. Your configuration should look like this (Queue URL can be skipped if Queue Name is specified):

      sns-sqs-config

    3. The corresponding XML configuration should be as follows:

      <sqs:config name="Amazon_SQS_Configuration" accessKey="${sqs.accesskey}" secretKey="${sqs.secretkey}" doc:name="Amazon SQS: Configuration" defaultQueueName="${sqs.queueName}" url="${sqs.queueUrl}"/>

      Make sure SQS-Queue that you mentioned in configuration should be subscribed to SNS-Topic.

  14. Click Test Connection to confirm that Mule can connect with the SQS instance. If the connection is successful, click OK to save the configurations. Otherwise, review or correct any incorrect parameters, then test again.

  15. Back in the properties editor of the Amazon SQS connector, configure the remaining parameters:

    Parameter Value

    Basic Settings

    Display Name

    Amazon SQS (Streaming) (or any other name you prefer).

    Connector Configuration

    Amazon_SQS_Configuration (the reference name to the global element you have created).

    Other fields in the group General

    Default values

  16. Check that your XML looks like:

    <sqs:receivemessages config-ref="Amazon_SQS_Configuration" doc:name="Receivemessages" doc:id="ID_VALUE" />
  17. Add a Logger scope after the Amazon SQS connector to print the data that is being passed by the Receive operation in the Mule Console. Configure the Logger according to the table below.

    Parameter Value

    Display Name

    Display Message (or any other name you prefer)

    Message

    Received Message : #[payload]

    Level

    INFO

  18. Save and run the project as a Mule Application. Right-click the project in Package Explorer. Run As > Mule Application.

  19. Open a web browser and check the response after entering the URL http://localhost:8081/. The logger displays the published message ID on the browser and the received message on the mule console.

Demo Mule Application XML Code

Paste this code into your XML Editor to quickly load the flow for this example use case into your Mule application.


          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:sqs="http://www.mulesoft.org/schema/mule/sqs" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:sns="http://www.mulesoft.org/schema/mule/sns" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/sqs http://www.mulesoft.org/schema/mule/sqs/current/mule-sqs.xsd
http://www.mulesoft.org/schema/mule/sns http://www.mulesoft.org/schema/mule/sns/current/mule-sns.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="ID_VALUE" >
    <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    <sqs:config name="Amazon_SQS_Configuration" doc:name="Amazon SQS Configuration" doc:id="ID_VALUE" >
            <sqs:basic-connection accessKey="${sqs.accessKey}" secretKey="${sqs.secretKey}" defaultQueueName="${sqs.queueName}"/>
    </sqs:config>
            <sns:config name="Amazon_SNS_configuration" doc:name="Amazon SNS configuration" doc:id="ID_VALUE >
                    <sns:basic-connection accessKey="${sns.accesskey}" secretKey="${sns.secretkey}" />
            </sns:config>
   <flow name="publish_message_to_topic" >
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <sns:publish config-ref="Amazon_SNS_Configuration" doc:name="Publish message to topic">
          <sns:publish topicArn="${sns.topic.arn}"  message="Hello world!" subject="Testing publish to queue"/>
          </sns:publish>
          <logger message="#[payload]" level="INFO" doc:name="Logger" />
   </flow>
   <flow name="recieve_message_from_queue">
        <sqs:receivemessages config-ref="Amazon_SQS_Configuration" doc:name="Receivemessages" doc:id="ID_VALUE" />
                   <logger level="INFO" doc:name="Logger" doc:id="ID_VALUE" message="#[payload]"/>
   </flow>
</mule>

See Also