-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
212 lines (172 loc) · 5.54 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
# Create simple VPC. One subnet with internet gateway
resource "aws_vpc" "nebari_sandbox_vpc" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "main"
}
}
# Create IGW and routes for it
resource "aws_internet_gateway" "nebari_sandbox_igw" {
vpc_id = aws_vpc.nebari_sandbox_vpc.id
}
resource "aws_route_table" "nebari_sandbox_route_table" {
vpc_id = aws_vpc.nebari_sandbox_vpc.id
}
resource "aws_route" "nebari_sandbox_route-public" {
destination_cidr_block = "0.0.0.0/0"
route_table_id = aws_route_table.nebari_sandbox_route_table.id
gateway_id = aws_internet_gateway.nebari_sandbox_igw.id
}
# Create subnet and associate public route table
resource "aws_subnet" "nebari_sandbox_subnet" {
vpc_id = aws_vpc.nebari_sandbox_vpc.id
cidr_block = "10.0.1.0/24"
}
resource "aws_route_table_association" "default" {
subnet_id = aws_subnet.nebari_sandbox_subnet.id
route_table_id = aws_route_table.nebari_sandbox_route_table.id
}
# Security group - allow SSH from user's home
resource "aws_security_group" "allow_ssh_home" {
name = "allow_ssh_home"
description = "Allow SSH connections from user home IP"
vpc_id = aws_vpc.nebari_sandbox_vpc.id
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${var.my_local_ip}/32"]
}
tags = {
Name = "allow_ssh_home"
}
}
# TODO - revise once all ports/protocols for Nebari resources are understood
# Security group - allow all
resource "aws_security_group" "allow_all_home" {
name = "allow_all_home"
description = "Allow all ports/protocols from user home IP"
vpc_id = aws_vpc.nebari_sandbox_vpc.id
ingress {
description = "All home"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
#cidr_blocks = ["${var.my_local_ip}/32"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_ssh_home"
}
}
# Security group - allow instance to reach internet
resource "aws_security_group" "allow_egress" {
name = "allow_egress"
description = "Allow outbound requests to internet"
vpc_id = aws_vpc.nebari_sandbox_vpc.id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_egress"
}
}
# Create EC2
# TODO - install nebari (this just installs docker) and figure out exposing outside of kind K8S cluster
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "nebari_sandbox_ec2" {
ami = data.aws_ami.ubuntu.id
instance_type = var.nebari_ec2_size
key_name = var.my_key_pair
subnet_id = aws_subnet.nebari_sandbox_subnet.id
vpc_security_group_ids = [aws_security_group.allow_ssh_home.id, aws_security_group.allow_egress.id, aws_security_group.allow_all_home.id]
tags = {
Name = "Nebari Local Deployment"
}
root_block_device {
delete_on_termination = true
volume_size = var.nebari_disk_size
}
# User data - install Docker, clone repos, upgrade dependencies
user_data = <<EOF
#!/bin/bash
# Install Docker
apt-get update
apt-get -y install ca-certificates curl gnupg
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
usermod -a -G docker ubuntu
# clone repos
for REPO in ${var.git_repos}
do
runuser -l ubuntu -c "git clone $REPO"
done
# Install and configure Nginx
apt-get -y install nginx
mkdir /etc/nginx/certs
openssl req -new -x509 -days 90 -newkey rsa:4096 -sha512 -subj "/C=US/ST=DC/L=Washington/O=Nebari/CN=nebari2.${var.my_route_53_domain}" -nodes -out /etc/nginx/certs/nginx.crt -keyout /etc/nginx/certs/nginx.key
cat > /etc/nginx/sites-enabled/nebari << EOL
server {
listen 443 ssl;
ssl_certificate /etc/nginx/certs/nginx.crt;
ssl_certificate_key /etc/nginx/certs/nginx.key;
server_name nebari2.${var.my_route_53_domain};
location / {
proxy_pass https://nebari.kflabs.click;
}
}
EOL
rm /etc/nginx/sites-enabled/default
echo "172.18.1.100 nebari.${var.my_route_53_domain}" | tee -a /etc/hosts
systemctl start nginx
# Install and Deploy Dependencies and MLFlow
apt-get -y install python3-pip
pip install pip==23.2
pip install pyopenssl --upgrade
pip install requests --upgrade
pip install nebari-plugin-label-studio-chart
EOF
}
# Create Elastic IP to associate with Nebari Sandbox EC2
resource "aws_eip" "nebari_sandbox_eip" {
instance = aws_instance.nebari_sandbox_ec2.id
}
# Create DNS record for Nebari
data "aws_route53_zone" "my_zone" {
name = var.my_route_53_domain
private_zone = false
}
resource "aws_route53_record" "nebari" {
zone_id = data.aws_route53_zone.my_zone.zone_id
name = "nebari"
=======
server_name nebari2.${var.my_route_53_domain};
location / {
proxy_pass https://nebari2.kflabs.click;
}
}