Usually when you’re using AWS’s services, you might want to know the usage of your resources, e.g. number of running instances, estimated cost and etc. Taking advatange of Slack’s webhook and AWS Service API, we can create a bot that sends AWS usage report to your slack channel periodically. This article introduces how it’s achieved.
Create the Webhook in your Slack Settings
Nagivate to your slack team management page and add a webhook, you will get a URL that is used to send message to Slack. Configure the settings to hook it to the desired channel and save it. Now sending message to the channel is just to call the URL with the correct payload.
Write your code
I use GO to implement the code. The logic is pretty simple. I used a few libraries to achieve it.
github.com/robfig/cron. A cron library in GO, which helps to schdule the messages.
github.com/aws/aws-sdk-go. AWS SDK in GO, which is used to retrieve information from your AWS account.
Set up IAM role or user
There two methods of granting your application to read access to your AWS account. The first method is to use IAM role, which is attached to your AWS EC2 instance, if you’re deploying the bot on a EC2 instance, this is the recommended method. The second methos is to use IAM user, you can create a user and get it’s credentials and set up your environment properly by using awscli or set the credentials in your code directly. If you set it in your code, you need to be very careful because it’s possible that it will get leaked.
Get estimated cost
To get estimated cost, you simple use GO AWS SDK to make query to CloudWatch:
It’s easy to write a bot in GO to send AWS Usage report to your Slack channel, as I has shown above. You’re not limited to do this, you can query other information you want as well. The full code is at here.