# Amazon SNS Outbound via AWS Lambda

[Amazon Simple Notification Service](https://aws.amazon.com/sns/) (Amazon SNS) is a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication.

The A2A pub/sub functionality provides topics for high-throughput, push-based, many-to-many messaging between distributed systems, microservices, and event-driven serverless applications. Using Amazon SNS topics, your publisher systems can fanout messages to a large number of subscriber systems including Amazon SQS queues, AWS Lambda functions and HTTPS endpoints, for parallel processing, and Amazon Kinesis Data Firehose. The A2P functionality enables you to send messages to users at scale via SMS, mobile push, and email.

## In AWS Console: Create an AWS Lambda function for Amazon SNS <a href="#in-aws-console" id="in-aws-console"></a>

{% hint style="info" %}
To set up the integration, you must have an Amazon SNS topic and the arn string for it.
{% endhint %}

1. Go to the AWS Console and then to **Amazon Lambda**, then click on the **Functions** link and on the **Create function** button\*\*.\*\*

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWuwLborDTMQP3EABLL%2F-MWxC9yIb4TF2JqzHdXd%2FFunctions_-_Lambda.png?alt=media\&token=82d69d38-560b-4753-b4cd-68df34ae91b9)

2. On the next page, choose **Author from scratch**, name the function e.g. myIncidentHandler and scroll down

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWuwLborDTMQP3EABLL%2F-MWxDKWRXjaqYnjBBTXb%2FLambda.png?alt=media\&token=840aa7e5-196f-4e87-9064-745ed85f025b)

3. **In the Change** default execution role section, choose **Create a new role from AWS policy template**, name the new role e.g. myIncidentHandlerRole, in the **Policy templates** section, choose **Amazon SNS publish policy** and click on the **Create function** button

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWuwLborDTMQP3EABLL%2F-MWxDaQUNbX_q2ShDAaq%2FLambda.png?alt=media\&token=9207484e-e175-4a7c-a6c2-f2c71753c985)

4. On the next page, change Code source as follows:

```javascript
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: process.env.AWS_REGION});

const topicArn = "arn:aws:sns:xxxxxxxxx:xxxxxxxxxx:MyTopic";

exports.handler = async (event) => {
    try {
        const incidentEvent = JSON.parse(event.body);

        // Create publish parameters
        var params = {
          Subject: incidentEvent.summary,
          Message: incidentEvent.details ? incidentEvent.details : incidentEvent.summary,
          TopicArn: topicArn,
          MessageAttributes: {
            "event": { StringValue: incidentEvent.eventType, DataType: "String" },
            "status": { StringValue: incidentEvent.status, DataType: "String" },
          },
        };

        // Create promise and SNS service object
        var publishTextPromise = new AWS.SNS({apiVersion: '2010-03-31'}).publish(params).promise();

        // Handle promise's fulfilled/rejected states
        const data = await publishTextPromise;

        const response = {
            statusCode: 200,
            body: JSON.stringify({message: `Message ${data.MessageId} sent to the topic ${params.TopicArn}`}),
        };
        return response;
    } catch(err) {
        const response = {
            statusCode: 500,
            body: JSON.stringify({message: err ? err.toString() : "An error occurred"}),
        };
        return response;
    }
};
```

5. Change the **topicArn** variable to the Amazon SNS topic arn and click on the **Deploy** button

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWxf90QXTAU_V8GMbKD%2F-MWxo_PVdIrgG7jRrVLl%2FmyIncidentHandler_-_Lambda.png?alt=media\&token=dd05239a-b99e-4e51-9ea1-803a505cb1d1)

6. In the **Function overview** section, click on the **Add trigger** button

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWxLPIO31J5HRW8rnUh%2F-MWxMIXxw9uv_yZ9SpGI%2FmyIncidentHandler_-_Lambda.png?alt=media\&token=07a3a005-cb99-4ef0-95f9-348a286bfbd5)

7. On the next page, choose **API Gateway** as trigger type, in the **API** section choose **Create an API**, in the **API type** section choose **REST API**, in the **Security** section choose **API key** and click on the **Add** button

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWxLPIO31J5HRW8rnUh%2F-MWxOcvOPi0_xtNWDHbb%2FLambda.png?alt=media\&token=dcd7da27-97c5-4151-8e48-96c2ce2403fd)

8. On the next page an **API endpoint** and an **API key** are generated. You will need this URL and key below when setting up the AWS Lambda connector in ilert.

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWxLPIO31J5HRW8rnUh%2F-MWxP_jhDciPe9K1E8sa%2FmyIncidentHandler_-_Lambda.png?alt=media\&token=28b1c716-b95f-4442-a691-fd8535496910)

## In ilert: Create an AWS Lambda Connector and link it to the alert source <a href="#in-ilert" id="in-ilert"></a>

{% hint style="info" %}
**Admin permission required**

To set up the integration, you must have admin rights in ilert.
{% endhint %}

1. **\*\*Click the gear icon and then click on the** Connectors\*\* link

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MVvPAJj6ASn-KnO5f4S%2F-MVvVEyvN3fXUo2aqmBu%2FScreenshot_16_03_21__15_46.png?alt=media\&token=7f6d05c8-befa-4aa7-af0e-51ab7d177b2c)

2. Click the **Add Connector** button

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MVvPAJj6ASn-KnO5f4S%2F-MVvVeIIkyW0rkE7P-0L%2FScreenshot_16_03_21__15_48.png?alt=media\&token=bf282036-0b4a-432d-8d75-51cf9ac4ebcc)

3. On the next page, choose **AWS Lambda** as type, name the connector, paste the **API key** that you generated in AWS Lambda and click on the **Save** button

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWxPbku0tZvGrqMPRSs%2F-MWxdJBGBQT5Jrr6bIEp%2FiLert.png?alt=media\&token=9145d65b-ea60-43f7-8a8f-7f30eff1622e)

4. Go to the alert sources tab and open the alert source whose alerts you want to publish to Amazon SNS. Click on the **Alert actions** tab and then on the **Add new alert action** button

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWxd_Red0pjkYkaKYQw%2F-MWxePHw4iI_U9IdB7Li%2FiLert.png?alt=media\&token=466edecc-7ddc-4ea8-98b9-b7382696f8f8)

5. On the next page choose **AWS Lambda** as the type, choose the connector created in step 3, name it, choose **Trigger mode,** paste the **API URL** that you generated in AWS Lambda and click on the **Save** button.

![](https://3394882078-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M76ygPnS4HUcFSX8ulm%2F-MWxd_Red0pjkYkaKYQw%2F-MWxezs38a926nwOZkGZ%2FiLert.png?alt=media\&token=f36bc0b2-2509-4738-9ca6-594d5e383a05)

6. Finished! Now an Amazon SNS notification will be created for each alert in automatic trigger mode or via manual alert action.
