Trying to associate VPC with my load balancer for auto-scaling

79 views
Skip to first unread message

boto.u...@gmail.com

unread,
Jan 19, 2015, 8:36:32 AM1/19/15
to boto-...@googlegroups.com
Hello,

I have a script which creates the infrastructure for auto-scaling. I want to use a VPC which I have already created with this configuration.

I am running into an issue when I run my script, everything appears to be created, however the instances launch and then terminate. The error I am receiving is as follows:
Launching a new EC2 instance: i-c823862c. Status Reason: EC2 instance i-c823862c is in VPC. Updating load balancer configuration failed

I have tried specifying parameters in the load balancer creation for the subnets and the security groups, however it is telling me it only takes 3 arguments, which are load balancer name, zones and ports.
Any help would be greatly appreciated!

I will post my full code below:

#!/usr/bin/python -tt

import sys
import boto
import boto.ec2
import boto.ec2.zone
import boto.ec2.elb
import boto.ec2.autoscale
import boto.ec2.cloudwatch
#from boto.ec2.elb import loadbalancer
from boto.ec2.connection import EC2Connection
from boto.ec2 import connect_to_region
from boto.ec2.elb import ELBConnection
from boto.ec2.elb import HealthCheck
from boto.ec2.autoscale import AutoScaleConnection
from boto.ec2.autoscale import LaunchConfiguration
from boto.ec2.autoscale import AutoScalingGroup
from boto.ec2.autoscale import ScalingPolicy
from boto.ec2.cloudwatch import MetricAlarm



region = "eu-west-1" #The region I am connecting to
zoneStrings = 'eu-west-1a','eu-west-1b' #availability zones of the VPC

#storage of data related to each section is performed through python dictionaries 

elastic_load_balancer = {
    'name': 'portland-lb',#The name of your load balancer
    'health_check_target': 'HTTP:8080/',#Location to perform health checks
    'ports': [(80, 8080, 'http'), (443, 8443, 'tcp')],#[Load Balancer Port, EC2 Instance Port, Protocol]
    'timeout': 5, #Number of seconds to wait for a response from a ping
    'interval': 30, #Number of seconds between health checks
    'vpc_subnets' : ['subnet-09519f50', 'subnet-9fa12dfa'],
    'security_groups': ['sg-be77e8db']
}

lc_name = 'portland-lc'

 
autoscaling_group = {
    'name': 'portland-autoscaling-group',#descriptive name for your auto scaling group
    'min_size': 1,#Minimum number of instances that should be running at all times
    'max_size': 2, #Maximum number of instances that should be running at all times
    'desired_capacity' : 1,
    'vpc_subnets' : ['subnet-09519f50', 'subnet-9fa12dfa']
 
}


portland_ami = {
    'id': 'ami-0f870678', #The AMI ID of the instance your Auto Scaling group will launch
    'access_key': 'demo_keys', #The key the EC2 instance will be configured with
    'security_groups': ['sg-be77e8db'], #The security group(s) your instances will belong to
    'instance_type': 't2.micro', #The size of instance that will be launched
    'instance_monitoring': True, #Indicated whether the instances will be launched with detailed monitoring enabled for CloudWatch.
    'associate_public_ip': ['subnet-09519f50', 'subnet-9fa12dfa'] 
}


elb_conn = boto.ec2.elb.connect_to_region('eu-west-1')

hc = HealthCheck(access_point=elastic_load_balancer['name'],#'healthCheck',
                     interval=elastic_load_balancer['interval'],
                     target=elastic_load_balancer['health_check_target'],
                     timeout=elastic_load_balancer['timeout'])
 

lb = elb_conn.create_load_balancer(elastic_load_balancer['name'],
                                       #zoneStrings,
                                       zones=None,
                                       subnets=elastic_load_balancer['vpc_subnets'],
                                       security_groups=elastic_load_balancer['security_groups'],
                                       scheme='internet-facing',
                                       complex_listeners=None)
                                       #elastic_load_balancer['ports']

