Skip to content

Commit 2d90684

Browse files
committed
refactor: modularize Terraform IAM resources properly
- Add IAM module to main.tf composition - Remove IAM role creation from EKS module - EKS module now accepts cluster_role_arn and node_role_arn - Add environment variable for multi-environment support - Remove unused eks_role_name and node_role_name variables - Follow single responsibility principle: IAM module owns all IAM resources - Improve module reusability and separation of concerns
1 parent 1bb08fb commit 2d90684

4 files changed

Lines changed: 56 additions & 134 deletions

File tree

infra/main.tf

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,36 @@ provider "aws" {
1919
region = var.region
2020
}
2121

22+
module "iam" {
23+
source = "./modules/iam"
24+
25+
prefix = var.cluster_name
26+
tags = {
27+
Environment = var.environment
28+
ManagedBy = "Terraform"
29+
}
30+
}
31+
2232
module "vpc" {
2333
source = "./modules/vpc"
2434

2535
vpc_cidr = var.vpc_cidr
2636
subnet_1_cidr = var.subnet_1_cidr
2737
subnet_2_cidr = var.subnet_2_cidr
28-
environment = "dev"
38+
environment = var.environment
2939
cluster_name = var.cluster_name
3040
}
3141

3242
module "eks" {
3343
source = "./modules/eks"
3444

35-
vpc_id = module.vpc.vpc_id
36-
subnet_ids = module.vpc.subnet_ids
37-
cluster_name = var.cluster_name
38-
node_group_name = var.node_group_name
39-
eks_role_name = var.eks_role_name
40-
node_role_name = var.node_role_name
41-
desired_size = var.desired_size
42-
min_size = var.min_size
43-
max_size = var.max_size
45+
vpc_id = module.vpc.vpc_id
46+
subnet_ids = module.vpc.subnet_ids
47+
cluster_name = var.cluster_name
48+
node_group_name = var.node_group_name
49+
cluster_role_arn = module.iam.cluster_role_arn
50+
node_role_arn = module.iam.node_role_arn
51+
desired_size = var.desired_size
52+
min_size = var.min_size
53+
max_size = var.max_size
4454
}

infra/modules/eks/main.tf

Lines changed: 14 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,142 +8,60 @@ terraform {
88
}
99
}
1010

11-
# Create an IAM role for EKS
12-
resource "aws_iam_role" "eks_role" {
13-
name = var.eks_role_name
14-
15-
assume_role_policy = jsonencode({
16-
Version = "2012-10-17"
17-
Statement = [
18-
{
19-
Effect = "Allow"
20-
Principal = {
21-
Service = "eks.amazonaws.com"
22-
}
23-
Action = "sts:AssumeRole"
24-
}
25-
]
26-
})
27-
}
28-
29-
# Attach the required policies to the IAM role
30-
resource "aws_iam_role_policy_attachment" "eks_cluster_policy" {
31-
role = aws_iam_role.eks_role.name
32-
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
33-
}
34-
35-
resource "aws_iam_role_policy_attachment" "eks_service_policy" {
36-
role = aws_iam_role.eks_role.name
37-
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy"
38-
}
39-
4011
# Create a security group for EKS
4112
resource "aws_security_group" "eks_sg" {
4213
name = var.eks_sg_name
4314
description = "Security group for EKS cluster"
4415
vpc_id = var.vpc_id
4516

46-
# Allow inbound HTTPS traffic for Kubernetes API server
4717
ingress {
4818
from_port = 443
4919
to_port = 443
5020
protocol = "tcp"
5121
cidr_blocks = [var.allowed_cidr_block]
52-
}
53-
54-
# Allow Kubelet API
55-
ingress {
56-
from_port = 10250
57-
to_port = 10250
58-
protocol = "tcp"
59-
self = true
60-
}
61-
62-
# Allow NodePort Services
63-
ingress {
64-
from_port = 30000
65-
to_port = 32767
66-
protocol = "tcp"
67-
cidr_blocks = [var.allowed_cidr_block]
22+
description = "Allow HTTPS traffic for Kubernetes API server"
6823
}
6924

7025
egress {
7126
from_port = 0
7227
to_port = 0
7328
protocol = "-1"
7429
cidr_blocks = ["0.0.0.0/0"]
30+
description = "Allow all outbound traffic"
7531
}
7632

7733
tags = {
7834
Name = var.eks_sg_name
7935
}
8036
}
8137

