forked from yamaszone/terraform-eks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
267 lines (216 loc) · 6.32 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
variable "cluster_name" {
description = "Name of this EKS cluster"
}
variable "common_tags" {
description = "Tags that should be applied to all resources"
type = "map"
}
variable "environment" {
description = "Name of your environment, e.g. dev, stg, prod, etc."
}
variable "key_name" {
description = "SSH keypair"
}
variable "region" {
description = "AWS region. Default us-west-2"
}
variable "cidr" {
description = "VPC CIDR block"
}
variable "internal_subnets" {
description = "List of private subnets"
type = "list"
}
variable "external_subnets" {
description = "List of public subnets"
type = "list"
}
variable "availability_zones" {
description = "AZ list"
type = "list"
}
variable "bastion_instance_type" {
description = "Instance type for the bastion"
}
variable "bastion_volume_size" {
description = "Volume size in GB"
}
variable "policy_arn_eks_cluster" {
description = "ARN of the default policy: AmazonEKSClusterPolicy."
type = "string"
}
variable "policy_arn_eks_service" {
description = "ARN of the default policy: AmazonEKSServicePolicy."
type = "string"
}
variable "policy_arn_eks_worker" {
description = "ARN of the default policy: AmazonEKSWorkerNodePolicy"
type = "string"
}
variable "policy_arn_eks_cni" {
description = "ARN of the default policy: AmazonEKS_CNI_Policy"
type = "string"
}
variable "policy_arn_ecr_read" {
description = "ARN of the default policy: AmazonEC2ContainerRegistryReadOnly"
type = "string"
}
provider "aws" {
version = "~> 1.22"
region = "${var.region}"
}
module "vpc" {
source = "./src/modules/vpc"
name = "${var.cluster_name}"
cidr = "${var.cidr}"
common_tags = "${var.common_tags}"
internal_subnets = "${var.internal_subnets}"
external_subnets = "${var.external_subnets}"
availability_zones = "${var.availability_zones}"
environment = "${var.environment}"
}
module "security_groups" {
source = "./src/modules/security-groups"
cluster_name = "${var.cluster_name}"
common_tags = "${var.common_tags}"
vpc_id = "${module.vpc.id}"
environment = "${var.environment}"
cidr = "${var.cidr}"
}
module "bastion" {
source = "./src/modules/bastion"
common_tags = "${var.common_tags}"
region = "${var.region}"
instance_type = "${var.bastion_instance_type}"
volume_size = "${var.bastion_volume_size}"
security_groups = "${module.security_groups.external_ssh},${module.security_groups.internal_ssh}"
vpc_id = "${module.vpc.id}"
subnet_id = "${element(module.vpc.external_subnets, 0)}"
key_name = "${var.key_name}"
environment = "${var.environment}"
}
module "iam" {
source = "./src/modules/iam"
policy_arn_eks_cni = "${var.policy_arn_eks_cni}"
policy_arn_eks_service = "${var.policy_arn_eks_service}"
policy_arn_ecr_read = "${var.policy_arn_ecr_read}"
policy_arn_eks_cluster = "${var.policy_arn_eks_cluster}"
policy_arn_eks_worker = "${var.policy_arn_eks_worker}"
}
module "eks" {
source = "./src/modules/eks"
cluster_name = "${var.cluster_name}"
common_tags = "${var.common_tags}"
role_arn = "${module.iam.role_arn_eks_basic_masters}"
cluster_subnets = "${module.vpc.external_subnets}"
sg_id_cluster = "${module.security_groups.sg_id_masters}"
}
module "worker" {
source = "./src/modules/worker"
# Use module output to wait for masters to create.
cluster_name = "${module.eks.cluster_id}"
common_tags = "${var.common_tags}"
instance_profile_name_workers = "${module.iam.instance_profile_name_workers}"
worker_subnets = "${module.vpc.internal_subnets}"
sg_id_workers = "${module.security_groups.sg_id_workers}"
}
// The region in which the infra lives.
output "region" {
value = "${var.region}"
}
// The bastion host IP.
output "bastion_ip" {
value = "${module.bastion.external_ip}"
}
// Comma separated list of internal subnet IDs.
output "internal_subnets" {
value = "${module.vpc.internal_subnets}"
}
// Comma separated list of external subnet IDs.
output "external_subnets" {
value = "${module.vpc.external_subnets}"
}
// The environment of the stack, e.g "prod".
output "environment" {
value = "${var.environment}"
}
// The VPC availability zones.
output "availability_zones" {
value = "${module.vpc.availability_zones}"
}
// The VPC security group ID.
output "vpc_security_group" {
value = "${module.vpc.security_group}"
}
// The VPC ID.
output "vpc_id" {
value = "${module.vpc.id}"
}
// Comma separated list of internal route table IDs.
output "internal_route_tables" {
value = "${module.vpc.internal_rtb_id}"
}
output "external_route_tables" {
value = "${module.vpc.external_rtb_id}"
}
output "endpoint" {
description = "Endpoint of the cluster."
value = "${module.eks.endpoint}"
}
output "cluster_id" {
description = "The name of the cluster."
value = "${module.eks.cluster_id}"
}
### kubecfg
locals {
kubeconfig-aws-1-10 = <<KUBECONFIG
apiVersion: v1
clusters:
- cluster:
server: ${module.eks.endpoint}
certificate-authority-data: ${module.eks.kubeconfig-certificate-authority-data}
name: "${module.eks.cluster_id}"
contexts:
- context:
cluster: "${module.eks.cluster_id}"
user: aws
name: aws
current-context: aws
kind: Config
preferences: {}
users:
- name: aws
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
command: heptio-authenticator-aws
args:
- "token"
- "-i"
- "${module.eks.cluster_id}"
KUBECONFIG
}
locals {
worker_iam_role_arn = <<ROLEARN
apiVersion: v1
kind: ConfigMap
metadata:
name: aws-auth
namespace: kube-system
data:
mapRoles: |
- rolearn: ${module.iam.role_arn_eks_basic_workers}
username: system:node:{{EC2PrivateDNSName}}
groups:
- system:bootstrappers
- system:nodes
ROLEARN
}
output "kubeconfig-aws-1-10" {
description = "Kubeconfig to connect to the cluster."
value = "${local.kubeconfig-aws-1-10}"
}
output "role_arn_eks_basic_workers" {
description = "ARN of the eks-basic-workers role."
value = "${local.worker_iam_role_arn}"
}