# AWS example: extend an approved Ubuntu VM image with the runtime installed.
# Build from a clean base image, never an active agent session VM.
packer {
  required_plugins {
    amazon = {
      source  = "github.com/hashicorp/amazon"
      version = ">= 1.0.0, < 2.0.0"
    }
  }
}

variable "region" { type = string }
variable "base_ami" { type = string }
variable "vpc_id" { type = string }
variable "subnet_id" { type = string }
variable "ssh_username" { type = string }
variable "instance_type" { type = string }

source "amazon-ebs" "agent" {
  region                      = var.region
  source_ami                  = var.base_ami
  vpc_id                      = var.vpc_id
  subnet_id                   = var.subnet_id
  instance_type               = var.instance_type
  ssh_username                = var.ssh_username
  ssh_interface               = "private_ip"
  associate_public_ip_address = false
  ami_name                    = "entergenie-agent-{{timestamp}}"
  encrypt_boot                = true
  imds_support                = "v2.0"
  metadata_options {
    http_endpoint = "enabled"
    http_tokens   = "required"
  }
  tags = { Purpose = "entergenie-agent" }
}

build {
  sources = ["source.amazon-ebs.agent"]
  provisioner "shell" {
    inline = [
      "sudo env DEBIAN_FRONTEND=noninteractive apt-get update",
      "sudo env DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git jq ca-certificates",
      "sudo rm -rf /var/lib/apt/lists/*",
      "git --version && jq --version"
    ]
  }
}
