-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
78 lines (63 loc) · 2.38 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
terraform {
required_version = ">= 0.15.3, < 2.0"
}
provider "google" {
project = var.project_id
region = var.region
}
provider "google-beta" {
project = var.project_id
region = var.region
}
# ----------------------------------------------------------------------------------------------------------------
# Retrieve zones
# ----------------------------------------------------------------------------------------------------------------
data "google_client_config" "main" {}
data "google_compute_zones" "main" {
project = data.google_client_config.main.project
region = var.region
}
# ----------------------------------------------------------------------------------------------------------------
# Create a VM for testing purposes.
# ----------------------------------------------------------------------------------------------------------------
resource "google_compute_instance" "workload_vm" {
count = (var.create_workload_vm ? 1 : 0)
name = "${local.prefix}workload-vm"
project = var.project_id
machine_type = "n2-standard-2"
zone = data.google_compute_zones.main.names[0]
allow_stopping_for_update = true
metadata = {
serial-port-enable = true
ssh-keys = "paloalto:${file(var.public_key_path)}"
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
subnetwork = module.vpc_trust.subnets_ids[0]
network_ip = cidrhost(var.cidr_trust, 5)
}
metadata_startup_script = <<SCRIPT
echo "while :" >> /network-check.sh
echo "do" >> /network-check.sh
echo " timeout -k 2 2 ping -c 1 8.8.8.8 >> /dev/null" >> /network-check.sh
echo " if [ $? -eq 0 ]; then" >> /network-check.sh
echo " echo \$(date) -- Online -- Source IP = \$(curl https://checkip.amazonaws.com -s --connect-timeout 1)" >> /network-check.sh
echo " else" >> /network-check.sh
echo " echo \$(date) -- Offline" >> /network-check.sh
echo " fi" >> /network-check.sh
echo " sleep 1" >> /network-check.sh
echo "done" >> /network-check.sh
chmod +x /network-check.sh
while ! ping -q -c 1 -W 1 google.com >/dev/null
do
echo "waiting for internet connection..."
sleep 10s
done
echo "internet connection available!"
apt update && apt install -y apache2
SCRIPT
}