How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

Reading Time: 10 minutes

Consider that you’re running an eCommerce website that doesn’t have scaling capability in place, and also you’re much aware of the load such as when the load will increase and decrease. You also don’t want to assign a dedicated person to manually add or remove instances behind the load balancer and you want to ensure that the servers are running all the time even when the load is normal so that you can have a cost-effective solution. In this blog, we will implement a solution that will automatically register/add and deregister/remove instances that are running behind the Application Load balancer based on the predicted load.

In this blog, we will cover:

  • A quick recap of the following AWS Services that we will be using in the implementation
    • CloudWatch 
    • Lambda functions
    • EC2 – Elastic Compute Cloud
    • IAM – Identity and Access Management
    • Cloudwatch Events
  • AWS Elastic Load Balancing
  • Hands-On
    •  Creating two Lambda functions
      • Lambda to deregister and stop EC2 instances.
      • Lambda to start and register EC2 instances.
    • Creating IAM policies
    • Attaching IAM policies with IAM Roles
    • Testing Lambda Executions
    • Creating Cloudwatch Event Rules
  • Conclusion

CloudWatch and CloudWatch Events

  • AWS CloudWatch monitors AWS applications and resources in real-time.
  • CloudWatch collects and tracks metrics.
  • Metrics are data points that are published to CloudWatch.
  • CloudWatch alarms monitor metrics and automatically initiate actions.
  • CloudWatch Logs centralizes logs from AWS services, applications, and systems.
  • CloudWatch Events delivers a stream of system events that describe changes in AWS resources.
CloudWatch Events

Cloudwatch events provide us with a real-time stream of system events which helps in describing the recent changes in the resources.

We can create an Event rule which will trigger or start an event for one or more targets.

It helps to track all the changes in the resources and take corrective actions by sending events such as changing, activating functions, and capturing state information.

Supported Targets:

  • Batch job queue
  • CodePipeline
  • Cloudwatch Log group
  • SNS, SQS, Step Function
  • Code build, Lambda, EC2, ECS tasks, Kinesis
  • SSM, Firehose delivery stream

Concepts of Cloudwatch Events

  • Events: An event indicates a change in your AWS environment. An event arises from within AWS when a resource changes its state. It can also arise when events are generated by API calls and console sign-ins that are delivered to Amazon CloudWatch Events via CloudTrail. You can also set up scheduled events that are generated on a periodic basis.
  • Rules: match incoming events and route them to one or more targets or processing. Rules do not have any order for processing, all the rules matching a particular event will be processed. This enables different parts of an organization to look for and process the events based on their interest and priority.  A single rule can route to multiple targets. 
  • Targets: process events and are specified within the rules. Different types of targets include built-in, Lambda functions, Kinesis streams, and SNS topics. Targets receive events in JSON format. A single rule can specify multiple targets.
CloudWatch Events

What is Lambda?

AWS Lambda is a serverless computing service provided by AWS which allows users to run application code without managing servers. You can create functions, self-contained applications written in one of the supported languages and runtimes, and upload them to AWS Lambda, which executes those functions in an efficient way with high flexibility.

Users can use Lambda to run virtually for type with zero administration. 

You can use Lambda functions for any kind of computing task including data processing, serving web pages, calling APIs, scaling RAM, patching security issues, managing underlying OS, etc.

Benefits of Using Lambda

AWS Lambda offers Scalability and you will be charged only when your code runs. There is no charge when your code is not running.

No need to create, manage and monitor servers. AWS will do it for you by running the code on a high-availability compute environment.

  • No servers to Manage
  • Continuous Scaling
  • Cost-optimized with milliseconds metering
  • Consistent performance at any scale

Supported Languages

  • Java 11, Java 8
  • Node.js 14.x , Node.js 12.x , Node.js 10.x
  • Python 3.6, Python 3.7, Python 3.8 
  • Ruby 2.7, Ruby 2.5
  • Go 1.x
  • .NET Core 2.1 (C#/Powershell)
  • .NET Core 3.1 (C#/Powershell)

What is Elastic Compute Cloud?

Elastic Compute Cloud is a primary service in the massive AWS ecosystem that is highly used by users. It is a virtual server with customizable hardware components and an OS using which you can run applications on the AWS infrastructure. It allows you to configure and run multiple virtual OS and manage the same with a single hardware. 

EC2 Instance Types:

AWS EC2 offers different types of EC2 instances based on different use cases.

Each instance type has a varying combination of CPU, Memory, Disk, and Network performance and we can choose the appropriate instance type that best fits our applications.

  • General purpose Instance types:

These instance types provide a balance of CPU, memory, and network resources and it is ideal for web servers and for hosting self-managed code repositories.

  • Compute-optimized Instance types:

These instance types are most suited for high-performance web servers, batch processing workloads, high-performance computing (HPC), machine learning interfaces, gaming servers, etc. 

  • Memory-optimized Instance types:

These instance types deliver high performance for workloads that manage huge data sets in memory. For example MongoDB.

  • Accelerated computing Instance types:

These instances use hardware accelerators to perform processing of graphics, data pattern matching, etc.

  • Storage optimized instance types:

These instances are designed for high workloads that perform sequential read and write operations on the large data which are stored locally.

What is Elastic Load Balancing?

Elastic load balancing automatically distributes incoming requests across multiple targets such as EC2 instances, containers, and lambda functions.

It can handle the load by spreading the traffic to multiple targets which are hosted in a single or across multiple availability zones.

There are four types of load balancers that offer auto-scaling and high availability

What is Elastic Load Balancing?

Application Load Balancer

  • Operated at the request level 
  • Routes based on the content of the request (layer 7)
  • Supports host-based routing, path-based routing, query string parameter-based routing, and source IP address-based routing 
  • Supports IP addresses, Lambda functions, and containers as targets 
Application Load Balancer

Classic Load Balancer

Classic Load Balancer

Network Load Balancer

  • Operated at the connection level 
  • Routes connections based on IP protocol data 
  • Offers high performance, low latency, and TLS offloading at a high scale 
  • Can have static IP/Elastic IP
  • Supports static IP addresses and UDP as targets
Network Load Balancer

Gateway Load Balancer

  • Gateway Load Balancer makes it easy to deploy, scale, and run third-party virtual networking appliances.
  • An ideal choice for working with third-party appliances for security, network analytics, and other use cases.
  • Can be deployed using orchestration tools from industry leaders

Features of Elastic Load Balancing 

Features of Elastic Load Balancing 

Benefits of Elastic Load Balancing

Benefits of Elastic Load Balancing

Companies using AWS ELB

Companies using AWS ELB

Hands-On

We are going to implement a solution that will automatically Register / Add and Deregister / Remove instances that are running behind the Application Load balancer based on the predicted load.

We will be implementing the following:

  • Creating lambda functions
  • Lambda to deregister and stop EC2 instances.
  • Lambda to start and register EC2 instances.
  • Creating IAM policies
  • Attaching IAM policies with IAM Roles
  • Testing Lambda Executions
  • Creating Cloudwatch Event Rules

After implementing this solution, there will be no manual intervention required to Start, Stop, Register, or Deregister EC2 instances. So it can be a cost-effective solution!

Creating Lambda Functions

We will be creating two lambda functions.

  • One to De-register and Stop the instances.
  • One to Start and Register the instances.

You will also need the details such as Instances id’s and the TargetGroupArn

To create a lambda function, Login to AWS Console.

Under Services, Choose Lambda, Click Create function

Choose Author from Scratch, Under Basic information,

Provide a name for the function. Under Runtime, Choose Python 3.8 and leave the rest of the settings to default, and click Create function.

How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

Once the function is created, Go to Code source and double click lambda_function.py.

Remove the existing code and copy & paste the below code. Don’t forget to replace the values of TargetGroupArn and Instance Ids.

And click Deploy.

How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

We should also increase the Timeout value to 360 seconds. To do so, Choose the function, Under Configuration, General configuration.

Click Edit and set the Timeout value.

Again, the timeout value depends on the Deregistration delay you have set on the Attributes of Target Group.  

For example: If you set the deregistration delay to 60 seconds, set the Timeout value to 70 seconds for the function and update the value in the function code time.sleep(65)

Now, let’s go ahead and create another lambda function. The process is still the same.

Click Create function

How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

Choose Author from Scratch, Under Basic information,

Provide a name for the function. Under Runtime, Choose Python 3.8 and leave the rest of the settings to default, and click Create function.

How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

Once the function is created, Go to Code source and double click lambda_function.py.

Remove the existing code and copy & paste the below code. You must replace the values of TargetGroupArn and Instance Ids.

And click Deploy.

How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

Now that we have the lambda functions implemented to manage the EC2 instances.

In order to manage the instances by the lambda functions, lambda functions need the permissions such as Instance Stop, Instance Start, Register, and Deregister Instances.

We have to create an IAM policy and attach it with the IAM Roles that are associated with lambda functions.

Creating IAM Policy

Login to IAM Console, From the Navigation pane, Choose Policies

Click Create policy and then choose JSON and remove the existing policy and copy & paste the below policy document.

Don’t forget to replace the Instance id and TargetGroupArn in the policy document.

How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

Click Next: Tags, and then click Next: Review

Enter a name for the IAM policy and then click Create policy

Attaching IAM policy with IAM Role

Now we need to attach the IAM policy with the IAM roles attached with the lambda functions.

Go to the Lambda console and choose the lambda functions. Click Configurations and then choose Permissions

Click the Role name and then click Attach policies. Select the policy that you have created and then click Attach policy. You can repeat the same step for the other lambda functions.

Testing Phase

Now we are in the testing phase. We will perform testing to deregister and stop the instance.

Let us invoke the lambda function: deregister-stop-instance – which will deregister the instance from the load balancer and then stop the EC2 instance.

To manually invoke/trigger the lambda functions, Choose the function, Under Code Source,  Choose the drop-down menu near Test, and Click Configure test event.

How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

Enter a name for the sample test event and click Create and then click Test

As soon as the function is triggered, You can notice from the EC2 console, that the instances in the target groups go to a draining state

How to auto register and deregister EC2 Instances behind Application Load Balancer (ALB) using Lambda?

Once the de-registration of an instance is completed, the instance will be stopped.

To test the start and register of the instances, You can invoke the start-register-instance function.

In order to automate this process, We should configure Cloudwatch event rules to trigger the lambda functions.

Creating Cloudwatch Event Rules

Go to Cloudwatch Console, In the navigation pane, Under Events, Choose Rules

 We will be creating 2 rules, One for start-register-instance and One for deregister-stop-instance.

Creating Cloudwatch Event Rules

Now let’s create rules to invoke targets based on the events occurring in the AWS resources. Click Create rule, Under Event Source, Choose Schedule. We will schedule the lambda functions. You can trigger targets at a fixed rate in Minutes, Hours, and Days.

To trigger the targets at a particular time, Select the Cron expression. Once you have provided the cron expression as per your requirement,

Next Under Targets, Click Add Target, In the drop-down menu, Choose the Lambda function

Click Configure details,

Under Rule definition, We need to provide a name for the cloudwatch event rule, a description of the rule

And make sure that State is Enabled.

Click Create rule.

You can create multiple event rules if you want to trigger the targets multiple times in a day at a specific time. You can follow the same process.

Repeat the same steps for creating a Cloudwatch event rule to deregister and stop the instance at scheduled intervals based on the cron expression.

Conclusion

We have successfully deployed a solution that will automatically add and remove the instances that are running behind the application load balancer based on the predicted load. By implementing this, we are able to save cost by stopping the instances when the load is less and also we are able to avoid the manual intervention in managing EC2 instances. We will discuss more about AWS ELB  in our upcoming blogs. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies. 

For any further queries, feel free to post your comments, we are happy to help!

Meanwhile …

Keep Exploring -> Keep Learning -> Keep Mastering

This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.

Back To Top