-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.tf
121 lines (100 loc) · 2.89 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
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: Apache-2.0
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "=3.68.0"
}
}
}
provider "google" {
project = var.project
region = var.region
}
resource "google_compute_network" "hashicat" {
name = "${var.prefix}-vpc-${var.region}"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "hashicat" {
name = "${var.prefix}-subnet"
region = var.region
network = google_compute_network.hashicat.self_link
ip_cidr_range = var.subnet_prefix
}
resource "google_compute_firewall" "http-server" {
name = "${var.prefix}-default-allow-ssh-http"
network = google_compute_network.hashicat.self_link
allow {
protocol = "tcp"
ports = ["22", "80"]
}
// Allow traffic from everywhere to instances with an http-server tag
source_ranges = ["0.0.0.0/0"]
target_tags = ["http-server"]
}
resource "tls_private_key" "ssh-key" {
algorithm = "ED25519"
}
resource "google_compute_instance" "hashicat" {
name = "${var.prefix}-hashicat"
zone = "${var.region}-b"
machine_type = var.machine_type
boot_disk {
initialize_params {
image = "ubuntu-os-cloud/ubuntu-2204-lts"
}
}
network_interface {
subnetwork = google_compute_subnetwork.hashicat.self_link
access_config {
}
}
metadata = {
ssh-keys = "ubuntu:${chomp(tls_private_key.ssh-key.public_key_openssh)} terraform"
}
tags = ["http-server"]
labels = {
name = "hashicat"
}
}
resource "null_resource" "configure-cat-app" {
depends_on = [
google_compute_instance.hashicat,
]
triggers = {
build_number = timestamp()
}
provisioner "file" {
source = "files/"
destination = "/home/ubuntu/"
connection {
type = "ssh"
user = "ubuntu"
timeout = "300s"
private_key = tls_private_key.ssh-key.private_key_pem
host = google_compute_instance.hashicat.network_interface.0.access_config.0.nat_ip
}
}
provisioner "remote-exec" {
inline = [
"sudo apt -y update",
"sleep 15",
"sudo apt -y update",
"sudo apt -y install apache2",
"sudo systemctl start apache2",
"sudo chown -R ubuntu:ubuntu /var/www/html",
"chmod +x *.sh",
"PLACEHOLDER=${var.placeholder} WIDTH=${var.width} HEIGHT=${var.height} PREFIX=${var.prefix} ./deploy_app.sh",
"sudo apt -y install cowsay",
"cowsay Mooooooooooo!",
]
connection {
type = "ssh"
user = "ubuntu"
timeout = "300s"
private_key = tls_private_key.ssh-key.private_key_pem
host = google_compute_instance.hashicat.network_interface.0.access_config.0.nat_ip
}
}
}