Skip to content

Commit c8c271b

Browse files
authored
Merge pull request #44 from arunlalp/master
[Feat] Added EKS Module
2 parents d016270 + bd43305 commit c8c271b

File tree

7 files changed

+432
-0
lines changed

7 files changed

+432
-0
lines changed

infra/eks-cluster/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
provider "aws" {
2+
region = var.region
3+
}
4+
5+
module "eks-cluster" {
6+
source = "../modules/eks"
7+
cluster_name = var.cluster_name
8+
role_name = var.role_name
9+
vpc_subnets = var.vpc_subnets
10+
node_group_name = var.node_group_name
11+
node_instance_type = var.node_instance_type
12+
node_disk_size = var.node_disk_size
13+
policy_arns = var.policy_arns
14+
eks_addons = var.eks_addons
15+
principal_arn = var.principal_arn
16+
kubernetes_groups = var.kubernetes_groups
17+
access_policy_arn = var.access_policy_arn
18+
}

infra/eks-cluster/outputs.tf

Whitespace-only changes.

infra/eks-cluster/variables.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
variable "region" {
2+
type = string
3+
description = "Region of the EC2 instance"
4+
}
5+
6+
variable "cluster_name" {
7+
description = "The name of the EKS cluster"
8+
type = string
9+
}
10+
11+
variable "role_name" {
12+
description = "Name of the IAM role for EKS"
13+
type = string
14+
}
15+
16+
variable "vpc_subnets" {
17+
description = "List of VPC subnet IDs"
18+
type = list(string)
19+
}
20+
21+
variable "node_group_name" {
22+
description = "The name of the node group"
23+
type = string
24+
}
25+
26+
variable "node_instance_type" {
27+
description = "EC2 instance type for the node group"
28+
type = list(string)
29+
}
30+
31+
variable "node_disk_size" {
32+
description = "Disk size for the node group instances"
33+
type = number
34+
}
35+
36+
variable "policy_arns" {
37+
description = "List of IAM policy ARNs to attach to the roles"
38+
type = list(string)
39+
}
40+
41+
variable "eks_addons" {
42+
description = "List of EKS addons and their versions"
43+
type = map(string)
44+
}
45+
46+
variable "principal_arn" {
47+
description = "The ARN of the principal"
48+
type = string
49+
}
50+
51+
variable "kubernetes_groups" {
52+
description = "Kubernetes groups"
53+
type = list(string)
54+
}
55+
56+
variable "access_policy_arn" {
57+
description = "The ARN of the access policy"
58+
type = string
59+
}

