Deploy an App with a Basic Ingress Service on AWS EKS Using Kubernetes and Terraform

Ammar Suhail
5 min readJul 10, 2023

--

Step 1: Set up the AWS EKS Cluster

  1. Create an Amazon EKS cluster using the AWS Management Console or AWS CLI. For example, using AWS CLI:

Create a Kubernetes service manifest (e.g., service.yaml) to expose your application within the cluster. Here's an example:

Step 3: Set Up an Ingress Controller

Deploy an Ingress controller to handle incoming requests and route them to the appropriate services. You can use Nginx Ingress Controller or AWS ALB Ingress Controller. Here, we’ll use Nginx Ingress Controller. Apply the following manifest (e.g., nginx-ingress-controller.yaml):

Create an Ingress resource manifest (e.g., ingress.yaml) to define the rules for routing incoming traffic to your services. Here's an example:

Step 4: Create Terraform Configuration

Step 5: Apply the Terraform Configuration

Certainly! Here’s an example that demonstrates how to deploy an app with a basic Ingress service on AWS EKS using Kubernetes and Terraform manifests.

Step 1: Set up the AWS EKS Cluster

  1. Create an Amazon EKS cluster using the AWS Management Console or AWS CLI. For example, using AWS CLI:
aws eks create-cluster --name my-cluster --role-arn <eks-service-role-arn> --resources-vpc-config subnetIds=<subnet-ids>,securityGroupIds=<security-group-ids>
  1. Configure your local machine to interact with the EKS cluster using the AWS CLI. For example:
aws eks update-kubeconfig --name my-cluste

Step 2: Prepare the Kubernetes Manifests

  1. Create a Kubernetes deployment manifest (e.g., deployment.yaml) for your application. Here's an example for a simple NGINX deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
  1. Create a Kubernetes service manifest (e.g., service.yaml) to expose your application within the cluster. Here's an example:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP

Step 3: Set Up an Ingress Controller

  1. Deploy an Ingress controller to handle incoming requests and route them to the appropriate services. You can use Nginx Ingress Controller or AWS ALB Ingress Controller. Here, we’ll use Nginx Ingress Controller. Apply the following manifest (e.g., nginx-ingress-controller.yaml):
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-ingress-controller
spec:
replicas: 1
selector:
matchLabels:
app: nginx-ingress-controller
template:
metadata:
labels:
app: nginx-ingress-controller
spec:
containers:
- name: nginx-ingress-controller
image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.32.0
args:
- /nginx-ingress-controller
- --configmap=$(POD_NAMESPACE)/nginx-configuration
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
- --udp-services-configmap=$(POD_NAMESPACE)/udp-services
- --publish-service=$(POD_NAMESPACE)/nginx-ingress-controller
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
---
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress-controller
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: nginx-ingress-controller
  1. Create an Ingress resource manifest (e.g., ingress.yaml) to define the rules for routing incoming traffic to your services. Here's an example:
yamlapiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80

Step 4: Create Terraform Configuration

  1. Create a Terraform file (e.g., main.tf) with the following content:
provider "aws" {
region = "us-west-2" # Replace with your desired AWS region
}
resource "aws_vpc" "eks_vpc" {
cidr_block = "10.0.0.0/16" # Replace with your desired VPC CIDR
enable_dns_hostnames = true
}
resource "aws_subnet" "eks_subnet" {
vpc_id = aws_vpc.eks_vpc.id
cidr_block = "10.0.0.0/24" # Replace with your desired subnet CIDR
}
resource "aws_security_group" "eks_cluster_sg" {
vpc_id = aws_vpc.eks_vpc.id
# Define your security group rules as per your requirements
}
resource "aws_eks_cluster" "my_cluster" {
name = "my-cluster"
role_arn = "<eks-service-role-arn>"
vpc_config {
subnet_ids = [aws_subnet.eks_subnet.id]
security_group_ids = [aws_security_group.eks_cluster_sg.id]
}
}
provider "kubernetes" {
config_path = "~/.kube/config" # Replace with the path to your Kubernetes config file
}
resource "kubernetes_namespace" "app_namespace" {
metadata {
name = "my-app"
}
}
resource "kubernetes_deployment" "app_deployment" {
metadata {
name = "nginx-deployment"
namespace = kubernetes_namespace.app_namespace.metadata[0].name
}
spec {
replicas = 3
selector {
match_labels = {
app = "nginx"
}
}
template {
metadata {
labels = {
app = "nginx"
}
}
spec {
container {
name = "nginx"
image = "nginx:latest"
port {
container_port = 80
}
}
}
}
}
}
resource "kubernetes_service" "app_service" {
metadata {
name = "nginx-service"
namespace = kubernetes_namespace.app_namespace.metadata[0].name
}
spec {
selector = {
app = "nginx"
}
port {
protocol = "TCP"
port = 80
target_port = 80
}
type = "ClusterIP"
}
}
resource "kubernetes_ingress" "app_ingress" {
metadata {
name = "app-ingress"
namespace = kubernetes_namespace.app_namespace.metadata[0].name
annotations = {
"kubernetes.io/ingress.class" = "nginx"
}
}
spec {
rule {
host = "example.com"
http {
path {
path = "/"
pathType = "Prefix"
}
backend {
service_name = kubernetes_service.app_service.metadata[0].name
service_port = kubernetes_service.app_service.spec[0].port[0].port
}
}
}
}
}
Initializethe Terraform working directory by running terraform init
terraform validate

Deploy the Application

Use the Kubernetes configuration file generated by Terraform to set the KUBECONFIG environment variable:

export KUBECONFIG=<path-to-kubeconfig-file>
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml

Verify that the application is running and accessible by checking the Ingress address and accessing it in a web browser.

That’s it! You have successfully deployed an app with a basic Ingress service on AWS EKS using Kubernetes and Terraform manifests. Remember to clean up the resources when you’re done to avoid incurring unnecessary costs.

--

--

Ammar Suhail
Ammar Suhail

Written by Ammar Suhail

DevOps/ Cloud Engineer/AWS Certified/ CKAD Certified/ Terraform Certified/M.S Electrical & Computer Engineering (USA)

Responses (2)