发送短信给cloud-watch在us-east以外的地区发出警报? [英] Send an SMS for a cloudwatch Alarm outside of us-east?

查看:110
本文介绍了发送短信给cloud-watch在us-east以外的地区发出警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AWS似乎没有为美国东部以外的SNS主题订户提供SMS作为协议。我想挂接CloudWatch警报并在出现故障但无法将其发送到SMS时接收文本消息。

解决方案

是的!



经过一番挖掘,我终于能够使用它了。

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



数据流如下:

 
>警报触发了
>->将通知推送到SNS
>-> SNS发布到lambda
>-> lambda重新发布到us-east-1
上的SNS-->订户收到消息

只是服务:

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

我不会过多谈论设置SNS或设置lambda,您可以查看已经存在的示例。但是,我将分享为执行此操作而编写的lambda代码。您可以通过使用主题主题的发布消息来对其进行测试。

  console.log(加载功能); 

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

var usEastSns = new Aws.SNS({
accessKeyId:在这里输入您的访问密钥,
secretAccessKey:在这里输入您的秘密密钥,
地区: us-east-1,
记录器:控制台
});

exports.snsProxy =函数(事件,上下文){
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); //发生错误
} else {
//重要的是,我们必须成功完成回调
// //否则,如果不发送
console.log(data);
context.succeed(message );
}
})
};

函数buildNewMessage(message){
//由于某些原因,在
//事件中发送的消息不包含与库$ b $相同的接口b //期望因此需要创建
return {
TargetArn:在这里输入您的名字,
消息:message.Message,
主题:message。主题
MessageAttributes:collectAttr(message.MessageAttributes)
};
}

函数collectAttr(attrs){
var newAttrs = {};
表示(attrs中的var attr){
newAttrs [attr] = {
数据类型:attrs [attr] .Type,
StringValue:attrs [attr] .Value
}
}
返回newAttrs;
}

除了aws-sdk之外,不需要任何其他库已经在lambda函数中。如果您不想要控制台日志,则可以随意删除,但它对于调试很有用。



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



请记住,lambda不应位于us-east-1上,因为它将在任何区域触发



链接到教程:



SNS发布api:



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



如何在lambda中使用node.js:



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



关于使用其他aws函数的非常不错的教程:



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


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.

解决方案

YES!

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!

The key to the solution was using AWS's lambda functions!

The flow of data is such:

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

Just the services:

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

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;
}

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.

If you use there test functionality, it will likely fail. I believe this is because the callback never happens.

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

Links to tutorials:

SNS Publish api:

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

How to node.js in lambda:

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

A really great tutorial on using other aws functions:

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

这篇关于发送短信给cloud-watch在us-east以外的地区发出警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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