简体   繁体   English

CloudWatch向不同地区的SNS发出警报

[英]CloudWatch alarm to SNS in different region

I'm trying to notify an SNS topic from a CloudWatch alarm that's in a different region. 我正在尝试从位于不同区域的CloudWatch警报通知SNS主题。 The reason is that I want SMS alerting, which isn't available in the region where my services are. 原因是我想要短信提醒,这在我的服务所在的地区是不可用的。 If I enter the ARN of the subscription and save the changes in the console, I get "There was an error saving the alarm. Please try again." 如果我输入订阅的ARN并在控制台中保存更改,我会收到“保存警报时出错。请再试一次。” Trying again does not help. 再试一次没有用。 Using a topic in the local region does work, but that's not what I need. 在本地区域使用主题确实有效,但这不是我需要的。

Is there a way to notify a topic in a different region? 有没有办法通知不同地区的主题? If not, is there another easy way I can achieve my goal? 如果没有,还有另一种简单的方法可以实现我的目标吗?

Didn't find any docs that explicitly say this can't be done but tried to set an SNS from us-east-1 as an action of an alarm in eu-west-1 using the CLI and I got this: 没有找到任何明确表示无法完成的文档,但尝试使用CLI在us-east-1中设置SNS作为eu-west-1中的警报动作,我得到了:

An error occurred (ValidationError) when calling the PutMetricAlarm operation: Invalid region us-east-1 specified. Only eu-west-1 is supported.

So I'll assume it's not supported. 所以我认为它不受支持。

To get the functionality you need you can use AWS Lambda. 要获得所需的功能,您可以使用AWS Lambda。 Lets say your service is in a region where SMS is not supported, I'll use eu-central-1 as an example. 假设您的服务位于不支持SMS的区域,我将使用eu-central-1作为示例。

Setup would go like this: 安装程序将如下所示:

  1. [ us-east-1 ] Create your SNS topic that can send SMS messages, in the region where SMS is supported. [ us-east-1 ]在支持SMS的区域中创建可以发送SMS消息的SNS主题。
  2. [ eu-central-1 Create a lambda function that sends messages to the SNS topic from step 1 in the region where your service is. [ eu-central-1创建一个lambda函数,用于从服务所在区域的步骤1向SNS主题发送消息。
  3. [ eu-central-1 ] Create an SNS topic in the region where your service is. [ eu-central-1 ]在您的服务所在的地区创建一个SNS主题。 For the SNS topic configure subscription with AWS Lambda protocol and point it to lambda from step 2. 对于SNS主题,请使用AWS Lambda协议配置订阅,并将其从步骤2指向lambda。
  4. [ eu-central-1 ] Create your alarm in the region where your service is and put the SNS topic from step 3 as an action. [ eu-central-1 ]在您的服务所在区域创建警报,并将步骤3中的SNS主题作为操作。

To add to @Tartaglia's answer, here's the source of such a lambda function using Python 3, cobbled together from various sources because I don't have time to do it properly: 要添加到@Tartaglia的答案,这里是使用Python 3的这种lambda函数的来源,从各种来源拼凑而成,因为我没有时间正确地执行它:

import json
import logging

import boto3


logger = logging.getLogger()
logger.setLevel(logging.INFO)


session = boto3.Session(region_name='eu-west-1') # EU (Ireland)
sns_client = session.client('sns')


def lambda_handler(event, context):
    logger.info('Received event: %s', event)

    for record in event['Records']:
        sns_message = record['Sns']

        response = sns_client.publish(
            TopicArn='YOUR TOPIC ARN HERE',
            Subject=sns_message.get('Subject', None),
            Message=sns_message.get('Message', None))

        logger.info('Publish response: %s', response)

    return 'OK'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM