Skip to content

Commit a1f0770

Browse files
committed
refactor: ♻️ create and use eks-cluster module
1 parent bf8ca11 commit a1f0770

25 files changed

Lines changed: 228 additions & 745 deletions

File tree

html-db-website/aws-eks/README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ This is an example repository containing Terraform code. It contains the code to
1010
├── README.md
1111
└── terraform
1212
├── files
13-
│   ├── alb-policy.json
14-
│   ├── alb-values.yaml
1513
│   └── html-db-values.yaml
16-
├── iam.tf
1714
├── kubernetes.tf # We are deploying Kubernetes objects here.
1815
├── main.tf
19-
├── network.tf
2016
├── outputs.tf
2117
├── provider.tf
22-
├── security_group.tf
2318
└── variables.tf
2419
```
2520

@@ -73,7 +68,7 @@ export TF_VAR_db_password=
7368

7469
Use this command to get merge the kubeconfig with `~/.kube/config`:
7570
```shell
76-
aws eks update-kubeconfig --name nginx-cluster --region us-east-1
71+
aws eks update-kubeconfig --name html-db --region us-east-1
7772
```
7873

7974
You may need to delete `validatingwebhookconfigurations` and `mutatingwebhookconfigurations` during `terraform destroy`.

html-db-website/aws-eks/terraform/kubernetes.tf

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
resource "kubernetes_service_account_v1" "alb_controller" {
2-
metadata {
3-
name = "aws-load-balancer-controller"
4-
namespace = "kube-system"
5-
6-
annotations = {
7-
"eks.amazonaws.com/role-arn" = aws_iam_role.alb_controller.arn
8-
}
9-
}
10-
}
11-
12-
resource "helm_release" "alb_controller" {
13-
depends_on = [
14-
aws_eks_cluster.nginx,
15-
aws_eks_fargate_profile.nginx,
16-
aws_eks_node_group.system_nodes,
17-
kubernetes_service_account_v1.alb_controller
18-
]
19-
20-
name = "aws-load-balancer-controller"
21-
repository = "https://aws.github.io/eks-charts"
22-
chart = "aws-load-balancer-controller"
23-
namespace = "kube-system"
24-
25-
values = [
26-
templatefile("${path.module}/files/alb-values.yaml", {
27-
CLUSTER_NAME = aws_eks_cluster.nginx.name
28-
SERVICE_ACCOUNT_NAME = kubernetes_service_account_v1.alb_controller.metadata[0].name
29-
AWS_REGION = var.aws_region
30-
VPC_ID = aws_vpc.eks.id
31-
SUBNET_1 = aws_subnet.eks[0].id
32-
SUBNET_2 = aws_subnet.eks[1].id
33-
})
34-
]
35-
}
36-
371
resource "helm_release" "html-db" {
38-
depends_on = [helm_release.alb_controller]
2+
depends_on = [module.eks_cluster]
393

404
name = "html-db"
415
repository = "https://falltrades.github.io/cloud-example"
@@ -47,9 +11,9 @@ resource "helm_release" "html-db" {
4711

4812
values = [
4913
templatefile("${path.module}/files/html-db-values.yaml", {
50-
SUBNET_1 = aws_subnet.public[0].id
51-
SUBNET_2 = aws_subnet.public[1].id
52-
ALB_SG_ID = aws_security_group.alb.id
14+
SUBNET_1 = module.eks_cluster.public_subnets[0]
15+
SUBNET_2 = module.eks_cluster.public_subnets[1]
16+
ALB_SG_ID = module.eks_cluster.alb_security_group_id
5317
DB_NAME = var.db_name
5418
DB_USER = var.db_username
5519
DB_PASSWORD = var.db_password
Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,4 @@
1-
resource "aws_eks_cluster" "nginx" {
2-
depends_on = [aws_iam_role_policy_attachment.eks_cluster]
3-
4-
name = "nginx-cluster"
5-
role_arn = aws_iam_role.eks_cluster.arn
6-
7-
vpc_config {
8-
subnet_ids = aws_subnet.eks[*].id
9-
endpoint_public_access = true
10-
endpoint_private_access = false
11-
}
1+
module "eks_cluster" {
2+
source = "../../../terraform-modules/aws/eks-cluster"
3+
cluster_name = "html-db"
124
}
13-
14-
resource "aws_eks_fargate_profile" "nginx" {
15-
depends_on = [aws_eks_cluster.nginx]
16-
17-
cluster_name = aws_eks_cluster.nginx.name
18-
fargate_profile_name = "nginx-fargate"
19-
pod_execution_role_arn = aws_iam_role.fargate.arn
20-
subnet_ids = aws_subnet.eks[*].id
21-
22-
selector {
23-
namespace = "default"
24-
25-
labels = {
26-
app = "nginx"
27-
}
28-
}
29-
}
30-
31-
resource "aws_eks_node_group" "system_nodes" {
32-
depends_on = [aws_eks_cluster.nginx]
33-
34-
cluster_name = aws_eks_cluster.nginx.name
35-
node_group_name = "system-nodes"
36-
node_role_arn = aws_iam_role.eks_nodes.arn
37-
subnet_ids = aws_subnet.eks[*].id
38-
39-
scaling_config {
40-
desired_size = 1
41-
max_size = 1
42-
min_size = 1
43-
}
44-
45-
instance_types = ["t3.medium"]
46-
}
47-

html-db-website/aws-eks/terraform/outputs.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Wait for the Ingress to be created by Helm, then read its status
21
data "kubernetes_ingress_v1" "html_db_ingress" {
32
depends_on = [helm_release.html-db]
43
metadata {

html-db-website/aws-eks/terraform/provider.tf

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ terraform {
55
source = "hashicorp/aws"
66
version = "~> 6.0"
77
}
8-
kubernetes = {
9-
source = "hashicorp/kubernetes"
10-
version = "~> 3.0.1"
11-
}
12-
helm = {
13-
source = "hashicorp/helm"
14-
version = "~> 3.1.1"
15-
}
16-
tls = {
17-
source = "hashicorp/tls"
18-
version = "~> 4.2.1"
19-
}
208
}
219
}
2210

@@ -28,20 +16,20 @@ provider "aws" {
2816
skip_requesting_account_id = true
2917
}
3018

19+
data "aws_eks_cluster_auth" "cluster" {
20+
name = module.eks_cluster.cluster_name
21+
}
22+
3123
provider "kubernetes" {
32-
host = aws_eks_cluster.nginx.endpoint
33-
cluster_ca_certificate = base64decode(
34-
aws_eks_cluster.nginx.certificate_authority[0].data
35-
)
36-
token = data.aws_eks_cluster_auth.nginx.token
24+
host = module.eks_cluster.cluster_endpoint
25+
cluster_ca_certificate = base64decode(module.eks_cluster.cluster_ca_data)
26+
token = data.aws_eks_cluster_auth.cluster.token
3727
}
3828

3929
provider "helm" {
4030
kubernetes = {
41-
host = aws_eks_cluster.nginx.endpoint
42-
cluster_ca_certificate = base64decode(
43-
aws_eks_cluster.nginx.certificate_authority[0].data
44-
)
45-
token = data.aws_eks_cluster_auth.nginx.token
31+
host = module.eks_cluster.cluster_endpoint
32+
cluster_ca_certificate = base64decode(module.eks_cluster.cluster_ca_data)
33+
token = data.aws_eks_cluster_auth.cluster.token
4634
}
4735
}

html-db-website/aws-eks/terraform/security_group.tf

Lines changed: 0 additions & 27 deletions
This file was deleted.

static-website/aws-eks/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@ This is an example repository containing Terraform. It contains the code to depl
99
│   └── architecture.dot.png # Generated with https://github.com/patrickchugh/terravision.
1010
├── README.md
1111
└── terraform
12-
├── files
13-
│   ├── alb-policy.json
14-
│   └── alb-values.yaml
15-
├── iam.tf
1612
├── kubernetes.tf # We are deploying Kubernetes object here.
1713
├── main.tf
18-
├── network.tf
1914
├── outputs.tf
2015
├── provider.tf
21-
├── security_group.tf
2216
└── variables.tf
2317
```
2418

@@ -66,7 +60,7 @@ This is an example repository containing Terraform. It contains the code to depl
6660

6761
Use this command to get merge the kubeconfig with `~/.kube/config`:
6862
```shell
69-
aws eks update-kubeconfig --name nginx-cluster --region us-east-1
63+
aws eks update-kubeconfig --name nginx --region us-east-1
7064
```
7165

7266
You may need to delete `validatingwebhookconfigurations` and `mutatingwebhookconfigurations` during `terraform destroy`.

0 commit comments

Comments
 (0)