Autoscaling With Lambda Part-1

Autoscaling With Lambda Part-1

This blog will be a combination of 2 parts and it will cover the concept of launching an EC2, adding the newly created EC2 in AutoScaling Group every time a URL gets hit.

If you want to launch a new server for every new user that hit your URL, then you have come to the right place.

In this part of the blog, we will launch an EC2 and add that EC2 to an AutoScaling group and the good thing is all this will be done by the AWS Lambda functions.

The first thing we need to do is make an AWS Lambda function that will launch an EC2. I'll be using python to write my Lambda functions.

So let's get going and do some work...

Step-1 Make a Lambda function to launch EC2

Login into your AWS account, go to Lambda service -> Click on create function -> select Author from scratch

Write Function name -> select Runtime (I'll be using python 3.7 ) -> click Architecture (I'll be using x86_64) and finally select appropriate execution role ( I will suggest creating a new role and giving necessary permission to it. Permission name given at the end of this blog )

Step-2 Write code to complete the Lambda function

Go to the Code tab and paste the below code -> click on Deploy

import os
import boto3

AMI = os.environ['AMI']
INSTANCE_TYPE = os.environ['INSTANCE_TYPE']
KEY_NAME = os.environ['KEY_NAME']
SUBNET_ID = os.environ['SUBNET_ID']

ec2 = boto3.resource('ec2')


def lambda_handler(event, context):

    instance = ec2.create_instances(
        ImageId=AMI,
        InstanceType=INSTANCE_TYPE,
        KeyName=KEY_NAME,
        SubnetId=SUBNET_ID,
        MaxCount=1,
        MinCount=1,

    )
    return(instance[0].id)

In the above code, I've used the python 3.7 version along with boto3, here I'm also feeding various environment variables like AMI, INSTANCE_TYPE, KEY_NAME, and SUBNET_ID.

All these variables need to be set in the Configuration tab and Environment Variables block of Lambda. You can also change MaxCount and MinCount according to your choice. I'll be returning EC2 ID as output in this function for future use.

Step-3 Testing the Lambda

Click Test -> Configure test event will be opened -> select Create new test event -> select Event template and choose hello-world -> put event name of ur choice -> hit create and finally hit Test

Now if everything is done correctly you will see an EC2 being launched in your dashboard. If you can see the extra EC2, then congrats you have completed 50% of the process.

Now we will move on to the next part of our process adding that EC2 into our Autoscaling Group.

Step-4 Adding EC2 in our Autoscaling Group

To add our EC2 to the Autoscaling group we need to create another Lambda function. We need to follow Step 1 again but this time have to choose python 3.9 and select the same execution role we choose for our previous Lambda function.

Step-5 Write code to complete the Lambda function

import boto3
import json

client = boto3.client('autoscaling')

def lambda_handler(event, context):

    response = client.attach_instances(
    InstanceIds=[
        event,
    ],

    AutoScalingGroupName='trial-asg'

)

In this Lambda function, we feed our Autoscaling group name ( for me it's 'trial-asg' ) in which we want to add our EC2.

If you remember we made our EC2 ID as an output in our first Lambda function it's time to use it.

Now here is a catch, the output of one Lambda function can't be directly used in another Lambda function. So we have to use the AWS StepFunction service to integrate both of our Lambda functions.

Step-6 Integration of our Lambda fucntions

Now the question is how we gonna integrate our Lambdas using AWS StepFunction...

Well if you check our second Lambda code we are using an event name variable inside our InstanceIds list block, we gonna store our newly launched EC2 ID which is generated in our first Lambda function in this event variable and pass it to our second Lambda function.

Now let's make a StepFunctions for our integration...

Step-7 Making StepFunction

Go to AWS StepFunction service -> hit Create State Machine -> choose Design your workflow visually -> Type Standard -> Hit Next

Now in the Action section select AWS LAMBDA and drag it to the first state in your workflow area.

In the Configuration section on your right choose your State name -> Integration type Optimized -> API Parameters to select your first Lambda ( In my case it's ec2-launch ) and leave the other setting as it is.

Now drag the second Lambda function as we did before then in the Configuration section on your right choose your State name -> Integration type Optimized -> API Parameters to select your first Lambda ( In my case it's adding-ec2 ).

Now the next step is to add a Wait State, purpose of a Wait State is to wait for a few seconds ( in my case it 4secs ) before adding our EC2 in our AutoScaling Group so that our EC2 go from pending to initialization state and won't cause any error.

After all the 7 Steps your StepFunction should look like the above.

Click Next -> Review generate code ->click Next

Then name your State Machine -> Create a new role in the permission section and finally hit Create state machine.

At last start the execution of your State Machine.

Important Points

  1. Make sure your Lambda execution role has all necessary permission like AutoScaling group, EC2, StepFunction, and Lambda.

  2. Also make sure your AutoScaling Group has the correct value set for Desired capacity, Minimum capacity, and Maximum capacity.

If you have followed me till the end of this blog, then I wanna appreciate your support. I also wanna say thanks to the StackOverflow guys (whose names I don't remember ) to help me come up with this solution.