82-
# Create an EKS cluster
83-
resource "aws_eks_cluster" "eks_cluster" {
38+
# Create the EKS cluster
39+
resource "aws_eks_cluster" "main" {
8440
name = var.cluster_name
85-
role_arn = aws_iam_role.eks_role.arn
41+
role_arn = var.cluster_role_arn
8642

8743
vpc_config {
8844
subnet_ids = var.subnet_ids
8945
security_group_ids = [aws_security_group.eks_sg.id]
9046
}
9147

92-
depends_on = [
93-
aws_iam_role_policy_attachment.eks_cluster_policy,
94-
aws_iam_role_policy_attachment.eks_service_policy,
95-
]
96-
}
97-
98-
# Create an IAM role for node group
99-
resource "aws_iam_role" "node_role" {
100-
name = var.node_role_name
101-
102-
assume_role_policy = jsonencode({
103-
Version = "2012-10-17"
104-
Statement = [
105-
{
106-
Action = "sts:AssumeRole"
107-
Effect = "Allow"
108-
Principal = {
109-
Service = "ec2.amazonaws.com"
110-
}
111-
},
112-
]
113-
})
48+
depends_on = [aws_security_group.eks_sg]
11449
}
11550

116-
resource "aws_iam_role_policy_attachment" "node_policy" {
117-
role = aws_iam_role.node_role.name
118-
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
119-
}
120-
121-
resource "aws_iam_role_policy_attachment" "cni_policy" {
122-
role = aws_iam_role.node_role.name
123-
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
124-
}
125-
126-
resource "aws_iam_role_policy_attachment" "ecr_policy" {
127-
role = aws_iam_role.node_role.name
128-
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
129-
}
130-
131-
# Create a node group
132-
resource "aws_eks_node_group" "node_group" {
133-
cluster_name = aws_eks_cluster.eks_cluster.name
51+
# Create the EKS node group
52+
resource "aws_eks_node_group" "main" {
53+
cluster_name = aws_eks_cluster.main.name
13454
node_group_name = var.node_group_name
135-
node_role_arn = aws_iam_role.node_role.arn
55+
node_role_arn = var.node_role_arn
13656
subnet_ids = var.subnet_ids
13757

13858
scaling_config {
13959
desired_size = var.desired_size
140-
min_size = var.min_size
14160
max_size = var.max_size
61+
min_size = var.min_size
14262
}
14363

144-
depends_on = [
145-
aws_iam_role_policy_attachment.node_policy,
146-
aws_iam_role_policy_attachment.cni_policy,
147-
aws_iam_role_policy_attachment.ecr_policy,
148-
]
64+
instance_types = ["t3.medium"]
65+
66+
depends_on = [aws_eks_cluster.main]
14967
}

infra/modules/eks/variables.tf

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
variable "allowed_cidr_block" {
2-
description = "The CIDR block that is allowed to access the EKS cluster"
3-
type = string
4-
default = "10.0.0.0/16"
5-
}
6-
71
variable "vpc_id" {
82
description = "The ID of the VPC where the EKS cluster will be created"
93
type = string
@@ -24,22 +18,14 @@ variable "node_group_name" {
2418
type = string
2519
}
2620

27-
variable "eks_role_name" {
28-
description = "The name of the IAM role for the EKS cluster"
21+
variable "cluster_role_arn" {
22+
description = "The ARN of the IAM role for the EKS cluster"
2923
type = string
30-
default = "eks-cluster-role"
3124
}
3225

33-
variable "node_role_name" {
34-
description = "The name of the IAM role for the EKS node group"
26+
variable "node_role_arn" {
27+
description = "The ARN of the IAM role for the EKS node group"
3528
type = string
36-
default = "eks-node-role"
37-
}
38-
39-
variable "eks_sg_name" {
40-
description = "The name of the security group for the EKS cluster"
41-
type = string
42-
default = "eks-cluster-sg"
4329
}
4430

4531
variable "desired_size" {
@@ -59,3 +45,15 @@ variable "max_size" {
5945
type = number
6046
default = 3
6147
}
48+
49+
variable "allowed_cidr_block" {
50+
description = "The CIDR block that is allowed to access the EKS cluster"
51+
type = string
52+
default = "10.0.0.0/16"
53+
}
54+
55+
variable "eks_sg_name" {
56+
description = "The name of the security group for the EKS cluster"
57+
type = string
58+
default = "eks-cluster-sg"
59+
}

infra/variables.tf

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ variable "region" {
33
type = string
44
}
55

6+
variable "environment" {
7+
description = "Environment name (dev, stage, prod)"
8+
type = string
9+
default = "dev"
10+
}
11+
612
variable "vpc_cidr" {
713
description = "CIDR block for the VPC"
814
type = string
@@ -18,21 +24,11 @@ variable "subnet_2_cidr" {
1824
type = string
1925
}
2026

21-
variable "eks_role_name" {
22-
description = "Name of the EKS IAM role"
23-
type = string
24-
}
25-
2627
variable "cluster_name" {
2728
description = "Name of the EKS cluster"
2829
type = string
2930
}
3031

31-
variable "node_role_name" {
32-
description = "Name of the node group IAM role"
33-
type = string
34-
}
35-
3632
variable "node_group_name" {
3733
description = "Name of the node group"
3834
type = string

0 commit comments

Comments
 (0)