Skip to content

Commit 05a0e7b

Browse files
committed
adding eks module example
1 parent b4c8c85 commit 05a0e7b

File tree

14 files changed

+430
-0
lines changed

14 files changed

+430
-0
lines changed

‎aws_eks_module_demo/eks-from-own-module/.terraform.lock.hcl

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
terraform {
2+
backend "local" {
3+
path = "terraform.tfstate"
4+
}
5+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
provider "aws" {
2+
region = var.region
3+
}
4+
5+
module "vpc" {
6+
source = "./modules/vpc"
7+
private_subnets = var.private_subnets
8+
vpc_cidr = var.vpc_cidr
9+
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
10+
}
11+
12+
module "iam" {
13+
source = "./modules/iam"
14+
}
15+
16+
module "eks" {
17+
source = "./modules/eks"
18+
cluster_name = var.cluster_name
19+
vpc_id = module.vpc.vpc_id
20+
public_subnets = module.vpc.public_subnet_ids
21+
private_subnets = module.vpc.private_subnet_ids
22+
desired_capacity = var.desired_capacity
23+
max_size = var.max_size
24+
min_size = var.min_size
25+
instance_type = var.instance_type
26+
cluster_iam_role_arn = module.iam.eks_cluster_role_arn
27+
node_group_iam_role_arn = module.iam.eks_node_group_role_arn
28+
key_name = var.key_name
29+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
resource "random_id" "eks_cluster_id" {
2+
byte_length = 8
3+
}
4+
5+
resource "aws_eks_cluster" "eks_cluster" {
6+
name = "${var.cluster_name}-${random_id.eks_cluster_id.hex}"
7+
role_arn = var.cluster_iam_role_arn
8+
9+
vpc_config {
10+
subnet_ids = var.public_subnets
11+
}
12+
}
13+
14+
data "aws_ami" "eks_worker" {
15+
most_recent = true
16+
owners = ["602401143452"] # AWS EKS AMI owner ID
17+
18+
filter {
19+
name = "name"
20+
values = ["amazon-eks-node-*"]
21+
}
22+
23+
filter {
24+
name = "architecture"
25+
values = ["x86_64"]
26+
}
27+
}
28+
29+
resource "aws_launch_template" "eks_node_group" {
30+
name_prefix = "eks-node-group-"
31+
image_id = data.aws_ami.eks_worker.id
32+
instance_type = var.instance_type
33+
34+
key_name = var.key_name
35+
36+
lifecycle {
37+
create_before_destroy = true
38+
}
39+
}
40+
41+
resource "aws_eks_node_group" "eks_node_group" {
42+
depends_on = [ aws_eks_cluster.eks_cluster ]
43+
cluster_name = aws_eks_cluster.eks_cluster.name
44+
node_group_name = "eks-node-group"
45+
node_role_arn = var.node_group_iam_role_arn
46+
subnet_ids = var.private_subnets
47+
48+
launch_template {
49+
id = aws_launch_template.eks_node_group.id
50+
version = "$Latest"
51+
}
52+
53+
scaling_config {
54+
desired_size = var.desired_capacity
55+
max_size = var.max_size
56+
min_size = var.min_size
57+
}
58+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
output "cluster_endpoint" {
2+
description = "EKS cluster endpoint"
3+
value = aws_eks_cluster.eks_cluster.endpoint
4+
}
5+
6+
output "cluster_security_group_id" {
7+
description = "EKS cluster security group ID"
8+
value = aws_eks_cluster.eks_cluster.vpc_config[0].cluster_security_group_id
9+
}
10+
11+
output "cluster_arn" {
12+
description = "EKS cluster ARN"
13+
value = aws_eks_cluster.eks_cluster.arn
14+
}
15+
16+
output "cluster_id" {
17+
value = aws_eks_cluster.eks_cluster.id
18+
}
19+
20+
output "node_group_role_arn" {
21+
value = aws_eks_node_group.eks_node_group.node_role_arn
22+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
variable "cluster_name" {
2+
description = "EKS cluster name"
3+
type = string
4+
}
5+
6+
variable "vpc_id" {
7+
description = "VPC ID"
8+
type = string
9+
}
10+
11+
variable "public_subnets" {
12+
description = "Public subnet IDs"
13+
type = list(string)
14+
}
15+
16+
variable "private_subnets" {
17+
description = "Private subnet IDs"
18+
type = list(string)
19+
}
20+
21+
variable "desired_capacity" {
22+
description = "Desired number of worker nodes"
23+
type = number
24+
}
25+
26+
variable "max_size" {
27+
description = "Maximum number of worker nodes"
28+
type = number
29+
}
30+
31+
variable "min_size" {
32+
description = "Minimum number of worker nodes"
33+
type = number
34+
}
35+
36+
variable "instance_type" {
37+
description = "EC2 instance type for worker nodes"
38+
type = string
39+
}
40+
41+
variable "cluster_iam_role_arn" {
42+
description = "EKS Cluster IAM Role ARN"
43+
type = string
44+
}
45+
46+
variable "node_group_iam_role_arn" {
47+
description = "EKS Node Group IAM Role ARN"
48+
type = string
49+
}
50+
51+
variable "key_name" {
52+
description = "Node group instance key"
53+
type = string
54+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
resource "random_id" "eks_cluster_role_id" {
2+
byte_length = 8
3+
}
4+
5+
resource "aws_iam_role" "eks_cluster_role" {
6+
name = "eksClusterRole-${var.environment}-${random_id.eks_cluster_role_id.hex}"
7+
8+
assume_role_policy = jsonencode({
9+
Version = "2012-10-17"
10+
Statement = [
11+
{
12+
Effect = "Allow"
13+
Principal = {
14+
Service = "eks.amazonaws.com"
15+
}
16+
Action = "sts:AssumeRole"
17+
}
18+
]
19+
})
20+
}
21+
22+
resource "aws_iam_role_policy_attachment" "eks_cluster_policy" {
23+
role = aws_iam_role.eks_cluster_role.name
24+
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
25+
}
26+
27+
resource "aws_iam_role_policy_attachment" "eks_service_policy" {
28+
role = aws_iam_role.eks_cluster_role.name
29+
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy"
30+
}
31+
32+
resource "random_id" "eks_nodegroup_role_id" {
33+
byte_length = 8
34+
}
35+
36+
resource "aws_iam_role" "eks_node_group_role" {
37+
name = "eksNodeGroupRole-${var.environment}-${random_id.eks_nodegroup_role_id.hex}"
38+
39+
assume_role_policy = jsonencode({
40+
Version = "2012-10-17"
41+
Statement = [
42+
{
43+
Effect = "Allow"
44+
Principal = {
45+
Service = "ec2.amazonaws.com"
46+
}
47+
Action = "sts:AssumeRole"
48+
}
49+
]
50+
})
51+
}
52+
53+
resource "aws_iam_role_policy_attachment" "eks_worker_node_policy" {
54+
role = aws_iam_role.eks_node_group_role.name
55+
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
56+
}
57+
58+
resource "aws_iam_role_policy_attachment" "eks_cni_policy" {
59+
role = aws_iam_role.eks_node_group_role.name
60+
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
61+
}
62+
63+
resource "aws_iam_role_policy_attachment" "eks_ec2_container_registry_read_only" {
64+
role = aws_iam_role.eks_node_group_role.name
65+
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
66+
}
67+
68+
resource "aws_iam_role_policy_attachment" "eks_vpc_resource_controller_policy" {
69+
role = aws_iam_role.eks_node_group_role.name
70+
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSVPCResourceController"
71+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "eks_cluster_role_arn" {
2+
description = "EKS Cluster IAM Role ARN"
3+
value = aws_iam_role.eks_cluster_role.arn
4+
}
5+
6+
output "eks_node_group_role_arn" {
7+
description = "EKS Node Group IAM Role ARN"
8+
value = aws_iam_role.eks_node_group_role.arn
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "environment" {
2+
description = "Environment tag"
3+
type = string
4+
default = "dev"
5+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
resource "aws_vpc" "eks_vpc" {
2+
cidr_block = var.vpc_cidr
3+
}
4+
5+
data "aws_availability_zones" "available" {
6+
state = "available"
7+
}
8+
9+
resource "aws_subnet" "public_subnets" {
10+
count = length(var.public_subnets)
11+
vpc_id = aws_vpc.eks_vpc.id
12+
cidr_block = var.public_subnets[count.index]
13+
availability_zone = element(data.aws_availability_zones.available.names, count.index)
14+
map_public_ip_on_launch = true
15+
}
16+
17+
resource "aws_subnet" "private_subnets" {
18+
count = length(var.private_subnets)
19+
vpc_id = aws_vpc.eks_vpc.id
20+
cidr_block = var.private_subnets[count.index]
21+
}
22+
23+
resource "aws_internet_gateway" "igw" {
24+
vpc_id = aws_vpc.eks_vpc.id
25+
}
26+
27+
resource "aws_route_table" "public_rt" {
28+
vpc_id = aws_vpc.eks_vpc.id
29+
30+
route {
31+
cidr_block = "0.0.0.0/0"
32+
gateway_id = aws_internet_gateway.igw.id
33+
}
34+
}
35+
36+
resource "aws_route_table_association" "public_rt_assoc" {
37+
count = length(var.public_subnets)
38+
subnet_id = aws_subnet.public_subnets[count.index].id
39+
route_table_id = aws_route_table.public_rt.id
40+
}

0 commit comments

Comments
 (0)