|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + google = { |
| 4 | + source = "hashicorp/google" |
| 5 | + version = "~> 5.0" |
| 6 | + } |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +provider "google" { |
| 11 | + project = var.project_id |
| 12 | + region = var.region |
| 13 | + zone = var.zone |
| 14 | +} |
| 15 | + |
| 16 | +# Create a simple VM instance |
| 17 | +resource "google_compute_instance" "vm_instance" { |
| 18 | + name = var.instance_name |
| 19 | + machine_type = var.instance_type |
| 20 | + zone = var.zone |
| 21 | + |
| 22 | + boot_disk { |
| 23 | + initialize_params { |
| 24 | + image = "debian-cloud/debian-11" |
| 25 | + size = 20 |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + network_interface { |
| 30 | + network = var.vpc_network |
| 31 | + subnetwork = var.subnet_name |
| 32 | + |
| 33 | + access_config { |
| 34 | + # Ephemeral public IP |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + tags = ["http-server", "https-server"] |
| 39 | +} |
| 40 | + |
| 41 | +# Variables |
| 42 | +variable "project_id" { |
| 43 | + description = "GCP Project ID" |
| 44 | + type = string |
| 45 | +} |
| 46 | + |
| 47 | +variable "region" { |
| 48 | + description = "GCP Region" |
| 49 | + type = string |
| 50 | + default = "us-central1" |
| 51 | +} |
| 52 | + |
| 53 | +variable "zone" { |
| 54 | + description = "GCP Zone" |
| 55 | + type = string |
| 56 | + default = "us-central1-a" |
| 57 | +} |
| 58 | + |
| 59 | +variable "instance_name" { |
| 60 | + description = "Name of the VM instance" |
| 61 | + type = string |
| 62 | + default = "test-vm-instance" |
| 63 | +} |
| 64 | + |
| 65 | +variable "instance_type" { |
| 66 | + description = "Machine type for the instance" |
| 67 | + type = string |
| 68 | + default = "e2-medium" |
| 69 | +} |
| 70 | + |
| 71 | +variable "vpc_network" { |
| 72 | + description = "VPC Network name" |
| 73 | + type = string |
| 74 | + default = "default" |
| 75 | +} |
| 76 | + |
| 77 | +variable "subnet_name" { |
| 78 | + description = "Subnet name" |
| 79 | + type = string |
| 80 | + default = "default" |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +# Outputs |
| 85 | +output "instance_name" { |
| 86 | + description = "Name of the created instance" |
| 87 | + value = google_compute_instance.vm_instance.name |
| 88 | +} |
| 89 | + |
| 90 | +output "instance_ip" { |
| 91 | + description = "Public IP address of the instance" |
| 92 | + value = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip |
| 93 | +} |
| 94 | + |
| 95 | +output "instance_internal_ip" { |
| 96 | + description = "Internal IP address of the instance" |
| 97 | + value = google_compute_instance.vm_instance.network_interface[0].network_ip |
| 98 | +} |
0 commit comments