-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
84 lines (72 loc) · 1.67 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
# configura o provider
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "4.51.0"
}
}
}
# configura os dados do provider = GCP
provider "google" {
project = var.project
region = var.region
zone = var.zone
}
# cria a VPC - Network
resource "google_compute_network" "network-aula" {
name = "network-aula"
}
# cria IP público
resource "google_compute_address" "ip-aula" {
name = "ip-aula"
}
# cria Firewall e libera acesso SSH e HTTP
resource "google_compute_firewall" "firewall-aula" {
name = "firewall-aula"
network = google_compute_network.network-aula.name
target_tags = ["permite-ssh-http"]
source_ranges = ["0.0.0.0/0"]
allow {
protocol = "tcp"
ports = ["22", "80"]
}
}
# cria a VM
resource "google_compute_instance" "vm-aula" {
name = "vm-aula"
machine_type = "f1-micro"
tags = ["permite-ssh-http"]
# chave ssh
metadata = {
ssh-keys = "ubuntu:${file("id_rsa.pub")}"
}
# imagem do SO
boot_disk {
initialize_params {
image = "ubuntu-os-cloud/ubuntu-2204-lts"
}
}
# Associa a VM a VPC criada
network_interface {
network = google_compute_network.network-aula.name
access_config {
nat_ip = google_compute_address.ip-aula.address
}
}
}
# Atualiza o repositório e instala o nginx
resource "null_resource" "install_apache" {
connection {
type = "ssh"
user = var.user
private_key = file("id_rsa")
host = google_compute_address.ip-aula.address
}
provisioner "remote-exec" {
inline = [
"sudo apt update",
"sudo apt install nginx -y"
]
}
}