Skip to content

Commit 13002fc

Browse files
committed
Refactor Terraform configuration for improved readability and optimization, including updates to backend settings, variable descriptions, and resource definitions.
1 parent f5d4db4 commit 13002fc

File tree

11 files changed

+135
-146
lines changed

11 files changed

+135
-146
lines changed

Terraform/2-backend.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
terraform {
22
backend "s3" {
3-
bucket = "solar-system-terraform-state-123456"
4-
key = "o-tel-demo/terraform.tfstate"
5-
region = "us-east-1"
3+
bucket = "solar-system-terraform-state-123456"
4+
key = "o-tel-demo/terraform.tfstate"
5+
region = "us-east-1"
66
dynamodb_table = "solar-system-terraform-locks"
7-
encrypt = true
7+
encrypt = true
88
}
99
}

Terraform/3-main.tf

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ module "vpc" {
99
}
1010

1111
module "eks" {
12-
source = "./modules/eks"
13-
cluster_name = var.cluster_name
14-
cluster_version = var.cluster_version
15-
vpc_id = module.vpc.vpc_id
12+
source = "./modules/eks"
13+
cluster_name = var.cluster_name
14+
cluster_version = var.cluster_version
15+
vpc_id = module.vpc.vpc_id
1616
cluster_subnet_ids = concat(module.vpc.private_subnet_ids, module.vpc.public_subnet_ids)
17-
node_subnet_ids = module.vpc.private_subnet_ids
18-
node_groups = var.node_groups
17+
node_subnet_ids = module.vpc.private_subnet_ids
18+
node_groups = var.node_groups
19+
depends_on = [module.vpc]
1920
}
2021
resource "aws_eks_addon" "metrics_server" {
21-
cluster_name = module.eks.cluster_name
22-
depends_on = [module.eks]
23-
addon_name = "metrics-server"
24-
addon_version = null
22+
cluster_name = module.eks.cluster_name
23+
addon_name = "metrics-server"
24+
addon_version = null
2525
resolve_conflicts_on_create = "OVERWRITE"
2626
resolve_conflicts_on_update = "OVERWRITE"
27+
depends_on = [module.eks]
2728
}

Terraform/4-variables.tf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
variable "vpc_cidr" {
2-
description = "CIDR Block for VPC"
3-
type = string
4-
default = "10.0.0.0/16"
2+
description = "CIDR Block for VPC"
3+
type = string
4+
default = "10.0.0.0/16"
55
}
66

77
variable "availability_zones" {
@@ -11,29 +11,29 @@ variable "availability_zones" {
1111
}
1212

1313
variable "private_subnet_cidrs" {
14-
description = "CIDR blocks for private subnets"
15-
type = list(string)
16-
default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
14+
description = "CIDR blocks for private subnets"
15+
type = list(string)
16+
default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
1717
}
1818

1919

2020
variable "public_subnet_cidrs" {
21-
description = "CIDR blocks for public subnets"
22-
type = list(string)
23-
default = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
21+
description = "CIDR blocks for public subnets"
22+
type = list(string)
23+
default = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
2424
}
2525

2626

2727
variable "cluster_name" {
28-
description = "Name of the EKS cluster"
29-
type = string
30-
default = "otel-cluster"
28+
description = "Name of the EKS cluster"
29+
type = string
30+
default = "otel-cluster"
3131
}
3232

3333
variable "cluster_version" {
34-
description = "Kubernetes version"
35-
type = string
36-
default = "1.30"
34+
description = "Kubernetes version"
35+
type = string
36+
default = "1.30"
3737
}
3838

3939

@@ -45,7 +45,7 @@ variable "node_groups" {
4545
scaling_config = object({
4646
desired_size = number
4747
max_size = number
48-
min_size = number
48+
min_size = number
4949
})
5050
}))
5151

Terraform/5-outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
output "vpc_id" {
22
description = "VPC ID"
3-
value = module.vpc.vpc_id
3+
value = module.vpc.vpc_id
44
}
55

66
output "cluster_endpoint" {
77
description = "EKS cluster endpoint"
8-
value = module.eks.cluster_endpoint
8+
value = module.eks.cluster_endpoint
99
}
1010

1111
output "cluster_name" {
1212
description = "EKS cluster name"
13-
value = module.eks.cluster_name
13+
value = module.eks.cluster_name
1414
}
1515

