我可以在我们以外的地方发送一条关于云观察警报的短信吗? [英] Can I send an SMS message for a cloudwatch Alarm outside of us-east?

查看:111
本文介绍了我可以在我们以外的地方发送一条关于云观察警报的短信吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来AWS不提供SMS作为美国东部以外的SNS主题订阅者的协议。我想连接我的CloudWatch警报并在有问题但无法将其发送到短信时收到短信。

It looks like AWS does not provide SMS as a protocol for SNS topic subscribers outside of US East. I wanted to hook up my CloudWatch alarms and receive text messages when something breaks but cannot send them to SMS.

有什么方法可以做到这一点吗?

Is there any way to do this?

推荐答案

是!

经过一番挖掘,我能够让这个工作。这比仅仅选择一个主题或输入一个警报要复杂得多,但效果很好!

After some digging I was able to get this to work. It's a little more complicated than just selecting a topic or inputing an alarm but it works great!

解决方案的关键是使用AWS的lambda函数!

数据流如此:


> 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或者设置lambda,你可以浏览已经存在的例子。但是,我将分享我为执行此操作而编写的lambda代码。您可以使用发布消息到主题功能来测试它。

I won't talk too much about setting up SNS or setting up lambda, you can go through the examples that already exist for that. However, I will share the lambda code that I wrote to perform this. You can test it by using the publish message to topic functionality.

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's 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之外,这不需要任何其他库已经在lambda函数中。如果您不想要它,可以随意省略控制台日志记录,但它对于调试很有用。

This doesn't require any additional libraries other than the aws-sdk that is already in the lambda function. You can feel free to leave out the console logging if you don't want it but it was useful for debugging.

如果您使用测试功能,那么它可能会失败。我相信这是因为回调永远不会发生。

请记住lambda不应该在us-east-1上,因为它将在任何区域触发你正在使用。

Remember that the lambda should not be on us-east-1 since it will be triggered on whatever region you are using.

教程链接:

SNS发布api:

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

如何在lambda中使用node.js:

How to node.js in lambda:

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

使用其他aws函数的一个非常棒的教程:

A really great tutorial on using other aws functions:

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

这篇关于我可以在我们以外的地方发送一条关于云观察警报的短信吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