Usecase:- I'm trying to create a VM instance in gcp, with a disk size of 500gb. Here I want to vm instance with disk partition with given disk size through terraform module. I'm following this reference to create a simple VM -: instance https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance
Any helpful references and suggestions to create a Compute VM Instance with disk partitions in gcp using terraform module?
Hello @Dg03cloud ,Welcome on Google Cloud Community.
Compute disk module does not support partitioning by default. So here are your options:
1. Deploy VM with normal disk as provided example shows. Add metadata windows startup script, which will configure your VM AFTER deployment (best option). More info here: https://cloud.google.com/compute/docs/instances/startup-scripts/windows?hl=en#console
2. Deploy VM with additional disk as on example. Use null_resource and remote_exec to remotely execute PS script to configure your VM.
--
cheers,
DamianS
LinkedIn medium.com Cloudskillsboost
@DamianS Thanks for your quick reply. In this usecase, i want to create a linux vm instance resource through terraform. As suggested, will create a vm instance first, then will run the script ( given reference) for disk partition script. Here is my terraform configuration. Any additional changes are required on this configuration before creating a vm instance and then to run the disk partition disk script ?
resource "google_service_account" "default" {
account_id = "my-custom-sa"
display_name = "Custom SA for VM Instance"
}
resource "google_compute_instance" "default" {
name = "my-instance"
machine_type = "n2-standard-2"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
labels = {
my_label = "value"
}
}
}
// Local SSD disk
scratch_disk {
interface = "NVME"
}
network_interface {
network = "default"
access_config {
// Ephemeral public IP
}
}
metadata = {
foo = "bar"
}
metadata_startup_script = "echo hi > /test.txt"
service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}
Following this reference to create a vm instance through terraform. https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance.
Does the given reference supports to run a disk partition script on a Linux vm instance? Or is there any other reference to run a disk partition on a Linux vm?
Thanks.
Basically following TF code should work, as TF will create Linux instance AND then your shell script will do rest of magic. So I would suggest to focus on good shell script rather than TF code, as TF code will be the easiest part here.
--
cheers,
DamianS
LinkedIn medium.com Cloudskillsboost
Sure Damian.
Can we use this windows vm disk partition reference script to run on the linux VM instance for disk partition? [https://cloud.google.com/compute/docs/instances/startup-scripts/windows?hl=en#console]
Else, is there any other reference script to run on the linux VM machine for disk partition?
If yes, please share a reference script to run on the linux VM.
Thanks.
No, you can't use this Windows PS script for LInux. Unfortunately, I don't have it. Use any kind of AI tool to generate this, as it depends how you want to configure your filesystem structure.
@DamianS Have a followup question. While creation a simple gcp vm instance through terraform, is there a way to attach/create a secondary disk apart from a primary vm instance disk via terraform module. Any helpful references on this usecase?
@Dg03cloud
Sure thing that you are able to attach second disk. Grab this code :
# This code is compatible with Terraform 4.25.0 and versions that are backwards compatible to 4.25.0.
# For information about validating this Terraform code, see https://developer.hashicorp.com/terraform/tutorials/gcp-get-started/google-cloud-platform-build#format-and-validate-the-configuration
resource "google_compute_instance" "amazing-vm" {
attached_disk {
device_name = "amazing-vm-disk-additional"
mode = "READ_WRITE"
}
boot_disk {
auto_delete = true
device_name = "amazing-vm"
initialize_params {
image = "projects/debian-cloud/global/images/debian-12-bookworm-v20240515"
size = 10
type = "pd-balanced"
}
mode = "READ_WRITE"
}
can_ip_forward = false
deletion_protection = false
enable_display = false
labels = {
goog-ec-src="vm_add-tf"
}
machine_type = "e2-medium"
metadata = {
enable-oslogin = "true"
startup-script = " #! /bin/bash\n apt update\n apt -y install apache2\n cat <<EOF > /var/www/html/index.html\n <html><body><p>Linux startup script added directly.</p></body></html>\n EOF"
}
name = "amazing-vm"
network_interface {
access_config {
network_tier = "PREMIUM"
}
queue_count = 0
stack_type = "IPV4_ONLY"
subnetwork = "projects/webaap-wordpress-load/regions/us-central1/subnetworks/default"
}
scheduling {
automatic_restart = true
on_host_maintenance = "MIGRATE"
preemptible = false
provisioning_model = "STANDARD"
}
service_account {
email = "6935681435-compute@developer.gserviceaccount.com"
scopes = ["https://www.googleapis.com/auth/devstorage.read_only", "https://www.googleapis.com/auth/logging.write", "https://www.googleapis.com/auth/monitoring.write", "https://www.googleapis.com/auth/service.management.readonly", "https://www.googleapis.com/auth/servicecontrol", "https://www.googleapis.com/auth/trace.append"]
}
shielded_instance_config {
enable_integrity_monitoring = true
enable_secure_boot = false
enable_vtpm = true
}
zone = "us-central1-c"
}
PS: I will give you some hint. If you want to create terraform code for VM, you can simply go to GC Console -> Compute Engine -> Create VM -> Edit parameters -> EQUIVALENT CODE -> TERRAFORM. It will generate yaml code, which you can copy/paste and utilize as normal terraform file 🙂
PS2: You are not able to configure attached_disk size from "google_compute_instance" resource. So you should either create disk first, then make an reference in "google_compute_instance" resource.
So this code will handle all cases:
# This code is compatible with Terraform 4.25.0 and versions that are backwards compatible to 4.25.0.
# For information about validating this Terraform code, see https://developer.hashicorp.com/terraform/tutorials/gcp-get-started/google-cloud-platform-build#format-and-validate-the-configuration
### Initialize provider
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "5.30.0"
}
}
}
#######
## Create compute disk
resource "google_compute_disk" "seconddisk" {
name = "seconddisk"
type = "pd-standard"
zone = "us-central1-a" ## I'm using the same zone as for VM, as I'm not creating global disks.
size = "100" ## in GB
}
########
##Attach newly created disk to instance
resource "google_compute_attached_disk" "default" {
disk = google_compute_disk.seconddisk.self_link
instance = google_compute_instance.amazing-vm.id
}
## Create instance. This instance will be used for second disk attachment
resource "google_compute_instance" "amazing-vm" {
zone = "us-central1-a"
boot_disk {
auto_delete = true
device_name = "amazing-vm"
initialize_params {
image = "projects/debian-cloud/global/images/debian-12-bookworm-v20240515"
size = 10
type = "pd-balanced"
}
mode = "READ_WRITE"
}
can_ip_forward = false
deletion_protection = false
enable_display = false
labels = {
goog-ec-src="vm_add-tf"
}
machine_type = "e2-medium"
metadata = {
enable-oslogin = "true"
startup-script = " #! /bin/bash\n apt update\n apt -y install apache2\n cat <<EOF > /var/www/html/index.html\n <html><body><p>Linux startup script added directly.</p></body></html>\n EOF"
}
name = "amazing-vm"
network_interface {
access_config {
network_tier = "PREMIUM"
}
queue_count = 0
stack_type = "IPV4_ONLY"
subnetwork = "projects/webaap-wordpress-load/regions/us-central1/subnetworks/prod-gc" ### Change your VPC
}
lifecycle {
ignore_changes = [attached_disk]
}
scheduling {
automatic_restart = true
on_host_maintenance = "MIGRATE"
preemptible = false
provisioning_model = "STANDARD"
}
shielded_instance_config {
enable_integrity_monitoring = true
enable_secure_boot = false
enable_vtpm = true
}
}
###########
--
cheers,
DamianS
LinkedIn medium.com Cloudskillsboost
@DamianS Appreciate your reply. I will work with given reference config and update here. As suggested, I tried to run the 'EQUIVALENT CODE' on a new vm instance page, but I received this error.
@Dg03cloud
Might be possible that Google have some issue, hard to say. Try again tbh, as first piece of code which I've provided has been copy/paste from this EQUIVALENT CODE section 🙂
--
cheers,
DamianS
LinkedIn medium.com Cloudskillsboost