forked from antonputra/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-example.tf.bac
70 lines (57 loc) · 1.6 KB
/
1-example.tf.bac
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
locals {
project_id = "devopsbyexample-325402"
network = "default"
image = "ubuntu-2004-focal-v20211212"
ssh_user = "ansible"
private_key_path = "~/.ssh/ansbile_ed25519"
}
provider "google" {
project = local.project_id
region = "us-central1"
}
resource "google_service_account" "nginx" {
account_id = "nginx-demo"
}
resource "google_compute_firewall" "web" {
name = "web-access"
network = local.network
allow {
protocol = "tcp"
ports = ["80"]
}
source_ranges = ["0.0.0.0/0"]
target_service_accounts = [google_service_account.nginx.email]
}
resource "google_compute_instance" "nginx" {
name = "nginx"
machine_type = "e2-micro"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = local.image
}
}
network_interface {
network = local.network
access_config {}
}
service_account {
email = google_service_account.nginx.email
scopes = ["cloud-platform"]
}
provisioner "remote-exec" {
inline = ["echo 'Wait until SSH is ready'"]
connection {
type = "ssh"
user = local.ssh_user
private_key = file(local.private_key_path)
host = google_compute_instance.nginx.network_interface.0.access_config.0.nat_ip
}
}
provisioner "local-exec" {
command = "ansible-playbook -i ${google_compute_instance.nginx.network_interface.0.access_config.0.nat_ip}, --private-key ${local.private_key_path} nginx.yaml"
}
}
output "nginx_ip" {
value = google_compute_instance.nginx.network_interface.0.access_config.0.nat_ip
}