{{announcement.body}}
{{announcement.title}}

AWS Resources Utilisation Automation and Integration With MS-Teams/Slack

DZone 's Guide to

AWS Resources Utilisation Automation and Integration With MS-Teams/Slack

In this article, we discuss how to gather CPU utilization metrics and set up automated reporting on MS Teams and Slack.

· Cloud Zone ·
Free Resource

Problem Statement

  • Gather the metric statistics for under or over CPU Utilization for all EC2 instances under all regions.
  • Send the notification on MS-Teams/Slack for better communication and collaboration.

Solution

There are a lot of ways to collect the statistics, as list below:

  • AWS Console.
  • AWS CLI.
  • Terraform.
  • Customized Automation.

This is not the complete list, and there are more ways to achieve this task.

Approach

Here, we have taken an approach to gather metrics statistics using CloudWatch AWS CLI and send the notification on MS-Teams channel or Slack channel, without using CloudWatch Alarm and SNS Notification. Currently, MS-Teams and Slack are good tools for communication and collaboration to send the notification over a channel.

A customized script is written to gather metrics statistics for all running EC2 instances in all the regions; not only for a single region (usually it returns the details for default region). Maximum CPU Utilization statistics will be gathered for the last 24 hours on an hourly basis.

Prerequisites

AWS CLI should be installed, and credentials should be present in the “~/.aws/credentials” file after running the aws configure command.

MS-Teams and Slack should be installed, and channel should be created to send the notification, so every member can check resource utilization in the last 24 hours to take some action or audit purpose.

Conditions to be modified for under or over utilization are shown below

For over utilization: Current CPU Utilisation Percentage > Maximum CPU Threshold

There is a variable called “maxCPUTheshold” where maximum threshold value can be defined; as of now, this value is set as “70%”.

For below utilization: Current CPU Utilisation Percentage < Maximum CPU Threshold

There is a variable called “minCPUTheshold” where minimum threshold value can be defined; as of now, this value is set as “2%”.

Send the Notification

An incoming webhook needs to be created at MS-Teams or Slack side. Follow the below-mentioned steps to create incoming webhook:

  1. Open MS Teams, select “Channel”, and then click on More options (…) and choose Connectors.Selecting Connectors in MS-Teams
  2. A pop-up window will come up. Select All from the Category section in the left pane and click the Configure button to add an Incoming Webhook.Configuring incoming Webhook
  3. Another window will pop-up. That’s it for configuring the Incoming Webhooks connector for a Channel. Provide the webhook name, and you can upload an image for this incoming webhook like the company’s logo. Then, click on the Create button.Adding Connector
  4. The final step is to copy the URL and click on the Done button.Finishing adding Webhook

We can set up the incoming webhook in Slack in very much the same way. Please find the URL below to set the Slack incoming webhook.

https://api.slack.com/messaging/webhooks#posting_with_webhooks

Just change the incoming webhook URL for Slack in the existing script, and it will work fine for Slack as well.

How to Trigger

Either script can be called through a cron job or through Jenkinsfile. However, a trigger through Jenkinsfile is advisable, as Jenkinsfile will be stored at a remote repository in GitHub, GitLab, or BitBucket. You can also mention the schedule job under triggers, as shown below:

Groovy
 







Find the code repository in Github here AWS_Resources_Utilization, and check out my DZone profile here.

Please check another article written by me; where a dockerized approach is explained to execute the AWS CLI in the container AWS CLI in Docker.

Keep learning!!!

“Learning never exhausts the mind.” ― Leonardo da Vinci

Topics:
aws, docker, ec2, jenkins, notification, slack, task automation, teams

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}