Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #154

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
59 changes: 59 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion files/deploy_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cat << EOM > /var/www/html/index.html
<!-- BEGIN -->
<center><img src="http://${PLACEHOLDER}/${WIDTH}/${HEIGHT}"></img></center>
<center><h2>Meow World!</h2></center>
Welcome to ${PREFIX}'s app. Replace this text with your own.
Welcome to sjh's app. Replace this text with your own.
<!-- END -->

</div>
Expand Down
242 changes: 121 additions & 121 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,121 +1,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
}
}
}
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: Apache-2.0

terraform {
required_providers {
google = {
source = "hashicorp/google"
}
}
}

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"
department = "devops"
}

}

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
}
}
}
9 changes: 9 additions & 0 deletions remote_backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "sjh"
workspaces {
name = "hashicat-gcp"
}
}
}
5 changes: 0 additions & 5 deletions terraform.tfvars.example

This file was deleted.

16 changes: 16 additions & 0 deletions vpc.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module "network" {
source = "terraform-google-modules/network/google"
version = "3.4.0"
# insert required variables here

network_name = "sjh"
project_id = "p-gti8ayw9nnlr-0"
subnets = [
{
subnet_name = "gaurav-subnet"
subnet_ip = "10.100.10.0/24"
subnet_region = var.region
}
]

}