def replace_launch_config(name, image_id=None, key_name=None, security_groups=None, instance_type=None, instance_monitoring=None): """Replace launch configuration associated with auto scaling group.""" conn = boto.connect_autoscale() as_group = conn.get_all_groups(['{0}-as-group'.format(name)], max_records=1)[0] orig_launch_config_name = as_group.launch_config_name orig_launch_config = conn.get_all_launch_configurations(names=[orig_launch_config_name], max_records=1)[0] if not image_id: image_id = orig_launch_config.image_id if not key_name: key_name = orig_launch_config.key_name if not security_groups: security_groups = orig_launch_config.security_groups if not instance_type: instance_type = orig_launch_config.instance_type if not instance_monitoring: instance_monitoring = orig_launch_config.instance_monitoring config_name = "{0}_{1}".format(name, datetime.now().strftime("%Y%m%d%H%M")) puts("Replace launch config {0} with {1}".format(orig_launch_config_name, config_name)) new_launch_config = LaunchConfiguration(name=config_name, image_id=image_id, key_name=key_name, security_groups=security_groups, instance_type=instance_type, instance_monitoring=instance_monitoring) conn.create_launch_configuration(new_launch_config) min_size = int(as_group.min_size) max_size = int(as_group.max_size) as_group.launch_config_name = config_name # We need to setup min & max size again as_group.min_size = min_size as_group.max_size = max_size as_group.update() orig_launch_config.delete() return orig_launch_config_name, config_name
def create_ami(instance_id, name): """ Create AMI image from specified instance The instance needs to be shutdown before the creation begin. """ image_name = "{0}_{1}".format(name, datetime.now().strftime("%Y%m%d-%H%M")) conn = boto.connect_ec2() image_id = conn.create_image(instance_id=instance_id, name=image_name) puts("Creating AMI {0} for instance {1}".format(image_name, image_id)) while True: puts('.', end='') sys.stdout.flush() image = conn.get_image(image_id) if image.state == 'available': break if image.state == "failed": abort("Error creating AMI for {0}".format(image_id)) time.sleep(5.0) puts("\nImage {0} created".format(image_name)) return image_id