-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
197 lines (171 loc) · 4.97 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
provider "aws" {
region = var.aws_region
}
locals {
cluster_name = "${var.cluster_name}-${var.env_name}"
}
# EKS Cluster Resources
# * IAM Role to allow EKS service to manage other AWS services
# * EC2 Security Group to allow networking traffic with EKS cluster
# * EKS Cluster
#
resource "aws_iam_role" "ms-cluster" {
name = local.cluster_name
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "eks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "ms-cluster-AmazonEKSClusterPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.ms-cluster.name
}
resource "aws_security_group" "ms-cluster" {
name = local.cluster_name
description = "Cluster communication with worker nodes"
vpc_id = var.vpc_id
ingress {
description = "Inbound traffic from within the security group"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
tags = {
Name = "ms-up-running"
}
}
resource "aws_eks_cluster" "ms-up-running" {
name = local.cluster_name
role_arn = aws_iam_role.ms-cluster.arn
vpc_config {
security_group_ids = [aws_security_group.ms-cluster.id]
subnet_ids = var.cluster_subnet_ids
}
depends_on = [
aws_iam_role_policy_attachment.ms-cluster-AmazonEKSClusterPolicy
]
}
#
# EKS Worker Nodes Resources
# * IAM role allowing Kubernetes actions to access other AWS services
# * EKS Node Group to launch worker nodes
#
resource "aws_iam_role" "ms-node" {
name = "${local.cluster_name}.node"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "ms-node-AmazonEKSWorkerNodePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
role = aws_iam_role.ms-node.name
}
resource "aws_iam_role_policy_attachment" "ms-node-AmazonEKS_CNI_Policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
role = aws_iam_role.ms-node.name
}
resource "aws_iam_role_policy_attachment" "ms-node-AmazonEC2ContainerRegistryReadOnly" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
role = aws_iam_role.ms-node.name
}
resource "aws_eks_node_group" "ms-node-group" {
cluster_name = aws_eks_cluster.ms-up-running.name
node_group_name = "microservices"
node_role_arn = aws_iam_role.ms-node.arn
subnet_ids = var.nodegroup_subnet_ids
scaling_config {
desired_size = var.nodegroup_desired_size
max_size = var.nodegroup_max_size
min_size = var.nodegroup_min_size
}
disk_size = var.nodegroup_disk_size
instance_types = var.nodegroup_instance_types
depends_on = [
aws_iam_role_policy_attachment.ms-node-AmazonEKSWorkerNodePolicy,
aws_iam_role_policy_attachment.ms-node-AmazonEKS_CNI_Policy,
aws_iam_role_policy_attachment.ms-node-AmazonEC2ContainerRegistryReadOnly,
]
}
# Create a kubeconfig file based on the cluster that has been created
resource "local_file" "kubeconfig" {
content = <<KUBECONFIG
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: ${aws_eks_cluster.ms-up-running.certificate_authority.0.data}
server: ${aws_eks_cluster.ms-up-running.endpoint}
name: ${aws_eks_cluster.ms-up-running.arn}
contexts:
- context:
cluster: ${aws_eks_cluster.ms-up-running.arn}
user: ${aws_eks_cluster.ms-up-running.arn}
name: ${aws_eks_cluster.ms-up-running.arn}
current-context: ${aws_eks_cluster.ms-up-running.arn}
kind: Config
preferences: {}
users:
- name: ${aws_eks_cluster.ms-up-running.arn}
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
command: aws-iam-authenticator
args:
- "token"
- "-i"
- "${aws_eks_cluster.ms-up-running.name}"
KUBECONFIG
filename = "kubeconfig"
}
/*
# Use data to ensure that the cluster is up before we start using it
data "aws_eks_cluster" "msur" {
name = aws_eks_cluster.ms-up-running.id
}
# Use kubernetes provider to work with the kubernetes cluster API
provider "kubernetes" {
load_config_file = false
cluster_ca_certificate = base64decode(data.aws_eks_cluster.msur.certificate_authority.0.data)
host = data.aws_eks_cluster.msur.endpoint
exec {
api_version = "client.authentication.k8s.io/v1alpha1"
command = "aws-iam-authenticator"
args = ["token", "-i", "${data.aws_eks_cluster.msur.name}"]
}
}
# Create a namespace for microservice pods
resource "kubernetes_namespace" "ms-namespace" {
metadata {
name = var.ms_namespace
}
}
*/