Can I send an SMS to alert me that the cloud is intercepted outside of us - to the east?

AWS does not appear to provide SMS as a protocol for subscribers of SNS topics outside the United States. I wanted to connect my CloudWatch alerts and receive text messages when something breaks, but cannot send them in SMS.

Is there any way to do this?

+3
source share
2 answers

YES!

After some digging, I was able to make it work. It's a little trickier than just picking a theme or entering an alarm, but it works great!

The key to the solution was to use AWS lambda functions!

The data stream is as follows:

> Alarm triggered
> -> Push notification to SNS
>  -> SNS posts to lambda
>  -> lambda reposts to SNS on us-east-1
>  -> subscriber receives message

:

> CloudWatch Alarm   (us-west-2) 
>  -> SNS            (us-west-2) 
>  -> Lambda         (us-west-2)
>  -> SNS            (us-east-1)
>  -> SMS subscriber (us-east-1)

SNS , , . , -, , . , .

console.log('Loading function');

var Aws = require('aws-sdk');

var usEastSns = new Aws.SNS({
    accessKeyId: "ENTER YOUR ACCESS KEY HERE",
    secretAccessKey: "ENTER YOUR SECRET KEY HERE",
    region: "us-east-1",
    logger: console
});

exports.snsProxy = function(event, context) {
    var message = event.Records[0].Sns;
    console.log("received message: ", message);
    var newMessage = buildNewMessage(message);
    console.log("publishing: ", newMessage);
    usEastSns.publish(newMessage, function(err, data) {
        if (err) {
            console.log(err, err.stack); // an error occurred
        } else {
            // It important that we succeed in the callback
            // otherwise it seems to succeed without sending
            console.log(data);           
            context.succeed(message);    
        }
    })
};

function buildNewMessage(message) {
    // For some reason the message that gets sent in the event
    // does not contain the same interface as what the library
    // expects so it needs to be created
    return {
        TargetArn: "ENTER YOUR ARN IN US EAST HERE",
        Message: message.Message,
        Subject: message.Subject,
        MessageAttributes: collectAttr(message.MessageAttributes)
    };
}

function collectAttr(attrs) {
    var newAttrs = {};
    for (var attr in attrs) {
        newAttrs[attr] ={
            DataType: attrs[attr].Type,
            StringValue: attrs[attr].Value
        }
    }
    return newAttrs;
}

, aws-sdk, -. , , .

, , . , , .

, - -1, , .

:

SNS api:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#publish-property

node.js lambda:

http://docs.aws.amazon.com/lambda/latest/dg/programming-model.html

aws:

http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html

+9

/ , Node 8.10:

const AWS = require('aws-sdk');
const TARGET_ARN = process.env.TARGET_ARN;
var sns = new AWS.SNS({
    region: "us-east-1"
});

exports.handler = async (event) => {
    const message = event.Records[0].Sns;
    return await forwardMessage(message);
};

function forwardMessage(message) {
    const messageToForward = {
        TargetArn: TARGET_ARN,
        Message: message.Message,
        Subject: message.Subject
    };

    return sns.publish(messageToForward).promise();
}
0

Source: https://habr.com/ru/post/1690950/


All Articles