How to create and manage EKS environments on AWS with Terraform
Setting up the environment on AWS is straightforward, but a few details need close attention. Here’s how to manage a staging Kubernetes cluster with Terraform.

Editorial translation by Impulso.
Developers know how hard it can be to create and manage environments on AWS. It takes a lot of patience and a thorough knowledge of the tools. That’s why I decided to show you a practical, efficient way to do it. But before we go through it step by step, we need to cover a few definitions.
Let’s start with Kubernetes (also known as K8s), which is an open-source container orchestration system. It automates the deployment, scaling, and management of containerized applications. It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation. It works with a variety of containerization tools, including Docker.
That brings us to EKS, a Kubernetes service managed by Amazon. There’s also another option called AWS Fargate, which is a serverless version of this service. Today, though, we’ll stick to standard worker nodes.
A few more concepts
Before we go on, there are a few more terms I need to explain. The first is Terraform. It’s an open-source infrastructure-as-code tool created by HashiCorp. It lets users define and provision data center infrastructure using a high-level configuration language known as Hashicorp Configuration Language or, optionally, JSON.
An EKS cluster is made up of two main components: the cluster and the worker groups.
- The cluster is the main service responsible for managing the worker nodes and worker groups;
- A worker node is a physical or virtual machine where containers are deployed and managed by pods. A pod, in turn, is a group of one or more containers with a configuration that specifies how they run, how many copies to run, and so on;
- A worker group, as the name suggests, is a group of worker nodes. On AWS, it can be managed or unmanaged.
The example
We’ll create an example project called PersonalERP. I developed it a few years ago and am now migrating it to K8s. We’ll create two environments for it: one for staging and testing and one for production. This is the simplest way to show how to create multiple environments by reusing Terraform code and taking advantage of its module structure.
Folder structure
This is the basic folder structure for the cluster we’ll create.

Here’s what each folder and module is for:
- terraform: this folder holds all the Terraform scripts, organized by environment;
- terraform\prd: this folder holds the initialization scripts for the production environment. It’s where we import the modules shared across environments;
- terraform\stg: this folder holds the initialization scripts for the staging environment. It’s where you import the modules shared across environments and set the parameters for creating the cluster;
- terraform\modules: this folder holds the project modules;
- terraform\modules\network: this is where we create the basic network infrastructure (VPC, subnets, route tables);
- terraform\modules\cluster: this is where we create the cluster and its roles;
- terraform\modules\worknodes: this is where we create the worker nodes.
The network module
To create the cluster, we need two things: a VPC (Virtual Private Cloud) and at least two subnets in at least two different availability zones. This module has three files: variables.tf, main.tf, and output.tf.
Variables
These are the variables the module needs. Notice that we define the cluster name in this first module because we’ll need it to create the VPC.
Output
Here we define the module’s “outputs.” These variables will be used in the other modules. Notice that we output the VPC ID, the subnet IDs, and the cluster name, all of which the other modules will need.
Main
This is the main file, where we create the VPC personalerp_private_vpc, its subnets, and an internet gateway. Also note the tags. They’re important for our EKS cluster, which is why we define the cluster name in this module.
Another important detail here is creating a route table. This allows traffic to flow between the subnets. The official documentation doesn’t mention this, so pay attention to this detail.
For the subnets, we use Terraform’s count to create three networks. Since we use a data source to get the available availability zones, Terraform will distribute the three subnets across the zones it finds. In Brazil, we have three availability zones (sa-east-1a, sa-east-1b, and sa-east-1c), so each will have one subnet.
This subnet requirement matters because the cluster won’t be created unless there are at least two subnets in two different availability zones.
The cluster module
This is the main module for the cluster itself. Along with the subnets we created earlier, it requires a role for the cluster with the following permissions: AmazonEKSClusterPolicy and AmazonEKSServicePolicy. Let’s take a closer look at the files in this module:
Variables
Once again, we start with the module’s variables. Notice that this time we’re receiving three variables from the previous network module’s outputs: vpc_id, eks_subnet_ids, and cluster_name.
IAM
In this file, we define the required role and permissions as described above.
Main
This is the main part of the cluster configuration. We set the name, version, and role we created earlier, tie everything to the subnets we created earlier, and explicitly declare the dependency on the roles. This dependency ensures Terraform sets up the required permissions before creating the cluster.
The WorkNodes module
Now it’s time to define our cluster’s worker nodes and worker groups.
Variables
Once again, we define the variables for our worker nodes. The variables eks_subnet_ids and cluster_name come from the network module we defined earlier.
- worknodes: the number of worker groups we’ll create. We’ll use a loop to create as many worker groups as we want;
- worknode_desired_size: the desired group size;
- worknode_max_size: the maximum group size;
- worknode_min_size: the minimum group size.
IAM
Here we define the role required for the worker nodes. The required permissions are AmazonEKSWorkerNodePolicy, AmazonEKS_CNI_Policy, and AmazonEC2ContainerRegistryReadOnly.
Main
This is the main file for the worker node groups, where we link the subnets to the cluster. We also specify the EC2 instance types (the instances will be created according to the scaling settings we choose). If your environment doesn’t need to create and delete pods all the time, I recommend setting all three sizes to the same value so the group starts and stays at a fixed size.
That covers all the basic modules for our cluster.
The STG (staging) environment
Now let’s create our staging environment (or testing environment, if you prefer). To do that, we need to use all the modules we created earlier and set the parameters for each environment.
First, we define the Terraform backend. This is what stores the state of your infrastructure. There are several providers for this, but we’ll use an Amazon S3 bucket. We also pin Terraform to the current version. That way, future Terraform updates won’t break our script.
Next, we declare the AWS provider, which the modules in this tutorial rely on.
Just below that, we use the first module we created: network. As specified in the variable declarations, it takes the tags variable as a parameter. This is the set of tags we’ll apply in our network module.
Next, we declare the cluster module and use a few values for this environment from the network module’s outputs, such as vpc_id, eks_subnet_ids, and cluster_name.
Finally, we declare the worknodes module, where we again use the output variables from the network module. Here we also set variables that control how our groups scale, such as the number of groups and their minimum, maximum, and desired sizes.
With that done, let’s go to our application’s terraforms/stg directory and get the application ready for the changes. In that directory, enter the following command:
terraform init
This initializes the S3 backend and downloads the declared providers. Then we’ll run terraform plan to see what Terraform plans to create or change in our infrastructure:
This output shows the resources Terraform will create:
Plan: 18 to add, 0 to change, 0 to destroy.
Conclusion
As we’ve seen, with this latest version, it’s relatively simple to create EKS environments on AWS with Terraform. You can download the project’s source code, add other features, and make the instance type configurable. The source code for this project is available at https://github.com/orlandoburli/personalerp-ops. Now it’s time to put all this into practice! If you’d like more tips like these, check out the other posts on our blog and don’t forget to follow Impulso on social media.