lb.configure_health_check(hc)

print "Map the CNAME of your website to: %s" % (lb.dns_name)
print "Load balancer successfully created! Please check aws for more information."


#Launch Configuration and Auto Scaling Group Creation
asg_conn = boto.ec2.autoscale.connect_to_region('eu-west-1')

lc = LaunchConfiguration(name=lc_name, image_id=portland_ami['id'],
                             key_name=portland_ami['access_key'],
                             security_groups=portland_ami['security_groups'],
                             instance_type=portland_ami['instance_type'],
                             instance_monitoring=portland_ami['instance_monitoring'], 
                             associate_public_ip_address=portland_ami['associate_public_ip'])


asg_conn.create_launch_configuration(lc)
print "launch configuration created"

ag = AutoScalingGroup(group_name=autoscaling_group['name'], load_balancers=[elastic_load_balancer['name']],
                          availability_zones=zoneStrings,
                          launch_config=lc, min_size=autoscaling_group['min_size'], max_size=autoscaling_group['max_size'], 
                          vpc_zone_identifier=autoscaling_group['vpc_subnets'])
asg_conn.create_auto_scaling_group(ag)
print "Auto-Scaling Group created"


#Creation of Scaling Policies
scalingUpPolicy = ScalingPolicy(name='webserverScaleUpPolicy',
                                              adjustment_type='ChangeInCapacity',
                                              as_name=ag.name,
                                              scaling_adjustment=2,
                                              cooldown=180)
 

scalingDownPolicy = ScalingPolicy(name='webserverScaleDownPolicy',
                                              adjustment_type='ChangeInCapacity',
                                              as_name=ag.name,
                                              scaling_adjustment=-1,
                                              cooldown=180)



asg_conn.create_scaling_policy(scalingUpPolicy)
print "Scale up policy created"
asg_conn.create_scaling_policy(scalingDownPolicy)
print "Scale down policy created"

#Policy Refresh
scalingUpPolicy = asg_conn.get_all_policies(as_group=ag.name, policy_names=['webserverScaleUpPolicy'])[0]

scalingDownPolicy = asg_conn.get_all_policies(as_group=ag.name, policy_names=['webserverScaleDownPolicy'])[0]







Adrian Klaver

unread,
Jan 19, 2015, 9:54:32 AM1/19/15
to boto-...@googlegroups.com
On 01/19/2015 05:36 AM, boto.u...@gmail.com wrote:
> Hello,
>
> I have a script which creates the infrastructure for auto-scaling. I
> want to use a VPC which I have already created with this configuration.
>
> I am running into an issue when I run my script, everything appears to
> be created, however the instances launch and then terminate. The error I
> am receiving is as follows:
> Launching a new EC2 instance: i-c823862c. Status Reason: EC2 instance
> i-c823862c is in VPC. Updating load balancer configuration failed

So instance creation starts but the process halts when it gets to the
load balancer configuration, for the reasons below.

>
> I have tried specifying parameters in the load balancer creation for the
> subnets and the security groups, however it is telling me it only takes
> 3 arguments, which are load balancer name, zones and ports.
> Any help would be greatly appreciated!

What version of boto are you using?
The parameters accepted have changed over the versions.


There is a lot going on below. If it where me I would go with 'explict
is better then implicit'. In your load balance and autoscale creation I
would explicitly set the parameter vales instead of pulling them from
other objects, at least until you get this working. One less source of
error. Also makes easier for those of us trying to follow along:)
> --
> 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
> <mailto:boto-users+...@googlegroups.com>.
> To post to this group, send email to boto-...@googlegroups.com
> <mailto:boto-...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/boto-users.
> For more options, visit https://groups.google.com/d/optout.


--
Adrian Klaver
adrian...@aklaver.com
Reply all
Reply to author
Forward
0 new messages