0% found this document useful (0 votes)
26 views7 pages

Deploying An EKS Cluster With An ALB and NGINX Using Terraform

Uploaded by

phillas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Deploying An EKS Cluster With An ALB and NGINX Using Terraform

Uploaded by

phillas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Guide: Deploying an EKS Cluster with an ALB and NGINX using

Terraform

1. Introduction
In this guide, we will walk through the process of deploying an Amazon EKS (Elastic
Kubernetes Service) cluster using Terraform. We will also set up a Kubernetes Node Group,
an Application Load Balancer (ALB), and deploy a sample NGINX application to test the
setup.

Amazon EKS provides a managed Kubernetes service that simplifies running Kubernetes on
AWS without needing to manage the control plane. We will use Terraform to automate the
infrastructure deployment.

2. Prerequisites
Before starting, ensure that you have the following:

- AWS Account: An AWS account with necessary permissions to create EKS, ALB, and
related resources.
- Terraform Installed: Version 1.0 or above. You can download it from the official Terraform
website.
- AWS CLI Installed: To interact with AWS services from the terminal. You can download it
here.
- kubectl Installed: A command-line tool to interact with Kubernetes clusters. You can
install it following the instructions here.
- IAM Role Permissions: Ensure the necessary IAM roles are created for EKS and ALB to
access other AWS resources.

3. Step-by-Step Explanation of the Terraform File

a. AWS Provider Configuration

# Configure AWS provider


provider "aws" {
region = "us-east-1" # Replace with your desired AWS region
}

- provider "aws": Specifies the AWS provider to interact with AWS services.
- region: The AWS region where the resources will be deployed. You can replace `"us-east-
1"` with your preferred region.

b. Creating the EKS Cluster

# Create EKS cluster


resource "aws_eks_cluster" "example" {
name = "my-eks-cluster" # Replace with your desired EKS cluster
name
version = "1.26" # Replace with your desired EKS version

vpc_config {
subnet_ids = [
"subnet-12345678", # Replace with your own subnet ID
"subnet-87654321" # Replace with your own subnet ID
]
security_group_ids = [
"sg-12345678" # Replace with your own security group ID
]
}
}
- resource "aws_eks_cluster": Defines an EKS cluster.
- name: Name of the EKS cluster. Replace `"my-eks-cluster"` with your desired cluster
name.
- version: Specifies the Kubernetes version to use. Update it based on your requirements.
- vpc_config: Configuration for the EKS VPC, including subnet_ids (subnets where the
cluster will be placed) and security_group_ids (which security group the cluster will use).

c. Creating the Kubernetes Node Group

# Create Kubernetes Node Group


resource "aws_eks_node_group" "example" {
cluster_name = aws_eks_cluster.example.name
node_group_name = "my-node-group" # Replace with your desired node
group name
desired_size = 2 # Adjust desired number of nodes
min_size = 1 # Adjust minimum number of nodes
max_size = 4 # Adjust maximum number of nodes
subnet_ids = [
"subnet-12345678", # Replace with your own subnet ID
"subnet-87654321" # Replace with your own subnet ID
]
instance_types = ["t2.medium"] # Replace with your desired
instance type
ami_type = "AL2_x86_64" # Replace with your desired AMI
type (AL2_x86_64 or AL2_ARM_64)
labels = {
"eks.amazonaws.com/nodegroup" = "my-node-group" # Replace with your
own labels if necessary
}
}

- resource "aws_eks_node_group": Defines a node group for the EKS cluster.


- cluster_name: Links the node group to the EKS cluster created earlier.
- desired_size, min_size, max_size: Specifies the size of the node group (number of nodes
in the Kubernetes cluster).
- instance_types: The EC2 instance type to use for the nodes. `"t2.medium"` is used here
but can be replaced with larger or smaller instance types.
- ami_type: The AMI type for the nodes. Use `AL2_x86_64` for Amazon Linux 2-based
nodes.

d. Creating an Application Load Balancer (ALB)

# Create Application Load Balancer (ALB)


resource "aws_lb" "example" {
name = "my-alb" # Replace with your desired ALB
name
load_balancer_type = "application" # Choose ALB type
scheme = "internet-facing" # Choose ALB scheme (internal
or internet-facing)
security_groups = ["sg-12345678"] # Replace with your own security
group ID
subnets = ["subnet-12345678", "subnet-87654321"] # Replace
with your own subnet IDs
}

- resource "aws_lb": Defines an ALB.


- name: Name of the ALB. Replace with your desired name.
- load_balancer_type: Set to `"application"` for an ALB.
- scheme: Defines whether the ALB is internet-facing or internal.
- security_groups: The security group associated with the ALB.
- subnets: The subnets in which the ALB will be deployed.

e. Creating ALB Listener

# Create Listener for ALB


resource "aws_lb_listener" "example" {
load_balancer_arn = aws_lb.example.arn
port = 80 # Replace with your desired
listener port
protocol = "HTTP" # Use "HTTP" or "HTTPS" based
on your setup
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.example.arn
}
}

- resource "aws_lb_listener": Sets up a listener for the ALB.


- port: Port 80 is used for HTTP traffic.
- default_action: Defines the default action when traffic hits the listener. In this case, it
forwards traffic to the target group.

f. Creating ALB Target Group

# Create Target Group for ALB


resource "aws_lb_target_group" "example" {
name = "my-target-group" # Replace with your desired target
group name
port = 80 # Replace with your desired target
group port
protocol = "HTTP" # Use "HTTP" or "HTTPS" based on your
setup
target_type = "ip" # Adjust based on whether targets are
instances or IP addresses
vpc_id = "vpc-12345678" # Replace with your VPC ID
}

- resource "aws_lb_target_group": Defines a target group for the ALB.


- target_type: Can be set to `instance` or `ip`. Here, `ip` is used to target IP addresses.
- vpc_id: The VPC in which the target group operates.

g. Deploying NGINX Application

# Example of how to deploy a Kubernetes application (if required)


# You can replace this with actual Kubernetes deployment code (Helm,
kubectl, etc.)
resource "kubernetes_deployment" "nginx" {
metadata {
name = "nginx" # Replace with your application name
labels = {
app = "nginx"
}
}
spec {
replicas = 3 # Replace with the number of desired replicas
selector {
match_labels = {
app = "nginx"
}
}
template {
metadata {
labels = {
app = "nginx"
}
}
spec {
container {
image = "nginx:latest" # Replace with your desired container
image
name = "nginx"
ports {
container_port = 80 # Replace with your desired container
port
}
}
}
}
}
}

- resource "kubernetes_deployment": Defines a Kubernetes deployment.


- name: The name of the deployment.
- replicas: Specifies the number of NGINX instances.
- image: The container image for NGINX.

4. Deploying the Terraform File


1. Initialise the Terraform project:

terraform init

2. Preview the changes that Terraform will apply:

terraform plan

3. Apply the Terraform configuration:

terraform apply

Confirm the prompt by typing `yes`.

5. Accessing the EKS Cluster


Once the EKS cluster is deployed, configure `kubectl` to interact with the cluster:
1. Update your local kubeconfig with the EKS cluster information:

aws eks --region us-east-1 update-kubeconfig --name my-eks-cluster

2. Verify the connection to the cluster:

kubectl get nodes

6. Testing the EKS and ALB by Deploying a Sample NGINX Application


1. After the Terraform deployment is complete, use `kubectl` to verify the NGINX
deployment:

kubectl get deployments


2. Ensure the service is exposed via the ALB by checking the status of the load balancer:

kubectl get services

3. Access the NGINX application using the ALB DNS.

7. Conclusion
In this guide, we successfully deployed an Amazon EKS cluster with an Application Load
Balancer using Terraform. We also deployed a sample NGINX application to test the setup.
You can further extend this configuration by deploying more complex applications or
integrating CI/CD pipelines.

You might also like