-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
252 lines (209 loc) · 6.4 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
# main.tf
provider "aws" {
region = var.aws_region
}
provider "kubernetes" {
host = data.aws_eks_cluster.cluster.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
token = data.aws_eks_cluster_auth.cluster.token
# load_config_file = false
}
data "aws_eks_cluster" "cluster" {
name = module.eks.cluster_name
}
data "aws_eks_cluster_auth" "cluster" {
name = module.eks.cluster_name
}
# Step 1: create the AWS ECR registry
resource "aws_ecr_repository" "demoapp" {
name = var.ecr_repository_name
# let's make images more secure
image_scanning_configuration {
scan_on_push = true
}
image_tag_mutability = "MUTABLE"
# TODO(mblume): regularly check-in below issue
# https://github.com/hashicorp/terraform-provider-aws/issues/33523
force_delete = true
# TODO(mblume): remove this once the issue is fixed
# https://github.com/hashicorp/terraform-provider-aws/issues/33523
# manually delete all images in the ECR repository on destroy
provisioner "local-exec" {
when = destroy
command = "./teardown.sh"
}
}
# ensure old images are deleted automatically to save space and cost
# from https://registry.terraform.io/providers/hashicorp/aws/2.33.0/docs/resources/ecr_lifecycle_policy
resource "aws_ecr_lifecycle_policy" "foopolicy" {
repository = "${aws_ecr_repository.demoapp.name}"
policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Expire images older than 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}
EOF
}
# Step 2: build and upload docker image to ECR
# Terraform itself does not directly support building or pushing Docker images.
# It is designed to manage infrastructure, not to build or push Docker images.
# However, you can use a null_resource with a local-exec provisioner to run shell commands.
resource "null_resource" "push_docker_image" {
depends_on = [aws_ecr_repository.demoapp]
provisioner "local-exec" {
command = <<EOF
$(aws ecr get-login --no-include-email --region ${var.aws_region})
docker build -t ${var.docker_iamge_name} .
docker tag ${var.docker_iamge_name}:latest ${aws_ecr_repository.demoapp.repository_url}:${var.docker_iamge_name}
docker push ${aws_ecr_repository.demoapp.repository_url}:${var.docker_iamge_name}
EOF
}
}
# Filter out local zones, which are not currently supported
# with managed node groups
data "aws_availability_zones" "available" {
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
locals {
cluster_name = "consor-eks-${random_string.suffix.result}"
}
resource "random_string" "suffix" {
length = 8
special = false
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "consor-vpc"
cidr = "10.0.0.0/16"
azs = slice(data.aws_availability_zones.available.names, 0, 3)
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
# IIRC, there should not be a public subnets in the past
# public_subnets = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
# public_subnet_tags = {
# "kubernetes.io/cluster/${local.cluster_name}" = "shared"
# "kubernetes.io/role/elb" = 1
# }
private_subnet_tags = {
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/internal-elb" = 1
}
}
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "19.15.3"
cluster_name = local.cluster_name
cluster_version = "1.27"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
cluster_endpoint_public_access = true
eks_managed_node_group_defaults = {
ami_type = "AL2_x86_64"
}
eks_managed_node_groups = {
one = {
name = "node-group-1"
instance_types = ["t3.small"]
min_size = 1
max_size = 3
desired_size = 2
}
two = {
name = "node-group-2"
instance_types = ["t3.small"]
min_size = 1
max_size = 2
desired_size = 1
}
}
}
# once created the EKS cluster, we need to configure kubectl
# to connect to it
# aws eks --region $(terraform output -raw region) update-kubeconfig --name $(terraform output -raw cluster_name)
resource "null_resource" "configure_kubectl" {
depends_on = [module.eks]
provisioner "local-exec" {
when = create
command = "aws eks --region ${var.aws_region} update-kubeconfig --name ${module.eks.cluster_name}"
}
}
resource "kubernetes_deployment_v1" "java" {
depends_on = [null_resource.configure_kubectl]
metadata {
name = "demoapp-deployment"
labels = {
app = "demoapp"
}
}
spec {
replicas = 3
selector {
match_labels = {
app = "demoapp"
}
}
template {
metadata {
labels = {
app = "demoapp"
}
}
spec {
container {
image = "${aws_ecr_repository.demoapp.repository_url}:${var.docker_iamge_name}"
name = "demoapp-container"
port {
container_port = 8080
}
}
}
}
}
}
resource "kubernetes_service" "java" {
depends_on = [kubernetes_deployment_v1.java]
metadata {
name = "demoapp-service"
}
spec {
selector = {
app = "demoapp"
}
port {
port = 8081
target_port = 8080
}
type = "LoadBalancer"
}
}
# Usueful commands once terraform is complete:
# Inspect nodes
# kubectl get nodes -o custom-columns=Name:.metadata.name
# run image on port 80
# kubectl run --port 80 --image ${aws_ecr_repository.demoapp.repository_url}:latest consor-demoapp
# Inspect list of pods:
# kubectl get pods
#NAME READY STATUS RESTARTS AGE
#demoapp-deployment-76b8bcc995-5twqp 1/1 Running 0 6m39s
#demoapp-deployment-76b8bcc995-vbsdv 1/1 Running 0 6m39s
# port forward to the pod via kubectl so you can reach it from localhost:8089
# kubectl port-forward deployment/demoapp-deployment 8089:8080