AWSã®CloudWatchã§åå¾ã§ããBillingã®æ å ±ãæ¯æ¥Slackã«éç¥ããã¦è²»ç¨ã常ã«ææ¡ãã
DevOpsManã®æ§å
AWSã®å©ç¨éé¡ã¯æ¯æ¥ç¥ãããã
ããã§Slackã«æ¨æ¥ã¾ã§ã®å©ç¨éé¡(æãã¨ã«ãªã»ããããã)ãSlackã«éç¥ããããã«ããã
aws-sdkã¨ãã¯ä½¿ããã«ã·ã³ãã«ã«aws-cliã¨jqã¨curlã ãã§ããããã«ãã¦ããã
#!/usr/bin/env ruby require 'date' # ä»æ¥ã®æ¥ä» d = Time.now # æ¨æ¥ã® 00:00:00 ~ 23:59:59 ã®éã®ãã¼ã¿ãå©ç¨ã㦠start_time = DateTime.new(d.year, d.month, d.day) - 1 end_time = DateTime.new(d.year, d.month, d.day, 23, 59, 59) - 1 # ä¸æ¥åã® Sum å¤ã使ã£ã¦ period = '86400' # CloudWatchã®å¤ãåå¾ãã¦ã㦠strings = "æ¨æ¥ã¾ã§ã®AWSã®å©ç¨è²»(æãã¨)ã«ãªãã¾ã\n" strings << "```\n" # Billingã®ãã¼ã¿ãæã£ã¦ãã num = `aws cloudwatch --region us-east-1 get-metric-statistics \ --namespace 'AWS/Billing' \ --dimensions "Name=Currency,Value=USD" \ --metric-name EstimatedCharges --start-time #{start_time} \ --end-time #{end_time} \ --period #{period} --statistics 'Sum' \ | jq '.Datapoints[].Sum'` strings << "EstimatedCharges : $#{num}" strings << "```\n" strings << 'ã確èªããããããé¡ããããã¾ã' # DevOpsManã«ä¼ãã `curl -s https://slack.com/api/chat.postMessage -X POST \ -d 'channel=#CHANNEL' \ -d 'text= #{strings}' -d 'username=USERNAME' \ -d 'icon_emoji=:ICON:' \ -d 'token=xoxp-0000000000-0000000000-00000000000000000'`
å®å¸¸æ¥åã¯ã©ãã©ãbotã«ãããã¦ãçç£çãªæ¥åã«æ³¨åãã¦ããªã¥ã¼åºãã¦ãã