I tried notifying CloudWatch alarms to Teams and Chatwork!

table of contents
* Ramen Shin (Ibaraki City, Osaka Prefecture)
Hello!
I'm Hide, the Ramen King from Beyond Inc.'s Osaka office.
This is my fourth post.
In this article, I will introduce a method for notifying CloudWatch alarms to Teams and Chatwork, which I recently implemented in a project
In my previous blog post, I also wrote about how to study for the AWS Certified SAA (Solutions Architect Associate) certification, so if you're interested, please take a look!
Configuration diagram
The configuration diagram is shown above
CloudWatch retrieves EC2 metrics and notifies SNS when an alarm occurs. SNS then sends various values received from CloudWatch to Lambda
Lambda will then format the values and send them to each communication tool.
It may seem difficult because it uses various AWS resources, but it's easy to implement, so let's work together!
Construction procedure
① Create Lambda
①-①: Click Lambda > Functions > Create Function
①-②: Specify the following information and click [Create Function]
● Function name: Please specify a name of your choice
● Runtime: Please specify Python 3.
①-③: Paste the code for each communication tool
*If you don't want to write the Webhook URL, authentication token, etc. directly in your code, set environment variables
● Teams code
#!/usr/bin/python3.8 import boto3 import json from urllib.request import Request, urlopen #Assign WebhookURL HOOK_URL = "https://xxxxxxxxxxxxxxxx" def lambda_handler(event, context): #Production alarm #message = json.loads(event['Records'][0]['Sns']['Message']) #subject = event['Records'][0]['Sns']['Subject'] #Alarm test message = {"AlarmName":"test-CPUUtilization","AWSAccountId":"xxxxxxxxxxx","NewStateValue":"ALARM","NewStateReason":"Threshold Crossed: 1 out of the last 1 datapoints [1.8083480560463125 (xx/xx/xx xx:xx:xx)] was less than or equal to the threshold (80.0) (minimum 1 datapoint for OK -> ALARM transition).","StateChangeTime":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","Region":"Asia Pacific (Tokyo)"} subject = "ALARM: test-CPUUtilization in Asia Pacific (Tokyo)" #Alarm information alarm_name = message["AlarmName"] new_state = message["NewStateValue"] reason = message["NewStateReason"] alarm_time = message["StateChangeTime"] account_id = message["AWSAccountId"] region = message["Region"] alert_url = "https://ap-northeast-1.console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#alarmsV2:alarm/%s" %alarm_name alert_msg = { 'title': "%s" %subject, 'text': '<br> Alarm name: %s' %alarm_name + '<br> Status: %s' %new_state + '<br> Alarm reason: %s' %reason + '<br><br> Alarm occurrence time (UTC): %s' %alarm_time + '<br> Account ID: %s' %account_id + '<br> Region: %s' %region + '<br><br> Alarm URL:<br> %s' %alert_url } #Convert dictionary to string and generate HTTP request req = Request(HOOK_URL, json.dumps(alert_msg).encode('utf-8')) #Send urlopen(req)
● Chatwork code
#!/usr/bin/python3.8 import boto3 import json import urllib from urllib.request import Request, urlopen #Specify room ID room_id = 'xxxxxxxxxxx' #Specify token tokun = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx' chatwork_URL = "https://api.chatwork.com/v2/rooms/%s/messages" %room_id def lambda_handler(event, context): #Production alarm #message = json.loads(event['Records'][0]['Sns']['Message']) #subject = event['Records'][0]['Sns']['Subject'] #Alarm test message = {"AlarmName":"test-CPUUtilization","AWSAccountId":"xxxxxxxxxxx","NewStateValue":"ALARM","NewStateReason":"Threshold Crossed: 1 out of the last 1 datapoints [1.8083480560463125 (xx/xx/xx xx:xx:xx)] was less than or equal to the threshold (80.0) (minimum 1 datapoint for OK -> ALARM transition).","StateChangeTime":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","Region":"Asia Pacific (Tokyo)"} subject = "ALARM: test-CPUUtilization in Asia Pacific (Tokyo)" #Alarm information alarm_name = message["AlarmName"] new_state = message["NewStateValue"] reason = message["NewStateReason"] alarm_time = message["StateChangeTime"] account_id = message["AWSAccountId"] region = message["Region"] alert_url = "https://ap-northeast-1.console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#alarmsV2:alarm/%s" %alarm_name alert_msg = { 'body': '[info][title]%s[/title]'%subject+ 'Alarm Name: %s'%alarm_name +'\n'+ 'Status: %s'%new_state +'\n'+ 'Alarm Reason: %s'%reason +'\n'+ 'Alarm Occurrence Time (UTC): %s'%alarm_time +'\n'+ 'Account ID: %s'%account_id +'\n'+ 'Region: %s'%region +'\n\n'+ 'Alarm URL: %s'%alert_url + '[/info]' } headers = { 'X-ChatWorkToken': '%s' %tokun } #Generate a query string data = urllib.parse.urlencode(alert_msg) #Convert character code data = data.encode('utf-8') #Generate an HTTP request req = Request(chatwork_URL, data,headers) #Send urlopen(req)
*Note: Message notation for each communication tool
● Chatwork
https://developer.chatwork.com/docs/message-notation
● AWS
https://aws.amazon.com/jp/premiumsupport/knowledge-center/sns-lambda-webhooks-chime-slack-teams
①-④: Click [deploy]
①-⑤: Click 【Test】
①-⑥: Specify the following information and click [Save]
● Event name: Please specify a name of your choice
● Template - Options: sns-notification
①-⑦: Click [Test]
①-⑧: Check whether the test alarm is notified
● Teams

