-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
executable file
·224 lines (202 loc) · 7.02 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
provider "aws" {
version = "~> 3.0"
region = var.region
}
data "aws_availability_zones" "available" {
state = "available"
}
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = var.name
}
}
resource "aws_subnet" "public" {
count = var.num_zones
availability_zone = data.aws_availability_zones.available.names[count.index]
cidr_block = cidrsubnet(aws_vpc.vpc.cidr_block, var.subnet_addtl_bits, count.index)
vpc_id = aws_vpc.vpc.id
map_public_ip_on_launch = true
tags = {
Name = "${var.name}-public-${data.aws_availability_zones.available.names[count.index]}"
"kubernetes.io/role/elb" = "1"
}
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = var.name
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "${var.name}-public"
}
}
resource "aws_route_table_association" "public" {
count = var.num_zones
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
resource "aws_subnet" "private" {
count = var.num_zones
availability_zone = data.aws_availability_zones.available.names[count.index]
cidr_block = cidrsubnet(aws_vpc.vpc.cidr_block, var.subnet_addtl_bits, var.num_zones + count.index)
vpc_id = aws_vpc.vpc.id
tags = {
Name = "${var.name}-private-${data.aws_availability_zones.available.names[count.index]}"
"kubernetes.io/cluster/${var.name}" = "shared"
"kubernetes.io/role/internal-elb" = "1"
}
}
resource "aws_eip" "nat" {
count = var.num_zones
vpc = true
tags = {
Name = "${var.name}-nat-${data.aws_availability_zones.available.names[count.index]}"
}
}
resource "aws_nat_gateway" "nat" {
count = var.num_zones
allocation_id = aws_eip.nat[count.index].id
subnet_id = aws_subnet.public[count.index].id
tags = {
Name = "${var.name}-${data.aws_availability_zones.available.names[count.index]}"
}
}
resource "aws_route_table" "private" {
count = var.num_zones
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat[count.index].id
}
tags = {
Name = "${var.name}-private-${data.aws_availability_zones.available.names[count.index]}"
}
}
resource "aws_route_table_association" "private" {
count = var.num_zones
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[count.index].id
}
resource "aws_eks_cluster" "cluster" {
name = var.name
role_arn = aws_iam_role.cluster.arn
vpc_config {
endpoint_private_access = var.endpoint_private_access
endpoint_public_access = var.endpoint_public_access
public_access_cidrs = var.public_access_cidrs
subnet_ids = concat(aws_subnet.public[*].id, aws_subnet.private[*].id)
}
# Ensure that IAM Role permissions are created before and deleted after EKS Cluster handling.
# Otherwise, EKS will not be able to properly delete EKS managed EC2 infrastructure such as Security Groups.
depends_on = [
aws_iam_role_policy_attachment.AmazonEKSClusterPolicy,
aws_iam_role_policy_attachment.AmazonEKSServicePolicy,
]
}
resource "aws_eks_node_group" "nodegroup" {
count = length(var.node_pools)
cluster_name = aws_eks_cluster.cluster.name
node_group_name = format("%s-group", lookup(var.node_pools[count.index], "node_group_name", format("%03d", count.index + 1)))
node_role_arn = aws_iam_role.node.arn
subnet_ids = aws_subnet.private[*].id
scaling_config {
desired_size = lookup(var.node_pools[count.index], "desired_size", 1)
min_size = lookup(var.node_pools[count.index], "min_size", 1)
max_size = lookup(var.node_pools[count.index], "max_size", 2)
}
disk_size = lookup(var.node_pools[count.index], "disk_size", 20)
instance_types = [lookup(var.node_pools[count.index], "instance_type", "t3.small")]
# Allow external changes to autoscaling desired size without interference from Terraform
lifecycle {
ignore_changes = [scaling_config[0].desired_size]
}
# Ensure that IAM Role permissions are created before and deleted after EKS Node Group handling.
# Otherwise, EKS will not be able to properly delete EC2 Instances and Elastic Network Interfaces.
depends_on = [
aws_iam_role_policy_attachment.AmazonEKSWorkerNodePolicy,
aws_iam_role_policy_attachment.AmazonEKS_CNI_Policy,
aws_iam_role_policy_attachment.AmazonEC2ContainerRegistryReadOnly,
aws_iam_role_policy_attachment.AmazonSSMManagedInstanceCore,
aws_iam_role_policy.EKSClusterAutoscaler
]
}
resource "aws_iam_role" "cluster" {
name = "${var.name}-cluster"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "eks.amazonaws.com"
}
}]
})
}
resource "aws_iam_role_policy_attachment" "AmazonEKSClusterPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.cluster.name
}
resource "aws_iam_role_policy_attachment" "AmazonEKSServicePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy"
role = aws_iam_role.cluster.name
}
resource "aws_iam_role" "node" {
name = "${var.name}-node"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}]
})
}
resource "aws_iam_role_policy_attachment" "AmazonEKSWorkerNodePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
role = aws_iam_role.node.name
}
resource "aws_iam_role_policy_attachment" "AmazonEKS_CNI_Policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
role = aws_iam_role.node.name
}
resource "aws_iam_role_policy_attachment" "AmazonEC2ContainerRegistryReadOnly" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
role = aws_iam_role.node.name
}
resource "aws_iam_role_policy_attachment" "AmazonSSMManagedInstanceCore" {
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
role = aws_iam_role.node.name
}
resource "aws_iam_role_policy" "EKSClusterAutoscaler" {
name = "EKSClusterAutoscaler"
role = aws_iam_role.node.name
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"ec2:DescribeLaunchTemplateVersions"
],
Effect = "Allow"
Resource = "*"
}]
})
}