1616
output "app_namespace" {

Terraform/modules/eks/main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ resource "aws_eks_cluster" "main" {
99
}
1010

1111
vpc_config {
12-
subnet_ids = var.cluster_subnet_ids # Changed from var.subnet_ids
12+
subnet_ids = var.cluster_subnet_ids # Changed from var.subnet_ids
1313
}
1414

1515
depends_on = [
@@ -95,7 +95,7 @@ resource "aws_eks_node_group" "main" {
9595
cluster_name = aws_eks_cluster.main.name
9696
node_group_name = each.key
9797
node_role_arn = aws_iam_role.node.arn
98-
subnet_ids = var.node_subnet_ids # Changed from var.subnet_ids
98+
subnet_ids = var.node_subnet_ids # Changed from var.subnet_ids
9999
scaling_config {
100100
desired_size = each.value.scaling_config.desired_size
101101
max_size = each.value.scaling_config.max_size
@@ -120,7 +120,7 @@ resource "aws_iam_instance_profile" "karpenter_node" {
120120
# Karpenter Controller IAM Role
121121
resource "aws_iam_role" "karpenter_controller" {
122122
name = "KarpenterControllerRole-${var.cluster_name}"
123-
123+
124124
assume_role_policy = jsonencode({
125125
Version = "2012-10-17"
126126
Statement = [
@@ -182,8 +182,8 @@ resource "aws_iam_role_policy" "karpenter_controller" {
182182
Resource = "*"
183183
},
184184
{
185-
Effect = "Allow"
186-
Action = "iam:CreateServiceLinkedRole"
185+
Effect = "Allow"
186+
Action = "iam:CreateServiceLinkedRole"
187187
Resource = "*"
188188
Condition = {
189189
StringEquals = {

Terraform/modules/eks/outputs.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
output "cluster_endpoint" {
22
description = "EKS cluster endpoint"
3-
value = aws_eks_cluster.main.endpoint
3+
value = aws_eks_cluster.main.endpoint
44
}
55

66
output "cluster_name" {
77
description = "EKS cluster name"
8-
value = aws_eks_cluster.main.name
8+
value = aws_eks_cluster.main.name
99
}
1010

1111
output "cluster_certificate_authority_data" {
1212
description = "EKS cluster certificate authority data"
13-
value = aws_eks_cluster.main.certificate_authority[0].data
13+
value = aws_eks_cluster.main.certificate_authority[0].data
1414
}
1515

1616
output "karpenter_controller_role_arn" {
1717
description = "Karpenter controller IAM role ARN"
18-
value = aws_iam_role.karpenter_controller.arn
18+
value = aws_iam_role.karpenter_controller.arn
1919
}
2020

2121
output "karpenter_interruption_queue_name" {
2222
description = "Karpenter interruption SQS queue name"
23-
value = aws_sqs_queue.karpenter_interruption.name
23+
value = aws_sqs_queue.karpenter_interruption.name
2424
}

Terraform/modules/eks/variables.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
variable "cluster_name" {
2-
description = "Name of the EKS cluster"
3-
type = string
2+
description = "Name of the EKS cluster"
3+
type = string
44
}
55

66
variable "cluster_version" {
7-
description = "Kubernetes version"
8-
type = string
7+
description = "Kubernetes version"
8+
type = string
99
}
1010

1111
variable "vpc_id" {
@@ -23,14 +23,14 @@ variable "node_subnet_ids" {
2323
type = list(string)
2424
}
2525
variable "node_groups" {
26-
description = "EKS node groups configuration"
27-
type = map(object({
26+
description = "EKS node groups configuration"
27+
type = map(object({
2828
instance_types = list(string)
2929
capacity_type = string
3030
scaling_config = object({
3131
desired_size = number
3232
max_size = number
33-
min_size = number
33+
min_size = number
3434
})
3535
}))
3636
}

Terraform/modules/vpc/main.tf

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
resource "aws_vpc" "main" {
2-
cidr_block = var.vpc_cidr # "10.0.0.0/16"
3-
enable_dns_hostnames = true
4-
enable_dns_support = true
2+
cidr_block = var.vpc_cidr # "10.0.0.0/16"
3+
enable_dns_hostnames = true
4+
enable_dns_support = true
55

66
tags = {
7-
Name = "${var.cluster_name}-vpc"
8-
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
7+
Name = "${var.cluster_name}-vpc"
8+
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
99
}
1010
}
1111

1212
resource "aws_subnet" "private" {
13-
count = length(var.private_subnet_cidrs)
14-
vpc_id = aws_vpc.main.id
15-
cidr_block = var.private_subnet_cidrs[count.index] # "10.0.1.0/24"
16-
availability_zone = var.availability_zones[count.index]
13+
count = length(var.private_subnet_cidrs)
14+
vpc_id = aws_vpc.main.id
15+
cidr_block = var.private_subnet_cidrs[count.index] # "10.0.1.0/24"
16+
availability_zone = var.availability_zones[count.index]
1717

1818
tags = {
19-
Name = "${var.cluster_name}-private-${count.index + 1}"
20-
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
21-
"kubernetes.io/role/internal-elb" = "1"
22-
"karpenter.sh/discovery" = var.cluster_name
19+
Name = "${var.cluster_name}-private-${count.index + 1}"
20+
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
21+
"kubernetes.io/role/internal-elb" = "1"
22+
"karpenter.sh/discovery" = var.cluster_name
2323
}
2424
}
2525

2626
resource "aws_subnet" "public" {
27-
count = length(var.public_subnet_cidrs)
28-
vpc_id = aws_vpc.main.id
29-
cidr_block = var.public_subnet_cidrs[count.index] # "10.0.1.0/24"
30-
availability_zone = var.availability_zones[count.index]
31-
map_public_ip_on_launch = true
27+
count = length(var.public_subnet_cidrs)
28+
vpc_id = aws_vpc.main.id
29+
cidr_block = var.public_subnet_cidrs[count.index] # "10.0.1.0/24"
30+
availability_zone = var.availability_zones[count.index]
31+
map_public_ip_on_launch = true
3232

3333
tags = {
34-
Name = "${var.cluster_name}-public-${count.index + 1}"
35-
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
36-
"kubernetes.io/role/elb" = "1"
37-
"karpenter.sh/discovery" = var.cluster_name
34+
Name = "${var.cluster_name}-public-${count.index + 1}"
35+
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
36+
"kubernetes.io/role/elb" = "1"
37+
"karpenter.sh/discovery" = var.cluster_name
3838
}
3939
}
4040

@@ -44,7 +44,7 @@ resource "aws_internet_gateway" "main" {
4444
vpc_id = aws_vpc.main.id
4545

4646
tags = {
47-
Name = "${var.cluster_name}-igw"
47+
Name = "${var.cluster_name}-igw"
4848
}
4949
}
5050

@@ -63,22 +63,25 @@ resource "aws_route_table" "public" {
6363
}
6464

6565
resource "aws_route_table_association" "public" {
66-
count = length(var.public_subnet_cidrs)
67-
subnet_id = aws_subnet.public[count.index].id
68-
route_table_id = aws_route_table.public[count.index].id
66+
count = length(var.public_subnet_cidrs)
67+
subnet_id = aws_subnet.public[count.index].id
68+
route_table_id = aws_route_table.public[count.index].id
6969
}
7070

7171

7272
resource "aws_eip" "nat" {
73-
count = length(var.public_subnet_cidrs)
74-
domain = "vpc"
73+
count = 1
74+
domain = "vpc"
75+
tags = {
76+
Name = "${var.cluster_name}-eip-nat"
77+
}
7578
}
7679

7780
resource "aws_nat_gateway" "main" {
78-
count = length(var.public_subnet_cidrs)
79-
allocation_id = aws_eip.nat[count.index].id
80-
subnet_id = aws_subnet.public[count.index].id
81-
depends_on = [
81+
count = 1
82+
allocation_id = aws_eip.nat[0].id
83+
subnet_id = aws_subnet.public[0].id
84+
depends_on = [
8285
aws_internet_gateway.main,
8386
aws_subnet.public,
8487
aws_eip.nat
@@ -90,12 +93,12 @@ resource "aws_nat_gateway" "main" {
9093
}
9194

9295
resource "aws_route_table" "private" {
93-
count = length(var.private_subnet_cidrs)
94-
vpc_id = aws_vpc.main.id
96+
count = length(var.private_subnet_cidrs)
97+
vpc_id = aws_vpc.main.id
9598

9699
route {
97-
cidr_block = "0.0.0.0/0"
98-
nat_gateway_id = aws_nat_gateway.main[count.index].id
100+
cidr_block = "0.0.0.0/0"
101+
nat_gateway_id = aws_nat_gateway.main[0].id
99102
}
100103

101104
tags = {
@@ -104,8 +107,8 @@ resource "aws_route_table" "private" {
104107
}
105108

106109
resource "aws_route_table_association" "private" {
107-
count = length(var.private_subnet_cidrs)
108-
subnet_id = aws_subnet.private[count.index].id
109-
route_table_id = aws_route_table.private[count.index].id
110+
count = length(var.private_subnet_cidrs)
111+
subnet_id = aws_subnet.private[count.index].id
112+
route_table_id = aws_route_table.private[count.index].id
110113
}
111114

Terraform/modules/vpc/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
output "vpc_id" {
22
description = "VPC ID"
3-
value = aws_vpc.main.id
3+
value = aws_vpc.main.id
44
}
55

66
output "private_subnet_ids" {
77
description = "Private subnets IDS"
8-
value = aws_subnet.private[*].id
8+
value = aws_subnet.private[*].id
99
}
1010

1111
output "public_subnet_ids" {
1212
description = "public subnets IDS"
13-
value = aws_subnet.public[*].id
13+
value = aws_subnet.public[*].id
1414
}

0 commit comments

Comments
 (0)