AWS Builder Center

Automating Karpenter Installation on Amazon EKS with Bash Scripts

Helps to install Karpenter very easily using bash scripts.

We have been using the Kubernetes Cluster Autoscaler in our Amazon EKS clusters. Cluster Autoscaler depends on predefined Amazon EC2 Auto Scaling Groups, it can only scale from a fixed set of instance types and configurations. This reduced our ability to optimize costs, especially during periods of high demand, where scaling decisions were taken by the available node groups and provisioning could take longer than expected. These limitations triggered us to evaluate a more flexible and efficient solution.
Karpenter has become the preferred Kubernetes node autoscaler for Amazon EKS because it provisions the right compute resources based on pod requirements without depending on preconfigured Auto Scaling Groups. It launch the most suitable EC2 instance types in the real time, improve resource utilization, reduce provisioning time, and optimize infrastructure costs.
The Karpenter installing is very straightforward, configuring IAM roles, IAM policies, Helm charts, and NodePools across multiple environments can be repetitive and time-consuming. This process required a standardized deployment process or run book, there is a higher risk of configuration drift or deployment errors.
To simplify this process, I created three Bash scripts, that fully automate the Karpenter installation lifecycle for Amazon EKS.
Press enter or click to view image in full size
Automating Karpenter Installation on Amazon EKS with Bash Scripts
Pull the code to execute the bash script on each steps Git Repo:  https://github.com/bibincatchme/Karpenter-installation-with-bash-script 

Script 1 — Automating IAM Configuration

The first script creates all IAM resources required by Karpenter.

Validates prerequisites

The script checks:
  • Cluster name
  • AWS Account ID
  • Policy directory
  • Existing IAM roles
This prevents accidental deployment with incorrect configuration.

Automatically discovers cluster information

Instead of hard coding values, it retrieves:
  • AWS Region
  • AWS Partition
  • OIDC Provider
  • Cluster information

Creates the Controller IAM Role

The script dynamically generates the IRSA trust policy.
It configures the trust relationship with:
  • EKS OIDC Provider
  •  system:serviceaccount:kube-system:karpenter 
This allows the Karpenter controller pod to assume the IAM role securely.

Creates and Updates IAM Policies

Rather than maintaining static policies, the script:
  • Reads policy templates
  • Replaces environment variables using  envsubst 
  • Validates JSON using  jq 
  • Creates new policies
  • Updates existing policy versions
  • Attaches policies automatically
This makes policy management much easier when upgrading Karpenter.

Creates the Node IAM Role

The script also provisions the IAM role used by EC2 instances launched by Karpenter.
It attaches the required AWS managed policies:
  • AmazonEKSWorkerNodePolicy
  • AmazonEKS_CNI_Policy
  • AmazonEC2ContainerRegistryReadOnly
  • AmazonSSMManagedInstanceCore
At the end, it outputs both IAM Role ARNs for future use.
Execude the script

chmod +x karpenter-01.sh
bash -x ./karpenter-01.sh


Script 2 — Installing or Upgrading Karpenter

The second script installs a specific version of the Karpenter Helm deployment. Supported modes:
  •  dry 
  •  install 
  •  upgrade 

Dry Run

The dry mode renders the Helm manifests without installing them.
This is useful for:
  • Reviewing manifests
  • GitOps workflows
  • CI/CD validation
  • Security reviews

Installation

Install mode performs:
  • Namespace creation
  • Helm installation
  • Waits for deployment completion
It automatically injects:
  • Cluster name
  • Controller IAM Role ARN
  • Resource requests
  • Resource limits
  • Service Account annotations

Upgrade

Upgrade mode uses the same configuration to perform an in-place Helm upgrade while preserving existing resources.

Controller Scheduling Protection

One useful addition is the node affinity configuration.
The controller is configured so it never schedules itself onto Karpenter-managed nodes. This avoids situations where Karpenter terminates the node that is running its own controller.
Execude the script

chmod +x karpenter-02.sh
bash -x ./karpenter-02.sh install

Script 3 — Creating EC2NodeClass and NodePools

The final script handles infrastructure provisioning for Karpenter.
This includes:
  • Resource discovery
  • AWS tagging
  • EC2NodeClass creation
  • NodePool generation
  • Applying manifests

Environment-Aware Configuration

The script supports multiple clusters with environment-specific settings.
For example: Testing
  • Small instances
  • Reduced node limits
Development
  • Smaller instance types
  • Lower CPU limits
  • Lower memory limits
Production
  • High-performance compute instances
  • Dedicated memory node pools
  • Higher scaling limits
Each environment can have its own:
  • Instance types class
  • Scaling limits
  • Labels
  • Resource quotas

Automatic Resource Discovery

The script automatically discovers:
  • Cluster Security Group
  • Cluster Subnets
It then applies the required discovery tags:
karpenter.sh/discovery=<cluster-name>
These tags allow Karpenter to identify networking resources automatically.

EC2NodeClass Generation

The script dynamically generates an EC2NodeClass including:
  • AL2023 AMI
  • IAM Role
  • Subnet selectors
  • Security Group selectors
  • Cluster tags
No manual YAML editing is required.

Multiple NodePools

The script creates two dedicated NodePools to handle different workload characteristics while giving Karpenter the flexibility to select the most appropriate instance size based on pending pod resource requests.

Compute NodePool

The Compute NodePool is dedicated for CPU-intensive workloads, such as application servers, APIs, and etc.
Instead of restricting Karpenter on a single EC2 instance type, the script allows multiple instance sizes within the same instance family. This enables Karpenter to provision the smallest or largest instance required based on the workload.
Production Example
  • c7i.2xlarge
  • c7i.4xlarge
  • c7i.8xlarge
Development Example
  • c5.xlarge
  • c5.2xlarge
  • c5.4xlarge
Testing Example
  • t3.medium
  • t3.large
  • t3.xlarge

Memory NodePool

The Memory NodePool is intended for memory-intensive workloads, such as caching services, data processing applications, and workloads that require higher memory-to-vCPU ratios.
Similar to the Compute NodePool, multiple instance sizes are defined within the same family, allowing Karpenter to dynamically provision the most suitable node.
Production Example
  • m7i.2xlarge
  • m7i.4xlarge
  • m7i.8xlarge
Development Example
  • t3.xlarge
  • t3.2xlarge
Testing Example
  • t3.medium
  • t3.large
  • t3.xlarge

Supported Modes

The script supports four execution modes.

Dry

Generate the YAML only.

Tag

Apply AWS discovery tags without deploying Kubernetes resources.

Apply

  • Tag AWS resources
  • Deploy EC2NodeClass
  • Deploy NodePools

Delete

Remove all generated Karpenter resources from the cluster.

End-to-End Workflow

The three scripts together provides a complete automation workflow:
  1. Create IAM roles and policies.
  1. Install or upgrade the Karpenter controller using Helm.
  1. Tag AWS networking resources.
  1. Generate EC2NodeClass and NodePools.
  1. Deploy Karpenter resources to the cluster.
This approach significantly reduces the manual effort involved in setting up Karpenter and ensures consistent deployments across multiple EKS environments.
Execude the script

chmod +x karpenter-03.sh
bash -x ./karpenter-03.sh apply

Benefits of This Automation

Some key advantages include:
  • Fully automated Karpenter installation
  • Consistent deployments across environments
  • Eliminates repetitive manual IAM configuration
  • Supports Helm dry-run, install, and upgrade workflows
  • Automatically discovers and tags AWS networking resources
  • Generates environment-specific NodePools
  • Reduces configuration drift
  • Simplifies Karpenter upgrades

Conclusion

By automating the Karpenter installation with Bash scripts helps on IAM provisioning, Helm deployment/upgrade, and NodePool creation with minimal effort.
You can further extend these scripts to support Spot instances, GPU workloads, custom AMIs, taints, startup taints, or additional NodePools, making them a solid foundation for production-ready Karpenter deployments.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article