[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

I tried notifying CloudWatch alarms to Teams/Chatwork!

*Ramen Shin (Ibaraki City, Osaka Prefecture)

Hello!
My name is Hide, the ramen king of Beyond Osaka Office.
This is my fourth post.

In this article, I will introduce a method to notify Teams/Chatwork of CloudWatch alarms, which I recently implemented in a project.

In my previous blog post, I wrote an article about how to study for the AWS certification SAA (Solution Architect Associate), so if you are interested, please take a look!

[Must-see for test takers] We will teach you how to study for the AWS certification SAA (Solution Architect)! !

Configuration diagram

The configuration diagram is shown above.

Obtain EC2 metrics with CloudWatch and notify SNS when an alarm occurs. SNS then sends various values ​​sent from CloudWatch to Lambda.

Lambda formats the value and sends it to each communication tool.
It may seem difficult because it uses various AWS resources, but it is easy to implement, so let's do our best together!

Construction steps

① Create Lambda

①-①:Lambda > Function > Click Create function

①-②: Specify the following information and click [Create function]

● Function name: Please specify a name of your choice
● Runtime: Please specify Python3 series

①-③: Paste the code for each communication tool

*If you do not want to write the webhook URL or authentication token directly in the code, please set environment variables.

● Code for Teams

#!/usr/bin/python3.8 import boto3 import json from urllib.request import Request, urlopen #Substitute 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 type to string and generate HTTP request req = Request(HOOK_URL, json.dumps(alert_msg).encode('utf-8')) #Send urlopen(req)

● Code for Chatwork

#!/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 query string data = urllib.parse.urlencode(alert_msg) #Convert character code data = data.encode('utf-8') #Generate HTTP request req = Request(chatwork_URL, data, headers) #send urlopen(req)

*Supplement: Message notation for each communication tool

● Teams
https://docs.microsoft.com/ja-jp/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL

● 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 has been notified

● Teams

● Chatwork

①-⑨: Edit variables as below for actual alarm notification

  #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:sns :ap-northeast-1:xxxxxxxx:test"],"InsufficientDataActions":[],"Trigger":{"MetricName":"CPUUtilization","Namespace":"AWS/EC2","StatisticType":"Statistic" ,"Statistic":"AVERAGE","Unit":"null","Dimensions":[{"value":"i-07876365e6af5774f","name":"InstanceId"}],"Period":300," EvaluationPeriods":1,"DatapointsToAlarm":1,"ComparisonOperator":"LessThanOrEqualToThreshold","Threshold":80.0,"TreatMissingData":"missing","EvaluateLowSampleCountPercentile":""}} #subject = "ALARM: test-CPUUtilization in Asia Pacific (Tokyo)"

② Create SNS

②-①: SNS > Topics > Click Create Topic

②-②: Specify the following information and click [Create topic]

● Type: Standard
● Name: Please enter your favorite name

②-③: Click [Create subscription]

 

 

②-④: Specify the following information and click [Create subscription]

● Protocol: Lambda
● Endpoint: *See below

*Lambda ARN information location

The ARN of the function is located on the far right of the created function > Function overview, so copy it from there.

②-⑤: Check status

③ Create CloudWatch

*We will proceed on the assumption that EC2 has been created.

③-①: Cloudwatch > Alarm > Click [Create alarm]

③-②: Select your favorite metrics

③-③: Select alarm condition

*Please set the alarm to occur.

③-④: Notification settings

③-⑤: Specify the alarm name

③-⑥: Click [Create alarm]

④ Alarm notification test

④-①: Check if the alarm is on.

④-②: Check whether the alarm has been notified

● Teams

● Chat Work

*Note:
If you want to check all the alarm information in CloudWatch, you can refer to it by editing the contents of the variable alert_msg 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":"Statistic","Statistic":" AVERAGE","Unit":"null","Dimensions":[{"value":"i-xxxxxxxxxxxxxx","name":"InstanceId"}],"Period":300,"EvaluationPeriods":1," DatapointsToAlarm":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/SNS does not have the ability to send alarms to Teams/Chatwork, so it is difficult to do so if you want to notify alarms.

You can also edit the code to send messages accordingly. Please make use of it when notifying an alarm!

If you found this article helpful , please give it a like!
4
Loading...
4 votes, average: 1.00 / 14
5,933
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Hide@Infrastructure Engineer

It all started with a very interesting interview.
A mid-career employee of the System Solutions Department in Osaka.My
job is to build and operate servers and clouds!
I have the qualifications of LPIC1, AWS SAA, and OCI Architect Associate.

Actually, I love ramen and
have investigated over 100 stores in Osaka (。-∀-) I'm striving to become the Ramen King of Nibi Beyond
!

I'm also on Twitter, so please follow me (´∇`)
Click on the Twitter mark on the right corner! !