0
votes

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 instances which are part of auto-scaling group.

For ec2 instances, which are part of auto-scaling group, ec2 instances are re-launched & running again.

AWS Lambda code is as follows;

import boto3

ec2 = boto3.client('ec2')


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()
    for reservation in ec2_instances['Reservations']:
        for ec2_instance in reservation['Instances']:
            if action == "stop":
                stop_ec2(ec2_instance)
            elif action == "start":
                start_ec2(ec2_instance)


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']])

Could you please assist to resolve this issue? I would like to make sure that ec2 instances which are part of auto-scaling must also stop and start as well.

1
That's how asg work. You can't stop instances in asg.Marcin
@Marcin - Thank you for quick reply. I can understand, but this lambda is to reduce cost of ec2 instances on Development and Test environments. So, I am looking from that point of view, for any workaround or a trick to stop ec2 on Development and Test environments.shubha
If you want to "Stop" rather than "Terminate" the Dev/Test instances, then do not put them in an Auto Scaling group. You could temporarily remove them from the Auto Scaling group and then stop them, but that defeats the whole purpose of using an Auto Scaling group. Either those instances can be terminated & relaunched (in which case use Scaling Policies to add/remove instances), or do not use an Auto Scaling group.John Rotenstein

1 Answers

0
votes

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
        )