● Chatwork
①-⑨: Edit the variables for production alarm notifications as follows:
#Production alarm message = json.loads(event['Records'][0]['Sns']['Message']) subject = event['Records'][0]['Sns']['Subject'] #Alarm test #message = {"AlarmName":"test-CPUUtilization","AlarmDescription":"null","AWSAccountId":"xxxxxxxxxxx","AlarmConfigurationUpdatedTimestamp":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","NewStateValue":"ALARM","NewStateReason":"Threshold Crossed: 1 out of the last 1 datapoints [1.8083480560463125 (xx/xx/xx xx:xx:xx)] was less than or equal to the threshold (80.0) (minimum 1 datapoint for OK -> ALARM transition).","StateChangeTime":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","Region":"Asia Pacific (Tokyo)","AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:xxxx:alarm:test-CPUUtilization","OldStateValue":"OK","OKActions":[],"AlarmActions":["arn:aws:s ns:ap-northeast-1:xxxxxxxx:test"],"InsufficientDataActions":[],"Trigger":{"MetricName":"CPUUtilization","Namespace":"AWS/EC2","StatisticType":"Statist ic","Statistic":"AVERAGE","Unit":"null","Dimensions":[{"value":"i-07876365e6af5774f","name":"InstanceId"}],"Period":300,"EvaluationPeriods":1,"Datapo intsToAlarm":1,"ComparisonOperator":"LessThanOrEqualToThreshold","Threshold":80.0,"TreatMissingData":"missing","EvaluateLowSampleCountPercentile":""}} #subject = "ALARM: test-CPUUtilization in Asia Pacific (Tokyo)"
② Create a social networking site
②-①: Click SNS > Topics > Create Topic
②-②: Specify the following information and click [Create Topic]
● Type: Standard
● Name: Please enter your preferred name
②-③: Click [Create Subscription]
②-④: Specify the following information and click [Create Subscription]
● Protocol: Lambda
● Endpoint: *See below

* Lambda ARN information location
The function ARN is located on the far right of the created function > function summary, so copy it from there
②-⑤: Check the status

③ Create CloudWatch
*We will proceed under the assumption that EC2 has been created
③-①: Cloudwatch > Alarms > Click [Create Alarm]
③-②: Select your preferred metrics
③-③: Select the alarm condition
*Please set the alarm to occur

③-④: Notification settings
③-⑤: Specify the alarm name
③-⑥: Click [Create alarm]
④ Alarm notification test
④-①: Check if an alarm is in effect
④-②: Check if the alarm has been notified
● Teams
● ChatWork
*Note:
If you want to check all of the CloudWatch alarm information, you can view it by editing the contents of the alert_msg variable as shown below.
■ Teams
alert_msg ={ "text":"%s" %message }
■ Chatwork
alert_msg = { 'body': '%s' %message }
[Output result]
{"AlarmName":"test-CPUUtilization","AlarmDescription":"null","AWSAccountId":"xxxxxxxxxxx","AlarmConfigurationUpdatedTimestamp":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","NewStateValue":"ALARM","NewStateReason":"Threshold Crossed: 1 out of the last 1 datapoints [1.8083480560463125 (xx/xx/xx xx:xx:xx)] was less than or equal to the threshold (80.0) (minimum 1 datapoint for OK -> ALARM transition).","StateChangeTime":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","Region":"Asia Pacific (Tokyo)","AlarmArn":"arn:aws:cloudwatch:ap-northeast-1:xxxx:alarm:test-CPUUtilization","OldStateValue":"OK","OKActions":[],"AlarmActions":["arn:aws: sns:ap-northeast-1:xxxxxxxx:test"],"InsufficientDataActions":[],"Trigger":{"MetricName":"CPUUtilization","Namespace":"AWS/EC2","StatisticType":"Stati stic","Statistic":"AVERAGE","Unit":"null","Dimensions":[{"value":"i-xxxxxxxxxxxxxx","name":"InstanceId"}],"Period":300,"EvaluationPeriods":1,"Datapoi ntsToAlarm":1,"ComparisonOperator":"LessThanOrEqualToThreshold","Threshold":80.0,"TreatMissingData":"missing","EvaluateLowSampleCountPercentile":""}}
summary
thank you for your hard work!
This time, we introduced how to notify each communication tool of alarm content!
Slack can easily notify alarms by using AWS Chatbot, but CloudWatch and SNS do not have the functionality to send to Teams and Chatwork, so this is how you can notify alarms, although it is difficult
You can also edit the code to send messages according to the processing. Make use of this when sending alarm notifications!
4
















