Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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." 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?

like image 916
Mark Probst Avatar asked Jul 06 '18 05:07

Mark Probst


2 Answers

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:

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. Lets say your service is in a region where SMS is not supported, I'll use eu-central-1 as an example.

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.
  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.
  3. [eu-central-1] Create an SNS topic in the region where your service is. For the SNS topic configure subscription with AWS Lambda protocol and point it to lambda from step 2.
  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.
like image 92
Dejan Peretin Avatar answered Sep 19 '22 05:09

Dejan Peretin


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:

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'
like image 28
Thomas Avatar answered Sep 22 '22 05:09

Thomas