Newer
Older
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
terraform {
required_version = ">= 1.0"
}
provider "google" {
project = var.projectID
zone = var.zone
credentials = file(var.gcpCredentialsFilePath)
}
locals {
sshUserName = "schnarkus"
}
resource "google_compute_network" "the_network" {
name = "the-network"
}
resource "google_compute_firewall" "gate_guardian" {
name = "gate-guardian"
network = google_compute_network.the_network.name
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = ["22", "8080"] # ssh localhost
}
source_ranges = ["0.0.0.0/0"]
}
resource "google_compute_instance" "schminstance" {
name = "schminstance"
machine_type = "e2-micro"
boot_disk {
initialize_params {
image = data.google_compute_image.image.self_link
}
}
network_interface {
network = google_compute_network.the_network.name
access_config {}
}
metadata = {
ssh-keys = "${local.sshUserName}:${file(var.sshPublicKeyPath)}"
}
}
data "google_compute_image" "image" {
family = "ubuntu-2004-lts"
project = "ubuntu-os-cloud"
}
# get ip and publish
output "instanceIPv4" {
description = "Public IP address of the Google Compute Engine instance"
value = google_compute_instance.schminstance.network_interface[0].access_config[0].nat_ip
}