Skip to content Skip to sidebar Skip to footer

Lambda Not Working For Ec2 With Auto-scaling

Following AWS Lambda is working as expected [i.e. ec2 instance stop and start] for ec2 instances, which are not part of any auto-scaling group, but it is not working for ec2 instan

Solution 1:

Following is kind of work-around. You might need to re-factor some code as per your setup.

import boto3

ec2 = boto3.client('ec2')

auto_scaling_group_client = boto3.client('autoscaling')
# TODO Refactor auto_scaling_group_processes list as per your setup
auto_scaling_group_processes = ['Launch',
                                'Terminate',
                                'HealthCheck',
                                'ReplaceUnhealthy',
                                'ScheduledActions',
                                'AddToLoadBalancer',
                                'AlarmNotification',
                                'AZRebalance']


def lambda_handler(event, context):
    action_handler(event['action'])


def get_ec2_instances():
    ec2_int = ec2.describe_instances(
        Filters=[{
            'Name': 'ops',
            'Values': [
                'cost-save'
            ]
        }]
    )
    return ec2_int


def action_handler(action):
    ec2_instances = get_ec2_instances()
    auto_scaling_groups_to_resume = set()
    # TODO Can refactor logic added related to auto_scaling
    for reservation in ec2_instances['Reservations']:
        for ec2_instance in reservation['Instances']:
            auto_scaling_group_name = get_auto_scaling_group_name(ec2_instance['InstanceId'])
            if action == "stop":
                suspend_processes(auto_scaling_group_name)
                stop_ec2(ec2_instance)
            elif action == "start":
                auto_scaling_groups_to_resume.add(auto_scaling_group_name)
                start_ec2(ec2_instance)
    resume_auto_scaling_group_processes(auto_scaling_groups_to_resume)


def stop_ec2(ec2_int):
    if ec2_int['State']['Name'] == 'running':
        ec2.stop_instances(InstanceIds=[ec2_int['InstanceId']])


def start_ec2(ec2_int):
    if ec2_int['State']['Name'] == 'stopped':
        ec2.start_instances(InstanceIds=[ec2_int['InstanceId']])


def resume_processes(auto_scaling_group_name):
    if auto_scaling_group_name is not None:
        auto_scaling_group_client.resume_processes(
            AutoScalingGroupName=auto_scaling_group_name,
            ScalingProcesses=auto_scaling_group_processes
        )


# TODO Can refactor
def get_auto_scaling_group_name(ec_int_id):
    auto_scaling_group = auto_scaling_group_client.describe_auto_scaling_instances(
        InstanceIds=[
            ec_int_id
        ]
    )
    for auto_scaling_int in auto_scaling_group['AutoScalingInstances']:
        return auto_scaling_int['AutoScalingGroupName']


# TODO Can refactor
def resume_auto_scaling_group_processes(auto_scaling_groups):
    for auto_scaling_group in auto_scaling_groups:
        resume_processes(auto_scaling_group)


def suspend_processes(auto_scaling_group_name):
    if auto_scaling_group_name is not None:
        auto_scaling_group_client.suspend_processes(
            AutoScalingGroupName=auto_scaling_group_name,
            ScalingProcesses=auto_scaling_group_processes
        )


Post a Comment for "Lambda Not Working For Ec2 With Auto-scaling"