Skip to content
Snippets Groups Projects
Commit 6a0995a6 authored by schnarkus's avatar schnarkus
Browse files

allocate a virtual machine in google cloud

parent 32731da5
No related branches found
No related tags found
2 merge requests!4build new image with ubuntu,!3allocate a virtual machine in google cloud
Pipeline #64333 passed
.local
artifact.bin
.terraform
*terraform.*
.ssh
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
}
1272 tofu init
1273 tofu apply
1274 tofu output instanceIPv4
1275 ssh -i ../.ssh/operator -l schnarkus $(tofu output -raw 'instanceIPv4')
1276 scp -i ../.ssh/operator ../artifact.bin schnarkus@$(tofu output -raw 'instanceIPv4'):~/webservice
1277 curl -s http://$(tofu output -raw 'instanceIPv4'):8080
1278 tofu destroy
HOST=0.0.0.0 PORT=8080 ./webservice
\ No newline at end of file
variable "sshPublicKeyPath" {
type = string
description = "Path to the public part of the SSH key pair"
default = "../.ssh/operator.pub"
}
variable "gcpCredentialsFilePath" {
type = string
description = "Path to a GCP credentials file (e.g., service account key)"
default = "~/.gcp/keyfile.json"
}
variable "projectID" {
type = string
description = "ID of a project within GCP"
default = "bht-devops24-ss"
}
variable "zone" {
type = string
description = "GCP zone to deploy resources in"
default = "europe-west3-b"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment