Back to blog
API GATEWAY

How to Use the Serverless Option for Provisioning an EKS Cluster

October 14, 2024 | 14 min read
How to Use the Serverless Option for Provisioning an EKS Cluster

Managing containerized applications with Kubernetes can be complex and resource-intensive. Fortunately, Amazon Elastic Kubernetes Service (EKS) offers a serverless option through AWS Fargate, which simplifies the process by allowing you to run Kubernetes pods without provisioning or managing EC2 instances. In this article, we'll walk through how to use AWS Fargate, the serverless compute engine, to provision EKS clusters easily. By the end, you’ll understand how to simplify cluster management using CloudFormation and Lambda to fully automate the provisioning process.

What is AWS Fargate and How It Enables Serverless EKS Clusters?

AWS Fargate it is AWS's serverless compute engine that allows you to run containers without worrying about the underlying infrastructure.

Instead of managing EC2 instances for your Kubernetes worker nodes, Fargate lets you run Kubernetes pods directly, completely removing the need to provision and scale servers manually. To put things into perspective, if you were setting up a Kubernetes cluster on bare metal, you'd have to manage everything from provisioning physical servers to setting up your networking, installing Kubernetes, and manually scaling the nodes.

Even with regular EKS, you'd still need to configure and manage EC2 instances and scaling groups and ensure worker nodes are healthy and available.

However, with Fargate, all of that complexity disappears. You no longer need to worry about the underlying infrastructure at all—AWS handles the provisioning, scaling, and maintenance of the compute resources for you. This serverless approach frees you up to focus entirely on deploying and managing your applications, not the servers they run on.

Steps to Provision an EKS Cluster Using AWS Fargate

The following steps will guide you in configuring your EKS cluster with Fargate.

Step 1: Create an EKS Cluster

The first step is creating the EKS cluster itself. You can do this via the AWS Management Console, eksctl, or using the AWS CLI.

Via AWS console:

Provision an EKS Cluster Using AWS Fargate

  • Fill in the required details like cluster name, Kubernetes version, etc.
Fill in the required details like cluster name, Kubernetes version, etc.

Via eksctl:

You can also use eksctl, a simple CLI tool for creating and managing EKS clusters. Use the following command to create a cluster via

eksctl
:

eksctl create cluster --name <cluster-name> --region <region> --fargate

Step 2: Setting up an EKS Pod execution IAM role

Before running pods on Fargate, you'll need to set up an EKS Pod Execution IAM role. This role allows Fargate to launch and manage pods in your EKS cluster securely. To create the role, follow the steps in this AWS article.

Setting up an EKS Pod execution IAM role

Step 3: Define Fargate Profiles

Once your cluster is up, you need to create a Fargate Profile. This step tells AWS which Kubernetes pods should be scheduled onto Fargate. Fargate profiles allow you to fine-tune which workloads should run on Fargate. You can associate namespaces and labels with your Fargate profile to ensure that only specific pods are deployed on Fargate.

Via AWS console:

  • Navigate to the EKS > Clusters page, select your EKS cluster and click Compute > Add Fargate Profile.
create a Fargate Profile

  • Specify a name for your profile and choose the Kubernetes namespace and pod labels you want to map to Fargate. This means that any pod in the chosen namespace will automatically run on Fargate.
Kubernetes namespace and pod labels

Via eksctl: Run the following command to create a Fargate profile:

eksctl create fargateprofile \
--cluster <cluster-name> \
--name <fargate-profile-name> \
--namespace <namespace-name>

Step 4: Deploy your workloads

Now that your cluster is configured with a Fargate profile, it's time to deploy your Kubernetes workloads. At this point, you have a serverless Kubernetes cluster ready, but without deploying workloads (pods), there's nothing running in your cluster. This step ensures your applications are deployed and managed by Fargate.

Using kubectl:

