Skip to content

Instantly share code, notes, and snippets.

@ghaering
Created June 25, 2014 09:36
Show Gist options
  • Save ghaering/0fb24ee0963a0225afe9 to your computer and use it in GitHub Desktop.
Save ghaering/0fb24ee0963a0225afe9 to your computer and use it in GitHub Desktop.
Create AWS Billing alarms tor SNS topis "AWSBill"
from boto import sns
from boto.ec2 import cloudwatch
MY_TOPIC = "AWSBill"
sns_con = sns.connect_to_region("us-east-1")
sns_topic = [v.values()[0] for v in sns_con.get_all_topics()["ListTopicsResponse"]["ListTopicsResult"]["Topics"] if v.values()[0].endswith(":" + MY_TOPIC)][0]
cw_con = cloudwatch.connect_to_region("us-east-1")
# Get overall estimated charges metric for all services combined
metric = [m for m in cw_con.list_metrics(metric_name=u'EstimatedCharges',namespace=u'AWS/Billing') if m.__dict__["dimensions"].keys() == [u"Currency"]][0]
for spending_limit in range(1000, 20000, 1000):
metric.create_alarm(name="Billing_{}".format(spending_limit), comparison=">=", threshold=spending_limit, period=86400, evaluation_periods=1, statistic="Maximum", alarm_actions=sns_topic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment