how to set up EKS via terraform

Introduction

We can use terraform to set up a lot of infrastructure configuration. So I will set up EKS via terraform today.

Prerequisite

Terraform
AWS

Initialize project

Write main.tf and variables.tf files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}

provider "aws" {
region = "us-east-2"
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}
1
2
3
4
5
6
7
8
# variables.tf
variable "aws_access_key" {
type = string
}

variable "aws_secret_key" {
type = string
}

Then you can run terraform init to initialize your project.

Set up EKS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# eks.tf
resource "aws_iam_role" "eks_cluster" {
name = "eks-cluster"

assume_role_policy = jsondecode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Service" : "eks.amazonaws.com"
},
"Action" : "sts:AssumeRole"
}
]
})
}

resource "aws_iam_role_policy_attachment" "AmazonEKSClusterPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.eks_cluster.name
}

resource "aws_iam_role_policy_attachment" "AmazonEKSServicePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy"
role = aws_iam_role.eks_cluster.name
}

resource "aws_eks_cluster" "aws_eks" {
name = "my-cluster"
role_arn = aws_iam_role.eks_cluster.arn

vpc_config {
# change this to your subnet ids
subnet_ids = ["subnet-03425fce64e1d9cc5", "subnet-01765ab9b3f13dace"]
}

tags = {
name = "my-cluster"
}
}

resource "aws_iam_role" "eks_nodes" {
name = "eks-node-group"

assume_role_policy = jsondecode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Service" : "ec2.amazonaws.com"
},
"Action" : "sts:AssumeRole"
}
]
})
}

resource "aws_iam_role_policy_attachment" "CloudWatchAgentServerPolicy" {
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
role = aws_iam_role.eks_nodes.name
}

resource "aws_iam_role_policy_attachment" "AmazonEKSWorkerNodePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
role = aws_iam_role.eks_nodes.name
}

resource "aws_iam_role_policy_attachment" "AmazonEKS_CNI_Policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
role = aws_iam_role.eks_nodes.name
}

resource "aws_iam_role_policy_attachment" "AmazonEC2ContainerRegistryReadOnly" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
role = aws_iam_role.eks_nodes.name
}

resource "aws_eks_node_group" "node" {
cluster_name = aws_eks_cluster.aws_eks.name
node_group_name = "node"
node_role_arn = aws_iam_role.eks_nodes.arn
# change this to your subnet ids
subnet_ids = ["subnet-03425fce64e1d9cc5", "subnet-01765ab9b3f13dace"]
# you can change this to you want instance type
instance_types = ["t3.xlarge"]

# you can change desired_size for default node count
scaling_config {
desired_size = 3
max_size = 5
min_size = 1
}

# Ensure that IAM Role permissions are created before and deleted after EKS Node Group handling.
# Otherwise, EKS will not be able to properly delete EC2 Instances and Elastic Network Interfaces.
depends_on = [
aws_iam_role_policy_attachment.CloudWatchAgentServerPolicy,
aws_iam_role_policy_attachment.AmazonEKSWorkerNodePolicy,
aws_iam_role_policy_attachment.AmazonEKS_CNI_Policy,
aws_iam_role_policy_attachment.AmazonEC2ContainerRegistryReadOnly,
]
}

Then you can run terraform plan check something to change.
If any change is ok then you can run terraform apply to set up infrastructure.