-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
108 lines (91 loc) · 3.08 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
resource "google_compute_address" "static" {
name = "static-ipv4-address"
}
resource "google_compute_network" "net_instance1" {
name = "tf-network-instance"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnet_instance1" {
name = "subnet"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
network = google_compute_network.net_instance1.self_link
}
resource "google_compute_global_address" "reserved_peering_range" {
project = var.project_id
name = "reserved-peering-range"
address_type = "INTERNAL"
prefix_length = 16
purpose = "VPC_PEERING"
network = google_compute_network.net_instance1.name
}
resource "google_service_networking_connection" "tf_network" {
depends_on = [google_compute_global_address.reserved_peering_range]
network = google_compute_network.net_instance1.name
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.reserved_peering_range.name]
}
// ############################ VM ############################
resource "google_compute_instance" "vm_instance1" {
name = "tf-vm-instance"
machine_type = "f1-micro"
tags = ["test", "dev", "ssh-server"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = google_compute_network.net_instance1.self_link
subnetwork = google_compute_subnetwork.subnet_instance1.self_link
access_config {
nat_ip = google_compute_address.static.address
}
}
metadata = {
ssh-keys = "${var.db_user1}:${file(var.ssh_key_file)}"
}
depends_on = [google_compute_network.net_instance1]
}
resource "google_compute_firewall" "fw_instance1" {
name = "tf-firewall-instance"
network = google_compute_network.net_instance1.name
direction = "INGRESS"
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["ssh-server"]
}
output "compute_instance_public_ip" {
value = google_compute_instance.vm_instance1.network_interface.0.access_config.0.nat_ip
}
// ############################ DB ############################
resource "google_sql_database_instance" "pg_instance1" {
name = "db-instance1"
region = "us-central1"
database_version = "POSTGRES_14"
depends_on = [google_service_networking_connection.tf_network]
settings {
tier = "db-f1-micro"
ip_configuration {
ipv4_enabled = false
private_network = google_compute_network.net_instance1.self_link
}
}
deletion_protection = false
}
resource "google_sql_database" "db_policy1" {
name = "db_policy1"
instance = google_sql_database_instance.pg_instance1.name
deletion_policy = "DELETE"
}
resource "google_sql_user" "db_user1" {
name = var.db_user1
instance = google_sql_database_instance.pg_instance1.name
password = var.db_password1
}
output "internal_ipv4" {
value = google_sql_database_instance.pg_instance1.private_ip_address
}