After you've configured the Fargate profile, you can deploy pods using the kubectl command. Make sure that the namespace and labels match what you defined in the Fargate profile:

kubectl apply -f <your-deployment-file>.yaml

Verification: You can check if the pods are running on Fargate by using the command:

kubectl get pods -n <namespace>

Serverless Automation with AWS CloudFormation and Lambda

To make the process of provisioning and managing your EKS clusters even more streamlined, you can use AWS CloudFormation and AWS Lambda. These tools automate the deployment of your infrastructure, so you won't need to manage resources manually.

Defining EKS Infrastructure with AWS CloudFormation

CloudFormation allows you to create templates that define your EKS infrastructure as code. This means you can automate the creation of the EKS cluster, VPCs, Fargate profiles, and other resources in a repeatable manner. By using CloudFormation, you can quickly replicate your EKS setup across different environments or recover quickly in case of failure. It also reduces the chance of human error when manually configuring your infrastructure.

To create a CloudFormation template for EKS, follow these steps:

Go to the CloudFormation console and select Create stack.

create a CloudFormation template for EKS

  • Upload or author your template that defines the EKS cluster, VPC, and Fargate configurations.
create a CloudFormation template for EKS

The following is a snippet of a CloudFormation template for setting up an EKS cluster with Fargate:

Resources:
EKSCluster:
Type: AWS::EKS::Cluster
Properties:
Name: myEKSCluster
ResourcesVpcConfig:
SubnetIds:
- subnet-abc123
RoleArn: arn:aws:iam::123456789012:role/EKSClusterRole
FargateProfile:
Type: AWS::EKS::FargateProfile
Properties:
ClusterName: !Ref EKSCluster
PodExecutionRoleArn: arn:aws:iam::123456789012:role/EKSPodRole
Selectors:
- Namespace: default

With this template, you can ensure consistency when spinning up multiple clusters.

Automating EKS Cluster Provisioning with AWS CloudFormation and Lambda

While CloudFormation helps define your infrastructure, AWS Lambda can automate the process of launching and managing it. Lambda functions are pieces of code that run in response to specific triggers or events, such as changes in your infrastructure.

For example, you can create a Lambda function that automatically updates your EKS cluster when a new version of Kubernetes is released. This ensures your infrastructure stays up-to-date without manual intervention. Automating routine infrastructure updates or tasks reduces the operational burden and makes your Kubernetes management more scalable. You don't have to worry about keeping your environment in sync — Lambda takes care of it for you.

The following steps will show you how to set up a Lambda function to automate parts of your infrastructure management:

  1. Go to the AWS Lambda console and create a new function.
  2. Define the function to trigger an update or create resources when changes occur in your CloudFormation stack.
  3. Use the AWS SDK in your Lambda function to interact with AWS services like EKS, CloudFormation, etc.

Benefits of using CloudFormation and Lambda

The combination of CloudFormation and Lambda allows for a powerful serverless automation pipeline. By relying on these tools, you achieve:

  • Reduced management overhead: With CloudFormation and Lambda, the entire lifecycle of your EKS infrastructure can be automated. CloudFormation defines the architecture, while Lambda automates updates, scaling, and maintenance. This reduces the need for manual operations like applying patches or upgrading Kubernetes versions, allowing your team to focus more on core application development rather than infrastructure tasks.
  • Consistency and reliability: By defining your infrastructure as code in CloudFormation templates, you ensure consistency across environments. Additionally, Lambda functions can automatically detect and correct any configuration drifts, ensuring that your infrastructure remains aligned with the defined specifications.
  • Cost-efficiency: Both CloudFormation and Lambda are serverless technologies, meaning you only pay for what you use. There's no need to maintain or manage EC2 instances just to handle infrastructure management. This serverless model ensures that you're not paying for idle compute resources.
  • Faster time to market: Automating the provisioning and updating of your EKS infrastructure means less time is spent on setup and configuration. You can deploy new environments rapidly without waiting for manual processes to be completed. This agility allows teams to experiment, launch, and scale their Kubernetes clusters faster, speeding up the development cycle and reducing downtime during infrastructure changes.
  • Improved Scalability: With CloudFormation defining your infrastructure, it's easy to replicate your EKS cluster setup as your application or team grows. And with Lambda, you can dynamically trigger scaling actions based on your application's performance metrics. Lambda functions can monitor usage and automatically provision additional resources when needed, ensuring your clusters are always optimized for performance.