modules/eks/main.tf

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
resource "aws_eks_cluster" "eks_cluster" {
2+
name = var.cluster_name
3+
role_arn = aws_iam_role.cluster_role.arn
4+
5+
vpc_config {
6+
subnet_ids = var.vpc_subnets
7+
endpoint_private_access = true
8+
endpoint_public_access = true
9+
}
10+
11+
access_config {
12+
authentication_mode = "API_AND_CONFIG_MAP"
13+
bootstrap_cluster_creator_admin_permissions = true
14+
}
15+
16+
depends_on = [
17+
aws_iam_role_policy_attachment.AmazonEKSClusterPolicy,
18+
aws_iam_role_policy_attachment.AmazonEKSVPCResourceController,
19+
]
20+
}
21+
22+
output "endpoint" {
23+
value = aws_eks_cluster.eks_cluster.endpoint
24+
}
25+
26+
output "kubeconfig-certificate-authority-data" {
27+
value = aws_eks_cluster.eks_cluster.certificate_authority[0].data
28+
}
29+
30+
data "aws_iam_policy_document" "assume_role" {
31+
statement {
32+
effect = "Allow"
33+
34+
principals {
35+
type = "Service"
36+
identifiers = ["eks.amazonaws.com"]
37+
}
38+
39+
actions = ["sts:AssumeRole"]
40+
}
41+
}
42+
43+
resource "aws_iam_role" "cluster_role" {
44+
name = var.role_name
45+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
46+
}
47+
48+
resource "aws_iam_role_policy_attachment" "AmazonEKSClusterPolicy" {
49+
policy_arn = var.policy_arns[0]
50+
role = aws_iam_role.cluster_role.name
51+
}
52+
53+
resource "aws_iam_role_policy_attachment" "AmazonEKSVPCResourceController" {
54+
policy_arn = var.policy_arns[1]
55+
role = aws_iam_role.cluster_role.name
56+
}
57+
58+
data "tls_certificate" "tls_cert" {
59+
url = aws_eks_cluster.eks_cluster.identity[0].oidc[0].issuer
60+
}
61+
62+
resource "aws_iam_openid_connect_provider" "eks_oidc_provider" {
63+
client_id_list = ["sts.amazonaws.com"]
64+
thumbprint_list = [data.tls_certificate.tls_cert.certificates[0].sha1_fingerprint]
65+
url = data.tls_certificate.tls_cert.url
66+
}
67+
68+
data "aws_iam_policy_document" "assume_role_policy" {
69+
statement {
70+
actions = ["sts:AssumeRoleWithWebIdentity"]
71+
effect = "Allow"
72+
73+
condition {
74+
test = "StringEquals"
75+
variable = "${replace(aws_iam_openid_connect_provider.eks_oidc_provider.url, "https://", "")}:sub"
76+
values = ["system:serviceaccount:kube-system:aws-node"]
77+
}
78+
79+
principals {
80+
identifiers = [aws_iam_openid_connect_provider.eks_oidc_provider.arn]
81+
type = "Federated"
82+
}
83+
}
84+
}
85+
86+
resource "aws_eks_addon" "coredns" {
87+
cluster_name = aws_eks_cluster.eks_cluster.name
88+
addon_name = "coredns"
89+
addon_version = var.eks_addons["coredns"]
90+
resolve_conflicts_on_update = "PRESERVE"
91+
}
92+
93+
resource "aws_iam_role" "vpc_cni_role" {
94+
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
95+
name = "vpc-cni-role"
96+
}
97+
98+
resource "aws_iam_role_policy_attachment" "vpc_cni_policy" {
99+
policy_arn = var.policy_arns[3]
100+
role = aws_iam_role.vpc_cni_role.name
101+
}
102+
103+
resource "aws_eks_addon" "vpc-cni" {
104+
cluster_name = aws_eks_cluster.eks_cluster.name
105+
addon_name = "vpc-cni"
106+
addon_version = var.eks_addons["vpc-cni"]
107+
resolve_conflicts_on_update = "PRESERVE"
108+
service_account_role_arn = aws_iam_role.vpc_cni_role.arn
109+
}
110+
111+
resource "aws_eks_addon" "kube-proxy" {
112+
cluster_name = aws_eks_cluster.eks_cluster.name
113+
addon_name = "kube-proxy"
114+
addon_version = var.eks_addons["kube-proxy"]
115+
resolve_conflicts_on_update = "PRESERVE"
116+
}
117+
118+
resource "aws_eks_addon" "eks-pod-identity-agent" {
119+
cluster_name = aws_eks_cluster.eks_cluster.name
120+
addon_name = "eks-pod-identity-agent"
121+
addon_version = var.eks_addons["eks-pod-identity-agent"]
122+
resolve_conflicts_on_update = "PRESERVE"
123+
}
124+
125+
resource "aws_eks_node_group" "node_group" {
126+
cluster_name = aws_eks_cluster.eks_cluster.name
127+
node_group_name = var.node_group_name
128+
version = aws_eks_cluster.eks_cluster.version
129+
node_role_arn = aws_iam_role.node-group-iam-role.arn
130+
subnet_ids = var.vpc_subnets
131+
capacity_type = "ON_DEMAND"
132+
disk_size = var.node_disk_size
133+
instance_types = var.node_instance_type
134+
135+
scaling_config {
136+
desired_size = 1
137+
max_size = 2
138+
min_size = 1
139+
}
140+
141+
update_config {
142+
max_unavailable = 1
143+
}
144+
145+
depends_on = [
146+
aws_iam_role_policy_attachment.AmazonEKSWorkerNodePolicy,
147+
aws_iam_role_policy_attachment.AmazonEKS_CNI_Policy,
148+
aws_iam_role_policy_attachment.AmazonEC2ContainerRegistryReadOnly,
149+
]
150+
}
151+
152+
resource "aws_iam_role" "node-group-iam-role" {
153+
name = "eks-node-group-role"
154+
155+
assume_role_policy = jsonencode({
156+
Statement = [{
157+
Action = "sts:AssumeRole"
158+
Effect = "Allow"
159+
Principal = {
160+
Service = "ec2.amazonaws.com"
161+
}
162+
}]
163+
Version = "2012-10-17"
164+
})
165+
}
166+
167+
resource "aws_iam_role_policy_attachment" "AmazonEKSWorkerNodePolicy" {
168+
policy_arn = var.policy_arns[2]
169+
role = aws_iam_role.node-group-iam-role.name
170+
}
171+
172+
resource "aws_iam_role_policy_attachment" "AmazonEKS_CNI_Policy" {
173+
policy_arn = var.policy_arns[3]
174+
role = aws_iam_role.node-group-iam-role.name
175+
}
176+
177+
resource "aws_iam_role_policy_attachment" "AmazonEC2ContainerRegistryReadOnly" {
178+
policy_arn = var.policy_arns[4]
179+
role = aws_iam_role.node-group-iam-role.name
180+
}
181+
182+
resource "aws_eks_access_entry" "access_entry" {
183+
cluster_name = aws_eks_cluster.eks_cluster.name
184+
principal_arn = var.principal_arn
185+
kubernetes_groups = var.kubernetes_groups
186+
type = "STANDARD"
187+
}
188+
189+
resource "aws_eks_access_policy_association" "access_association" {
190+
cluster_name = aws_eks_cluster.eks_cluster.name
191+
policy_arn = var.access_policy_arn
192+
principal_arn = aws_eks_access_entry.access_entry.principal_arn
193+
194+
access_scope {
195+
type = "cluster"
196+
}
197+
}
198+

modules/eks/outputs.tf

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
output "cluster_name" {
2+
description = "The name of the EKS cluster"
3+
value = aws_eks_cluster.eks_cluster.name
4+
}
5+
6+
output "cluster_endpoint" {
7+
description = "The endpoint of the EKS cluster"
8+
value = aws_eks_cluster.eks_cluster.endpoint
9+
}
10+
11+
output "cluster_certificate_authority_data" {
12+
description = "The certificate authority data for the cluster"
13+
value = aws_eks_cluster.eks_cluster.certificate_authority[0].data
14+
}
15+
16+
output "cluster_arn" {
17+
description = "The ARN of the EKS cluster"
18+
value = aws_eks_cluster.eks_cluster.arn
19+
}
20+
21+
output "oidc_provider_arn" {
22+
description = "The ARN of the OIDC provider"
23+
value = aws_iam_openid_connect_provider.eks_oidc_provider.arn
24+
}
25+
26+
output "node_group_name" {
27+
description = "The name of the EKS node group"
28+
value = aws_eks_node_group.node_group.node_group_name
29+
}
30+
31+
output "node_group_instance_types" {
32+
description = "The instance types used in the EKS node group"
33+
value = aws_eks_node_group.node_group.instance_types
34+
}
35+
36+
output "node_group_disk_size" {
37+
description = "The disk size for the EKS node group instances"
38+
value = aws_eks_node_group.node_group.disk_size
39+
}
40+
41+
output "iam_role_name" {
42+
description = "The name of the IAM role used for the EKS cluster"
43+
value = aws_iam_role.cluster_role.name
44+
}
45+
46+
output "vpc_subnets" {
47+
description = "The VPC subnets used by the EKS cluster"
48+
value = aws_eks_cluster.eks_cluster.vpc_config[0].subnet_ids
49+
}
50+
51+
output "coredns_addon_version" {
52+
description = "The version of the CoreDNS addon"
53+
value = aws_eks_addon.coredns.addon_version
54+
}
55+
56+
output "vpc_cni_addon_version" {
57+
description = "The version of the VPC CNI addon"
58+
value = aws_eks_addon.vpc-cni.addon_version
59+
}
60+
61+
output "kube_proxy_addon_version" {
62+
description = "The version of the kube-proxy addon"
63+
value = aws_eks_addon.kube-proxy.addon_version
64+
}
65+
66+
output "eks_pod_identity_agent_addon_version" {
67+
description = "The version of the EKS Pod Identity Agent addon"
68+
value = aws_eks_addon.eks-pod-identity-agent.addon_version
69+
}
70+
71+
output "access_entry_principal_arn" {
72+
description = "The ARN of the principal for the access entry"
73+
value = aws_eks_access_entry.access_entry.principal_arn
74+
}
75+
76+
output "access_policy_arn" {
77+
description = "The ARN of the access policy associated with the EKS cluster"
78+
value = aws_eks_access_policy_association.access_association.policy_arn
79+
}

0 commit comments

Comments
 (0)