-
Notifications
You must be signed in to change notification settings - Fork 0
/
alb.tf
135 lines (106 loc) · 3.05 KB
/
alb.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
resource "google_compute_instance" "alb" {
name = local.names.alb
machine_type = local.machine_type
zone = local.availability_zone
tags = ["alb"]
boot_disk {
initialize_params {
# image = "cos-cloud/cos-stable"
image = "debian-cloud/debian-12"
}
}
allow_stopping_for_update = true
network_interface {
network = google_compute_network.main.id
subnetwork = google_compute_subnetwork.public.id
access_config {
nat_ip = google_compute_address.alb.address
}
network_ip = local.ip[var.env].alb
}
metadata = {
enable-oslogin = "FALSE"
ssh-keys = "ludo:${tls_private_key.private.public_key_openssh}"
}
# metadata_startup_script = <<-EOF
# docker run -dp 80:80 --name alb \
# -e BACKEND_URL=10.0.2.2 \
# ${var.nginx_image}
# EOF
metadata_startup_script = <<-EOF
#!/bin/bash
apt-get update
apt-get install -y nginx
# Write the nginx.conf file content with placeholder
cat <<EOT > /tmp/nginx.conf
worker_processes auto;
error_log /var/log/nginx/error.log notice;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream backend {
server ${local.ip[var.env].app}${var.app_port};
}
upstream frontend {
server ${var.frontend_url};
}
server {
listen 80;
server_name localhost;
location /api {
proxy_pass http://backend;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_set_header Connection '';
chunked_transfer_encoding off;
}
location / {
proxy_pass http://frontend;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_set_header Connection '';
chunked_transfer_encoding off;
}
}
}
EOT
# Move the modified configuration to the correct location
mv /tmp/nginx.conf /etc/nginx/nginx.conf
# Restart nginx to apply the new configuration
systemctl restart nginx
EOF
}
resource "google_compute_firewall" "allow_alb_from_bastion" {
name = "${var.env}-allow-alb-from-bastion"
network = google_compute_network.main.name
source_tags = ["bastion"]
allow {
protocol = "tcp"
ports = ["22"]
}
}
resource "google_compute_firewall" "allow_alb_from_internet" {
name = "${var.env}-allow-alb-from-internet"
network = google_compute_network.main.name
source_ranges = ["0.0.0.0/0"]
allow {
protocol = "tcp"
ports = ["80", "443"]
}
allow {
protocol = "icmp"
}
}
resource "google_compute_address" "alb" {
name = "${var.env}-alb-ip"
region = var.region
}