Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

python 3.x - 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 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.

question from:https://stackoverflow.com/questions/65865305/lambda-not-working-for-ec2-with-auto-scaling

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
        )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...