Trying to create SNS topics for Scaling Policy alarms returns Invalid Parameter error

419 views
Skip to first unread message

boto.u...@gmail.com

unread,
Feb 18, 2015, 6:49:00 AM2/18/15
to boto-...@googlegroups.com
Hello,

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:

Traceback (most recent call last):
  File "./create_scaling_alarms_and_metrics.py", line 38, in <module>
    topic = conn.create_topic(topicname)
  File "/usr/local/lib/python2.7/dist-packages/boto-2.36.0-py2.7.egg/boto/sns/connection.py", line 203, in create_topic
    return self._make_request('CreateTopic', params)
  File "/usr/local/lib/python2.7/dist-packages/boto-2.36.0-py2.7.egg/boto/sns/connection.py", line 765, in _make_request
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.BotoServerError: BotoServerError: 400 Bad Request
{"Error":{"Code":"InvalidParameter","Message":"Invalid parameter: Topic Name","Type":"Sender"},"RequestId":"1d11e31b-ade6-58fc-9957-ae45d7403706"}


The full code is as follows:

#!/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'
email_addresses = 'joh...@gmail.com'

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()

Mitchell Garnaat

unread,
Feb 18, 2015, 9:09:57 AM2/18/15
to boto-users
Your topic name does not conform to the constraints specified by the SNS API:

Topic names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, and hyphens, and must be between 1 and 256 characters long.

--
You received this message because you are subscribed to the Google Groups "boto-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to boto-users+...@googlegroups.com.
To post to this group, send email to boto-...@googlegroups.com.
Visit this group at http://groups.google.com/group/boto-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages