fbpx

Optimizing AWS Costs: EKS Dynamic Scaling using EventBridge and Lambda

  • Home
  • Blogs
  • Optimizing AWS Costs: EKS Dynamic Scaling using EventBridge and Lambda

By Damindu Bandara

Introduction

Amazon EKS offers flexibility in deploying Kubernetes applications, with EC2 instances being a popular choice for node hosting. This guide focuses on optimizing costs by automating the scaling down of EKS clusters during off-peak hours. By leveraging AWS Lambda Functions and AWS EventBridge, you can efficiently reduce EC2 computing expenses without sacrificing operational efficiency.

Understanding the Components

AWS EventBridge:

  • Central hub for event-driven architecture.
  • Integrates various AWS services and custom applications.
  • Captures events from AWS services, SaaS apps, and custom sources.

AWS Lambda:

  • Serverless computing service.
  • Executes code in response to triggers.
  • Supports multiple programming languages.
  • Executes custom logic in response to events received from EventBridge.

Amazon Elastic Kubernetes Service (EKS):

  • Managed Kubernetes service on AWS.
  • Simplifies deployment, management, and scaling of containerized applications.
  • Eliminates the need to manage underlying infrastructure.
  • Enables efficient resource utilization and scaling based on demand.

Architecture Diagram

Steps Need to follow :

Step 1 : Configure an AWS IAM policy for EKS Cluster
Step 2 : Create an IAM role for the new policy for Lambda Function
Step 3 : Create Lambda Functions for ScaleUp and ScaleDown
Step 4 : Create EventBridge scheduler for Scale Down
Step 5 : Create EventBridge scheduler for Scale Up

Step 1 : Configure an AWS IAM policy for EKS Cluster

First we need to create an IAM policy for EKS Cluster. It allows ListNodegroupsUpdateNodegroupConfig, and DescribeNodegroup functions.

In IAM Console -> Access Management -> Policies -> Create Policy

On the JSON tab need to enter following policy code :

(You need to update cluster details in above code)

Step 2 : Create an IAM role for the new policy for Lambda Function

In IAM Console -> Access Management -> Roles-> Create Role

Select Trusted entity type as AWS Service and Use case as Lambda.

Next to the permissions and select the policy that you created in previous step.

Next to the review and enter role name as you preferred.

Step 3 : Create Lambda Functions for Scale Up and Scale Down

Step 3.1 : Create Lambda Functions for Scale Up

In Lambda -> Create Function

In Permission, Select Use an existing role and select role that you created in previous step.

Next add below python code in the code section and Deploy it.

(You need to update cluster details in above code)

Step 3.2 : Create Lambda Functions for Scale Down

Same as scale up function create the scale down function using below python code

(You need to update cluster details in above code)

Step 4 : Create EventBridge scheduler for Scale Down

In EventBridge -> Scheduler -> Create Schedule

Next you need to give schedule name and pattern. You can crate cron-based schedule for scale down the eks node groups.

Next select the target as Lambda.

In the Invoke section select the scale down function that you want to trigger.

Next Review and create a schedule.

Step 5 : Create EventBridge scheduler for Scale Up

Same as scale down scheduler, create a new scheduler for scale up function.

Conclusion

The article discusses about the scaling processes for Amazon Elastic Kubernetes Service (EKS) cluster, focusing specifically on those running on EC2 instances. It outlines the cost structure associated with running EKS clusters and suggests a strategy to reduce costs by scaling down non-production clusters during off-peak hours, such as at night. The proposed solution involves utilizing AWS Lambda Functions with Python scripts and AWS EventBridge to automate the scaling down process.