This repository has been archived by the owner on Jun 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
212 lines (179 loc) · 5.83 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
resource "aws_security_group" "eks-cluster" {
name = join("-", [var.cluster_name, "eks-cluster"])
description = "Cluster communication with worker nodes"
vpc_id = var.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.tags
}
## OPTIONAL: Allow inbound traffic from internet to the Kubernetes.
resource "aws_security_group_rule" "eks-cluster-ingress-internet-https" {
cidr_blocks = ["0.0.0.0/0"]
description = "Allow workstations to communicate with the cluster API Server"
from_port = 443
protocol = "tcp"
security_group_id = aws_security_group.eks-cluster.id
to_port = 443
type = "ingress"
}
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = ">= 8.0.0"
cluster_name = var.cluster_name
subnets = concat(var.public_subnets, var.private_subnets)
vpc_id = var.vpc_id
cluster_endpoint_private_access = true
cluster_endpoint_public_access = true
cluster_security_group_id = aws_security_group.eks-cluster.id
worker_ami_name_filter = var.eks_worker_ami_name_filter
cluster_enabled_log_types = var.eks_cluster_enabled_log_types
cluster_version = var.eks_cluster_version
write_kubeconfig = true
config_output_path = "./"
kubeconfig_name = var.cluster_name
kubeconfig_aws_authenticator_env_variables = {
AWS_PROFILE = var.aws_profile
}
# using launch configuration
worker_groups = var.eks_worker_groups
workers_group_defaults = {
instance_type = "r5.xlarge"
name = "eks_workers_a"
# ami_id = "ami-0d275f57a60281ccc"
asg_max_size = 10
asg_min_size = 2
root_volume_size = 100
root_volume_type = "gp2"
autoscaling_enabled = true
protect_from_scale_in = true
asg_force_delete = true # This is to address a case when terraform cannot delete autoscaler group if protect_from_scale_in = true
enable_monitoring = false
kubelet_extra_args = "--node-labels=kubernetes.io/lifecycle=on-demand"
subnets = var.private_subnets
additional_security_group_ids = var.default_security_group_id
}
worker_groups_launch_template = var.eks_worker_groups_launch_template
map_users = var.eks_map_users
map_roles = var.eks_map_roles
tags = var.tags
}
## attach iam policy to allow aws alb ingress controller
resource "aws_iam_policy" "eks-worker-alb-ingress-controller" {
name_prefix = "eks-worker-ingress-controller-${var.cluster_name}"
description = "EKS worker node alb ingress controller policy for cluster ${var.cluster_name}"
policy = file(
"${path.module}/files/aws-alb-ingress-controller-iam-policy.json",
)
}
resource "aws_iam_role_policy_attachment" "eks-worker-alb-ingress-controller" {
policy_arn = aws_iam_policy.eks-worker-alb-ingress-controller.arn
role = module.eks.worker_iam_role_name
}
## attach iam policy to allow external-dns
resource "aws_iam_policy" "eks-worker-external-dns" {
name_prefix = "eks-worker-external-dns-${var.cluster_name}"
description = "EKS worker node external dns policy for cluster ${var.cluster_name}"
policy = file("${path.module}/files/aws-external-dns-iam-policy.json")
}
resource "aws_iam_role_policy_attachment" "eks-worker-external-dns" {
policy_arn = aws_iam_policy.eks-worker-external-dns.arn
role = module.eks.worker_iam_role_name
}
## attach extra iam policies
resource "aws_iam_policy" "eks-worker-extra" {
count = length(var.eks_extra_policies)
name_prefix = "eks-worker-extra-${var.cluster_name}"
description = "EKS worker node extra permissions for cluster ${var.cluster_name}"
policy = var.eks_extra_policies[count.index]
}
resource "aws_iam_role_policy_attachment" "eks-worker-extra" {
count = length(var.eks_extra_policies)
policy_arn = aws_iam_policy.eks-worker-extra[count.index].arn
role = module.eks.worker_iam_role_name
}
/*
## Tiller Service Account
resource "kubernetes_service_account" "tiller" {
metadata {
name = "tiller"
namespace = "kube-system"
}
}
resource "kubernetes_cluster_role_binding" "tiller" {
metadata {
name = "tiller"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "cluster-admin"
}
subject {
kind = "ServiceAccount"
name = "tiller"
namespace = "kube-system"
}
}
## CircleCi Service account
resource "kubernetes_service_account" "circleci" {
metadata {
name = "circleci"
namespace = "kube-system"
}
}
resource "kubernetes_cluster_role" "circleci" {
metadata {
name = "circleci"
}
rule {
api_groups = [""]
resources = ["pods/portforward"]
verbs = ["create"]
}
rule {
api_groups = ["*"]
resources = ["*"]
verbs = ["list", "get"]
}
rule {
api_groups = ["apps", "extensions"]
resources = ["deployments"]
verbs = ["get", "patch"]
}
}
resource "kubernetes_cluster_role_binding" "circleci" {
metadata {
name = "circleci"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "circleci"
}
subject {
kind = "ServiceAccount"
name = "circleci"
namespace = "kube-system"
}
}
*/
resource "null_resource" "k8s-tiller-rbac" {
provisioner "local-exec" {
working_dir = path.module
command = <<EOS
for i in `seq 1 10`; do \
echo "${module.eks.kubeconfig}" > kube_config.yaml & \
kubectl apply -f files/tiller-rbac.yaml --kubeconfig kube_config.yaml && break || \
sleep 10; \
done; \
rm kube_config.yaml;
EOS
}
triggers = {
kube_config_rendered = module.eks.kubeconfig
}
}