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.
Set up IAM role or user
The best and easiest way of granting this role or user is to give readonly access to all services. However if you want to play safe, you can choose to only grant read access to the following services:
EC2
S3
CloudFront
RDS
Elasticache
CloudWatch
Billing
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.
{ "attachments": [ { "fallback": "Required plain-text summary of the attachment.", "color": "#2eb886", "pretext": "Optional text that appears above the attachment block", "author_name": "Bobby Tables", "author_link": "http://flickr.com/bobby/", "author_icon": "http://flickr.com/icons/bobby.jpg", "title": "Slack API Documentation", "title_link": "https://api.slack.com/", "text": "Optional text that appears within the attachment", "fields": [ { "title": "Priority", "value": "High", "short": false } ], "image_url": "http://my-website.com/path/to/image.jpg", "thumb_url": "http://example.com/path/to/thumb.png", "footer": "Slack API", "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png", "ts": 123456789 } ] }
Schedule it using cron
Taking advantage of cron, we can schedule it very easily. The following are some examples:
1 2 3
0 0 1 * * MON-FRI // Every 1am UTC on Weekdays 0/10 0 1 * * ? // Every 10 seconds 0/10 0 */8 * * ? // Every 8 hours
Summary
It’s easy to write a bot in GO to send AWS Usage report to your Slack channel, as I has shown above. You can extend the code by contributing to the repo and adding more information to report.