Best Practices for Optimizing AWS Fargate with EKS

Now that you've successfully configured your EKS cluster to run on Fargate, the next step is to optimize your workloads for performance, scalability, and cost-efficiency. Below are some best practices and strategies to get the most out of Fargate with EKS.

Fine-tune workload placement

One of the great features of Fargate is the ability to fine-tune which workloads run on Fargate by configuring Fargate profiles. By specifying namespaces and labels, you can selectively decide which pods are placed on Fargate.

This ensures that high-priority, stateless, or scalable applications get the benefits of Fargate's serverless infrastructure, while less demanding workloads can continue running on EC2 instances, if needed. Make sure your workloads are properly tagged and organized within namespaces. This allows you to maximize the efficiency of Fargate by only running critical or performance-sensitive workloads in this environment.

Resource allocation and limits

To prevent performance bottlenecks and cost overruns, it's essential to configure resource requests and limits properly for your Kubernetes pods running on Fargate. When Kubernetes schedules a pod, it uses the resource requests to determine which nodes (in this case, Fargate) can handle the workload.

This is important because if you request too few resources, your pods might face throttling or experience downtime due to insufficient memory or CPU. Alternatively, requesting too many resources might result in overpaying for underutilized capacity.

By carefully analyzing your workload's performance, you can optimize your pods to request the exact amount of memory and CPU required, keeping costs in check while maintaining performance.

Use Horizontal Pod Autoscaling

Horizontal Pod Autoscaling (HPA) is crucial when running on Fargate, especially when dealing with fluctuating traffic or demand spikes. HPA automatically scales the number of pods in your Kubernetes cluster based on CPU, memory usage, or custom metrics.

The benefit here is simple: you don't have to worry about overprovisioning resources. Fargate will automatically scale up or down based on workload requirements, so you only pay for what you use, and your applications stay responsive even during high-traffic periods.

How to Monitor and Optimize Costs for AWS Fargate EKS Clusters

While Fargate saves time and effort on infrastructure management, it's important to continuously monitor your costs to avoid overspending. AWS provides tools like Cost Explorer and CloudWatch to track your Fargate usage.

Set up alerts and budgets to monitor your spending. CloudWatch can also be configured to notify you when certain cost thresholds are met, helping you maintain control over expenses while scaling your applications.

Consider using savings plans or reserved capacity options if you expect consistent usage of Fargate for long-running workloads. While Fargate is cost-efficient for on-demand scaling, pre-planning for predictable workloads can provide substantial savings.

Conclusion

In this article, we've explored how Fargate allows you to run Kubernetes pods without the burden of infrastructure management, ensuring scalability and cost-efficiency.

Combining this with AWS CloudFormation and Lambda gives you the flexibility to automate the entire provisioning process, streamlining your infrastructure workflows even further.

By following these steps and best practices, you can ensure that your Kubernetes clusters are not only easy to set up but also optimized for performance and cost-effectiveness.

Edge Stack API Gateway is a powerful, Kubernetes API management solution that handles routing, security, rate-limiting, and service discovery for your microservices. It simplifies managing API traffic, ensuring high availability, scalability, and security for serverless Kubernetes clusters. The gateway acts as a centralized layer for managing internal and external API requests, providing enhanced control over microservices in a dynamic, cloud-native environment.

Edge Stack API Gateway

Simplify your API management and ensure seamless traffic routing —optimize your Kubernetes microservices