def create_launch_configuration(lcfg_name, configuration, client):
    user_data = configuration.get("user_data")
    image_id = configuration.get("image_id")
    security_group = configuration.get("security_groups").get("APP",
                                                              {}).get("id")
    instance_type = configuration.get("instance_type")
    associate_public_address = ASSOCIATE_PUBLIC_IP_BY_DEFAULT or not configuration.get(
        "debug_mode")
    try:
        response = client.create_launch_configuration(
            LaunchConfigurationName=lcfg_name,
            ImageId=image_id,
            SecurityGroups=[
                security_group,
            ],
            UserData=user_data,
            InstanceType=instance_type,
            InstanceMonitoring={'Enabled': True},
            PlacementTenancy='default',
            AssociatePublicIpAddress=associate_public_address,
        )
        validate_response_http_code(response)
    except ClientError as e:
        if _is_duplicate_exception(e):
            raise LCfgAlreadyExists
        else:
            raise e
Esempio n. 2
0
def get_subnets_for_vpc(session, vpc_id):
    client = _get_client(session)
    resource = _get_resource(session)

    subnets_collection = resource.subnets.all()
    subnet_ids = [x.id for x in subnets_collection]
    response = client.describe_subnets(SubnetIds=subnet_ids)
    validate_response_http_code(response)

    all_subnets = response["Subnets"]
    subnets_in_the_vpc = [x for x in all_subnets if x["VpcId"] == vpc_id]

    return [_get_subnet_data(subnet) for subnet in subnets_in_the_vpc]
Esempio n. 3
0
def get_available_vpcs(session):
    client = _get_client(session)
    resource = _get_resource(session)

    vpc_collection = resource.vpcs.all()

    vpc_ids = [x.id for x in vpc_collection]

    response = client.describe_vpcs(VpcIds=vpc_ids)

    validate_response_http_code(response)
    vpcs = response.get("Vpcs", [])
    return [_get_vpc_data(vpc) for vpc in vpcs]
Esempio n. 4
0
def _create_security_group(sg_name, vpc_id, client, description="generic description"):
    try:
        response = client.create_security_group(
            GroupName=sg_name,
            Description=description,
            VpcId=vpc_id)
        validate_response_http_code(response)
        security_group_id = response.get('GroupId')

    except ClientError as e:
        if _is_sg_duplicate_exception(e):
            security_group_id = _get_sg_id_by_name(sg_name, client)
            return security_group_id
        else:
            raise e
    return security_group_id
def create_scaling_policy(configuration, asg_name, client):
    project_name = configuration.get("project_name")
    scaling_target_value_percent = configuration.get(
        "scaling_target_value_percent")
    name = _get_scaling_policy_name(project_name)
    response = client.put_scaling_policy(AutoScalingGroupName=asg_name,
                                         PolicyName=name,
                                         PolicyType='TargetTrackingScaling',
                                         TargetTrackingConfiguration={
                                             'PredefinedMetricSpecification': {
                                                 'PredefinedMetricType':
                                                 'ASGAverageCPUUtilization',
                                             },
                                             'TargetValue':
                                             scaling_target_value_percent,
                                             'DisableScaleIn': False
                                         })
    validate_response_http_code(response)

    return name
def create_autoscaling_group(name, configuration, launch_cfg_name, client):

    project_name = configuration.get("project_name")
    min_size = configuration.get("min_size")
    max_size = configuration.get("max_size", 1)
    elb_name = configuration.get("elb_name")
    subnets = configuration.get("subnets")

    try:
        response = client.create_auto_scaling_group(
            AutoScalingGroupName=name,
            LaunchConfigurationName=launch_cfg_name,
            MinSize=min_size,
            MaxSize=max_size,
            DesiredCapacity=min_size,
            DefaultCooldown=DEFAULT_COOLDOWN_SECONDS,
            LoadBalancerNames=[
                elb_name,
            ],
            HealthCheckType='ELB',
            HealthCheckGracePeriod=HEATHCHECK_GRACE_PERIOD_SECONDS,
            VPCZoneIdentifier=subnets,
            NewInstancesProtectedFromScaleIn=False,
            Tags=[
                {
                    'ResourceId': name,
                    'ResourceType': 'auto-scaling-group',
                    'Key': 'Name',
                    'Value': project_name,
                    'PropagateAtLaunch': True
                },
            ],
        )
        validate_response_http_code(response)
    except ClientError as e:
        if _is_duplicate_exception(e):
            raise ASGAlreadyExists
        else:
            raise e
    return name