Tags on Cloudwatch alarm for opsgenie

0

Hi, Is there any way to send some tags to opsgenie along with alarm description, summary in cloudwatch alarms ? We want to send app name in tags so that we can route alerts to different teams using this tag in opsgenie.

asked 2 months ago107 views
2 Answers
0

While CloudWatch alarms support tagging, there isn't a direct way to send tags from CloudWatch alarms to OpsGenie along with the alarm description and summary. However, you can achieve your goal of routing alerts to different teams in OpsGenie by using a combination of CloudWatch alarm features and OpsGenie's integration capabilities.

Here's an approach you can consider:

  1. Use CloudWatch alarm tags to categorize your alarms. You can add tags like "AppName" with the value of your application name to each alarm.

  2. When setting up your CloudWatch alarm, include the relevant tag information (like the app name) in the alarm description or in a custom message that you configure for the alarm.

  3. Set up an SNS topic that will receive the CloudWatch alarm notifications.

  4. Configure the OpsGenie integration with AWS SNS. This integration can parse the incoming alarm information.

  5. In OpsGenie, set up alert routing rules based on the content of the alarm description or message. Since you've included the app name in the alarm description or message, you can use this information to route alerts to the appropriate teams.

This approach allows you to effectively use the tagging concept to categorize your alarms in CloudWatch and then leverage that information for routing in OpsGenie, even though the tags themselves are not directly sent to OpsGenie.

Remember that you can assign up to 50 tags to a CloudWatch alarm, which gives you flexibility in how you categorize and organize your alarms. You can manage these tags using the CloudWatch console, AWS CLI, API, or even AWS CloudFormation.
Sources
Alarms and tagging - Amazon CloudWatch
Tagging your Amazon CloudWatch resources - Amazon CloudWatch
Amazon CloudWatch now supports tagging alarms with AWS CloudFormation

profile picture
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago
0

Hello.

To send custom tags (such as the app name) to Opsgenie along with the alarm description and summary from CloudWatch alarms, you can follow a solution using AWS SNS, AWS Lambda, and the Opsgenie API. please follow below steps.

Create a CloudWatch Alarm

  • Go to the CloudWatch console and create an alarm for the metric you want to monitor.
  • Under the Actions section, select Send notification to an SNS topic.

Create an SNS Topic

  • In the SNS Console, create a new SNS topic. This will receive the alarm notification and trigger a Lambda function.
  • Subscribe your Lambda function to this SNS topic later.

Create a Lambda Function

  • This Lambda function will capture the SNS notification, add custom tags (e.g., app name), and send the alert to Opsgenie.

Example Lambda function code to send an alert to Opsgenie

import json
import urllib3

OPSGENIE_API_URL = "https://api.opsgenie.com/v2/alerts"
OPSGENIE_API_KEY = "your_opsgenie_api_key"
http = urllib3.PoolManager()

def lambda_handler(event, context):
    sns_message = event['Records'][0]['Sns']['Message']
    alarm_details = json.loads(sns_message)

    # Add custom tags like the app name
    tags = ["app:MyApp", "environment:production"]

    # Construct Opsgenie alert payload
    opsgenie_payload = {
        "message": alarm_details.get('AlarmName', 'CloudWatch Alarm'),
        "description": alarm_details.get('AlarmDescription', 'No description provided'),
        "tags": tags,
        "priority": "P3",  # Adjust based on severity
        "details": {
            "AWSAccount": alarm_details.get('AWSAccountId'),
            "Region": alarm_details.get('Region'),
            "State": alarm_details.get('NewStateValue'),
            "Reason": alarm_details.get('NewStateReason'),
        }
    }

    # Send alert to Opsgenie
    response = http.request(
        'POST',
        OPSGENIE_API_URL,
        body=json.dumps(opsgenie_payload),
        headers={
            'Content-Type': 'application/json',
            'Authorization': f'GenieKey {OPSGENIE_API_KEY}'
        }
    )

    return {
        'statusCode': response.status,
        'body': response.data.decode('utf-8')
    }

Subscribe Lambda Function to SNS Topic

  • In the SNS console, subscribe your Lambda function to the SNS topic.
  • This ensures that when the CloudWatch alarm triggers, the SNS topic sends the event to the Lambda function, which will forward the alert to Opsgenie.

Opsgenie Configuration

  • In Opsgenie, create an API Integration if you haven't already.
  • Obtain the Opsgenie API key and use it in your Lambda function to authenticate API requests.
  • Configure routing rules in Opsgenie based on the tags you send (e.g., app name) to route the alerts to the appropriate team.

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html https://community.atlassian.com/t5/Opsgenie-questions/How-do-I-set-Opsgenie-alert-tags-in-an-AWS-CloudWatch/qaq-p/2492739

EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions