Skip to content

Instantly share code, notes, and snippets.

@adrianholovaty
Created May 20, 2013 03:18
Show Gist options
  • Save adrianholovaty/4e354645dcf34ee0da92 to your computer and use it in GitHub Desktop.
Save adrianholovaty/4e354645dcf34ee0da92 to your computer and use it in GitHub Desktop.
Amazon Web Services auto-scaling setup
from boto.ec2.autoscale import AutoScalingGroup, LaunchConfiguration, ScalingPolicy
from boto.ec2.cloudwatch import MetricAlarm
LAUNCH_CONFIG_NAME = 'foo'
AMI_ID = 'your ami id here'
KEY_NAME = 'your key pair name here'
LOAD_BALANCER_NAME = 'your ELB name'
SECURITY_GROUPS = ['security groups here']
AVAILABILITY_ZONES = ['us-east-1a', 'us-east-1b']
INSTANCE_TYPE = 'm1.small' # Type of instance to use for new servers.
GROUP_NAME = 'group name here'
# Stuff to do on boot.
USER_DATA = """#!/bin/sh
/path/to/do_stuff
""".strip()
def set_up_aws():
lc = LaunchConfiguration(
name=LAUNCH_CONFIG_NAME,
image_id=AMI_ID,
key_name=KEY_NAME,
security_groups=SECURITY_GROUPS,
user_data=USER_DATA,
instance_type=INSTANCE_TYPE,
instance_monitoring=True,
)
conn = boto.connect_autoscale()
conn.create_launch_configuration(lc)
ag = AutoScalingGroup(
group_name=GROUP_NAME,
load_balancers=[LOAD_BALANCER_NAME],
availability_zones=AVAILABILITY_ZONES,
launch_config=lc,
min_size=2,
max_size=20,
connection=conn,
)
conn.create_auto_scaling_group(ag)
scale_up_policy = ScalingPolicy(
name='scale_up',
adjustment_type='ChangeInCapacity',
as_name=GROUP_NAME,
scaling_adjustment=2,
cooldown=180,
)
conn.create_scaling_policy(scale_up_policy)
scale_up_policy = conn.get_all_policies(as_group=GROUP_NAME, policy_names=['scale_up'])[0]
scale_down_policy = ScalingPolicy(
name='scale_down',
adjustment_type='ChangeInCapacity',
as_name=GROUP_NAME,
scaling_adjustment=-1,
cooldown=180,
)
conn.create_scaling_policy(scale_down_policy)
scale_down_policy = conn.get_all_policies(as_group=GROUP_NAME, policy_names=['scale_down'])[0]
cloudwatch = boto.connect_cloudwatch()
alarm_dimensions = {'AutoScalingGroupName': GROUP_NAME}
scale_up_alarm = MetricAlarm(
name='scale_up_on_cpu',
namespace='AWS/EC2',
metric='CPUUtilization',
statistic='Average',
comparison='>',
threshold='80',
period='60', # seconds
evaluation_periods=2, # How many `period`s it should wait until the alarm is set off.
alarm_actions=[scale_up_policy.policy_arn],
dimensions=alarm_dimensions,
)
cloudwatch.create_alarm(scale_up_alarm)
scale_down_alarm = MetricAlarm(
name='scale_down_on_cpu',
namespace='AWS/EC2',
metric='CPUUtilization',
statistic='Average',
comparison='<',
threshold='40',
period='60', # seconds
evaluation_periods=2, # How many `period`s it should wait until the alarm is set off.
alarm_actions=[scale_down_policy.policy_arn],
dimensions=alarm_dimensions,
)
cloudwatch.create_alarm(scale_down_alarm)
@shamrin
Copy link

shamrin commented May 20, 2013

@ShadySQL
Copy link

ShadySQL commented Apr 9, 2015

So how did you pass this in as user data when the autoscaling group would trigger a new EC2 instance to join it? Also based on:

Stuff to do on boot.

USER_DATA = """#!/bin/sh
/path/to/do_stuff
""".strip()

How can I pass the latest application release to this autoscaling group from git? I see that you did:
"""

!/bin/sh

su ubuntu
cd /path/to/soundslice/code
git pull
sudo service soundslice start
"""

Do you use virtual env?

@ravitejabadisa
Copy link

HI i am facing an issue whe i have migrated code from my local system to AWS

RawPostDataException at /users/
You cannot access body after reading from request's data stream

It seems i can't access request.method and request.data simulteneously.
Is this specific to AWS?Do i nedd to do some specific config changes to resolve this issue?

It would be great if you can suggest some work around for this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment