I am trying to automate the creation and subscription of SNS topics for my alarms and scaling policies for my auto scaling group. I was creating them manually before.
It keeps returning the following error when I try run my script and I am at a loss as to how to fix this:
#!/usr/bin/python -tt
import os
import getpass
import boto
import time
import boto.ec2
import boto.sns
import boto.ec2.cloudwatch
import boto.ec2.autoscale
import boto.ec2.cloudwatch.metric
import boto.ec2.cloudwatch.alarm
from boto.ec2.autoscale import ScalingPolicy
from boto.ec2.autoscale import AutoScalingGroup
from boto.ec2.cloudwatch import MetricAlarm
from boto.ec2.cloudwatch import Metric
from boto.ec2.cloudwatch import connect_to_region
from boto.ec2.cloudwatch.metric import Metric
from datetime import datetime
region = 'us-west-2'
auto_ScalingGroup = 'autoscaling-group'
metric_name = "StatusCheckFailed"
asg_conn = boto.ec2.autoscale.connect_to_region(region)
topicname = 'Scale up on status check failure alarm'
topicnameB = 'Scale down on status check pass alarm'
conn = boto.sns.connect_to_region(region)
topic = conn.create_topic(topicname)
topic_arn1 = topic['CreateTopicResponse']['CreateTopicResult']['TopicArn']
print topicname, "has been successfully created with a topic ARN of", topic_arn1
addr = conn.subscribe(topic_arn1, 'email', email_addresses)
print 'Subscribing %s to Topic %s' % (addr, topic_arn1)
conn = boto.sns.connect_to_region(region)
topic = conn.create_topic(topicnameB)
topic_arn2 = topic['CreateTopicResponse']['CreateTopicResult']['TopicArn']
print topicnameB, "has been successfully created with a topic ARN of", topic_arn2
addr = conn.subscribe(topic_arn2, 'email', email_addresses)
print 'Subscribing %s to Topic %s' % (addr, topic_arn2)
metric = cw.list_metrics(dimensions={"AutoScalingGroupName": 'auto_ScalingGroup'}, metric_name=metric_name)[0]
scalingUpPolicy = ScalingPolicy(name='webserverScaleUpPolicy',
adjustment_type='ChangeInCapacity',
as_name=auto_ScalingGroup,
scaling_adjustment=1,
cooldown=180)
scalingDownPolicy = ScalingPolicy(name='webserverScaleDownPolicy',
adjustment_type='ChangeInCapacity',
as_name=auto_ScalingGroup,
scaling_adjustment=-1,
cooldown=180)
def create_ScalingPolicies(): #Creation of Scaling Policies
print 'Script Start Time ' + datetime.today().isoformat(' ')
global scalingUpPolicy
asg_conn.create_scaling_policy(scalingUpPolicy)
print "Scale up policy created"
global scalingDownPolicy
asg_conn.create_scaling_policy(scalingDownPolicy)
print "Scale down policy created"
#Policy Refresh in order to update ASG.
scalingUpPolicy = asg_conn.get_all_policies(as_group=auto_ScalingGroup, policy_names=['webserverScaleUpPolicy'])[0]
scalingDownPolicy = asg_conn.get_all_policies(as_group=auto_ScalingGroup, policy_names=['webserverScaleDownPolicy'])[0]
def create_StatusAlarms():
cloudwatch = boto.ec2.cloudwatch.connect_to_region(region)
metric = cw.list_metrics(dimensions={"AutoScalingGroupName": 'auto_ScalingGroup'}, metric_name=metric_name)[0]
alarm_actions = [topic_arn1][0]
alarm_actions = []
alarm_actions.append(scalingUpPolicy.policy_arn)
if alarm_actions.append:
print "Alarm has been added to the Scaling Policy"
else:
print "Alarm append was unsuccessful."
metric.create_alarm(name=topicname,
comparison='>=',
threshold=2,
period=300,
evaluation_periods=1,
statistic='Average',
alarm_actions=alarm_actions,
ok_actions=alarm_actions)
if metric.create_alarm:
print "Scale up alarm created for status check failure"
else:
print "Alarm could not be created, please check parameters"
alarm_actions = [topic_arn2][0]
alarm_actions = []
alarm_actions.append(scalingDownPolicy.policy_arn)
if alarm_actions.append:
print "Alarm has been added to the Scaling Policy"
else:
print "Alarm append was unsuccessful."
metric.create_alarm(name=topicnameB,
comparison='<=',
threshold=2,
period=300,
evaluation_periods=1,
statistic='Average',
alarm_actions=alarm_actions,
ok_actions=alarm_actions)
if metric.create_alarm:
print "Scale down alarm created for status check failure"
else:
print "Alarm could not be created, please check parameters"
print 'Script End Time ' + datetime.today().isoformat(' ')
if __name__ == '__main__': #Python Boilerplate, runs functions in sequence, starting with main.
create_ScalingPolicies()