-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
154 lines (135 loc) · 3.82 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
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "tls_private_key" "private_key" {
algorithm = "RSA"
}
resource "aws_key_pair" "aws_keypair" {
public_key = tls_private_key.private_key.public_key_openssh
}
resource "aws_security_group" "allow_kube_api_server" {
name = "${var.minikube_instance_name}-allow-kube-api-server"
description = "Allow incoming K8S API Server traffic"
ingress = [
{
description = "allow ssh"
from_port = 22
to_port = 22
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = []
prefix_list_ids = []
security_groups = []
self = false
},
{
description = "allow api server"
from_port = 8443
to_port = 8443
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = []
prefix_list_ids = []
security_groups = []
self = false
}
]
egress = [
{
description = "allow all"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
prefix_list_ids = []
security_groups = []
self = false
}
]
tags = {
Name = "allow_kube_api_server"
}
}
resource "aws_security_group" "allow_additional_exposed_ports" {
name = "${var.minikube_instance_name}-allow-additional-exposed-ports"
description = "Allow exposed ports from services"
ingress = [
for exposed_port in var.exposed_ports : {
description = "allow ports ${exposed_port.port}"
from_port = exposed_port.port
to_port = exposed_port.port
protocol = exposed_port.protocol
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
prefix_list_ids = []
security_groups = []
self = false
}
]
}
resource "aws_eip" "instance_elastic_ip" {}
data "aws_ec2_instance_type" "this" {
instance_type = var.instance_type
}
locals {
setup_minikube_command = <<EOT
/home/ubuntu/setup_minikube.sh \
--elastic-ip ${aws_eip.instance_elastic_ip.public_ip}
EOT
}
resource "aws_instance" "minikube_instance" {
ami = data.aws_ami.ubuntu.id
instance_type = data.aws_ec2_instance_type.this.id
key_name = aws_key_pair.aws_keypair.key_name
security_groups = [
aws_security_group.allow_kube_api_server.name,
aws_security_group.allow_additional_exposed_ports.name
]
tags = {
Name = var.minikube_instance_name
}
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.private_key.private_key_pem
host = self.public_ip
}
provisioner "file" {
source = "${path.module}/scripts/setup_minikube.sh"
destination = "/home/ubuntu/setup_minikube.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /home/ubuntu/setup_minikube.sh",
local.setup_minikube_command
]
}
}
resource "null_resource" "download_kubeconfig" {
depends_on = [
aws_instance.minikube_instance
]
triggers = {
"timestamp" = timestamp()
}
provisioner "local-exec" {
command = "${path.module}/scripts/download_kubeconfig.sh \"$PRIVATE_KEY\" ubuntu ${aws_eip.instance_elastic_ip.public_ip} ${var.kubeconfig_output_location}"
environment = {
PRIVATE_KEY = tls_private_key.private_key.private_key_pem
}
}
}
resource "aws_eip_association" "minikube_eip_assoc" {
instance_id = aws_instance.minikube_instance.id
allocation_id = aws_eip.instance_elastic_ip.id
}