1
0
Fork 0
mirror of https://github.com/imjasonh/terraform-playground synced 2026-07-08 07:44:57 +00:00
terraform-playground/container-vm/main.tf

66 lines
1.9 KiB
Terraform
Raw Normal View History

2025-11-27 22:34:05 -06:00
# Fetch the latest source image based on the input variables (defaulting to COS)
data "google_compute_image" "source_image" {
family = var.source_image_family
project = var.source_image_project
}
# Render the cloud-init YAML template using dynamic module inputs
locals {
rendered_cloud_init = templatefile("${path.module}/cloud-init.yaml.tftpl", {
containers = var.containers,
enable_logging = var.enable_logging,
project_id = var.project_id,
region = var.region,
2025-11-27 22:34:05 -06:00
})
}
# The Instance Template resource itself
resource "google_compute_instance_template" "container_vm_template" {
project = var.project_id
name_prefix = "systemd-container-tmpl-"
description = "Runs a container using Docker, managed by a systemd unit via cloud-init."
machine_type = var.machine_type
region = var.region
labels = var.labels
2025-11-27 22:34:05 -06:00
# Disk configuration using the fetched OS image
disk {
source_image = data.google_compute_image.source_image.self_link
auto_delete = true
boot = true
}
# Network configuration (private VM - no external IP)
2025-11-27 22:34:05 -06:00
network_interface {
network = var.network
subnetwork = var.subnetwork
2025-11-27 22:34:05 -06:00
}
# Service Account configuration (needed for Google Cloud API access, including image pulls)
service_account {
email = var.service_account_email
2025-11-27 22:34:05 -06:00
scopes = ["cloud-platform"]
}
# Metadata for cloud-init and observability
2025-11-27 22:34:05 -06:00
metadata = {
user-data = local.rendered_cloud_init
google-logging-enabled = tostring(var.enable_logging)
google-monitoring-enabled = tostring(var.enable_monitoring)
cos-metrics-enabled = tostring(var.enable_cos_metrics)
2025-11-27 22:34:05 -06:00
}
scheduling {
automatic_restart = true
on_host_maintenance = "MIGRATE"
}
# Enable shielded VM features for security
shielded_instance_config {
enable_secure_boot = true
enable_vtpm = true
enable_integrity_monitoring = true
}
2025-11-27 22:34:05 -06:00
}