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

fmt and some small improvements #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions network-firewall.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@

# Allow http
resource "google_compute_firewall" "allow-http" {
name = "${var.app_name}-${var.app_environment}-fw-allow-http"
network = "${google_compute_network.vpc.name}"
name = "${var.app_name}-${var.app_environment}-fw-allow-http"
network = google_compute_network.vpc.name
source_ranges = var.source_ranges
allow {
protocol = "tcp"
ports = ["80"]
}
target_tags = ["http"]
target_tags = ["http"]
}

# allow https
resource "google_compute_firewall" "allow-https" {
name = "${var.app_name}-${var.app_environment}-fw-allow-https"
network = "${google_compute_network.vpc.name}"
name = "${var.app_name}-${var.app_environment}-fw-allow-https"
network = google_compute_network.vpc.name
source_ranges = var.source_ranges
allow {
protocol = "tcp"
ports = ["443"]
}
target_tags = ["https"]
target_tags = ["https"]
}

# allow ssh
resource "google_compute_firewall" "allow-ssh" {
name = "${var.app_name}-${var.app_environment}-fw-allow-ssh"
network = "${google_compute_network.vpc.name}"
name = "${var.app_name}-${var.app_environment}-fw-allow-ssh"
network = google_compute_network.vpc.name
source_ranges = var.source_ranges
allow {
protocol = "tcp"
ports = ["22"]
Expand All @@ -35,8 +38,9 @@ resource "google_compute_firewall" "allow-ssh" {

# allow rdp
resource "google_compute_firewall" "allow-rdp" {
name = "${var.app_name}-${var.app_environment}-fw-allow-rdp"
network = "${google_compute_network.vpc.name}"
name = "${var.app_name}-${var.app_environment}-fw-allow-rdp"
network = google_compute_network.vpc.name
source_ranges = var.source_ranges
allow {
protocol = "tcp"
ports = ["3389"]
Expand Down
6 changes: 3 additions & 3 deletions network-variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

# define GCP region
variable "gcp_region_1" {
type = string
type = string
description = "GCP region"
}

# define GCP zone
variable "gcp_zone_1" {
type = string
type = string
description = "GCP zone"
}

# define Public subnet
variable "public_subnet_cidr_1" {
type = string
type = string
description = "Public subnet CIDR 1"
}

3 changes: 2 additions & 1 deletion network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ resource "google_compute_network" "vpc" {
name = "${var.app_name}-${var.app_environment}-vpc"
auto_create_subnetworks = "false"
routing_mode = "GLOBAL"
project = var.app_project
}

# create public subnet
Expand All @@ -18,7 +19,7 @@ resource "google_compute_subnetwork" "public_subnet_1" {
# allow internal icmp (disable for better security)
resource "google_compute_firewall" "allow-internal" {
name = "${var.app_name}-${var.app_environment}-fw-allow-internal"
network = "${google_compute_network.vpc.name}"
network = google_compute_network.vpc.name
allow {
protocol = "icmp"
}
Expand Down
6 changes: 3 additions & 3 deletions provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ terraform {

provider "google" {
project = var.app_project
credentials = file(var.gcp_auth_file)
region = var.gcp_region_1
zone = var.gcp_zone_1
#credentials = file(var.gcp_auth_file)
region = var.gcp_region_1
zone = var.gcp_zone_1
}
5 changes: 3 additions & 2 deletions terraform.tfvars
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Application Definition
app_name = "kopicloud" #do NOT enter any spaces
app_environment = "test" # Dev, Test, Prod, etc
app_environment = "test" # Dev, Test, Prod, etc
app_domain = "kopicloud.com"
app_project = "kopicloud"

Expand All @@ -10,4 +10,5 @@ gcp_zone_1 = "europe-west1-b"
gcp_auth_file = "../auth/kopicloud-tfadmin.json"

# GCP Netwok
public_subnet_cidr_1 = "10.10.1.0/24"
public_subnet_cidr_1 = "10.10.1.0/24"
source_ranges = ["0.0.0.0/0"]
18 changes: 13 additions & 5 deletions variables-auth.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@

# GCP authentication file
variable "gcp_auth_file" {
type = string
type = string
description = "GCP authentication file"
}

# define GCP project name
variable "app_project" {
type = string
type = string
description = "GCP project name"
}

# define application name
variable "app_name" {
type = string
type = string
description = "Application name"
}

# define application domain
variable "app_domain" {
type = string
type = string
description = "Application domain"
}

# define application environment
variable "app_environment" {
type = string
type = string
description = "Application environment"
}

variable "source_ranges" {
type = list(string)
}
variable "image" {
type = string
default = "debian-12-bookworm-v20230609"
}
7 changes: 7 additions & 0 deletions vm-output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ output "vm-external-ip" {
output "vm-internal-ip" {
value = google_compute_instance.vm_instance_public.network_interface.0.network_ip
}
output "z_gcloud-compute-ssh-command" {
value = <<EOF

Connect to the VM using following command:
gcloud compute ssh --zone '${google_compute_instance.vm_instance_public.zone}' '${google_compute_instance.vm_instance_public.name}' --project '${google_compute_instance.vm_instance_public.project}'
EOF
}
14 changes: 7 additions & 7 deletions vm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Terraform plugin for creating random ids
resource "random_id" "instance_id" {
byte_length = 4
byte_length = 4
}

# Create VM #1
Expand All @@ -11,19 +11,19 @@ resource "google_compute_instance" "vm_instance_public" {
machine_type = "f1-micro"
zone = var.gcp_zone_1
hostname = "${var.app_name}-vm-${random_id.instance_id.hex}.${var.app_domain}"
tags = ["ssh","http"]
tags = ["ssh", "http"]

boot_disk {
initialize_params {
image = "ubuntu-os-cloud/ubuntu-1804-lts"
image = var.image
}
}

metadata_startup_script = "sudo apt-get update; sudo apt-get install -yq build-essential apache2"

network_interface {
network = google_compute_network.vpc.name
subnetwork = google_compute_subnetwork.public_subnet_1.name
access_config { }
network = google_compute_network.vpc.name
subnetwork = google_compute_subnetwork.public_subnet_1.name
access_config {}
}
}
}