packer {
  required_plugins {
    amazon = {
      version = ">= 1.2.5"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

source "amazon-ebs" "amazon_linux_2_x86" {
  ami_name      = "Jenkins common agent x86 v{{timestamp}}"
  instance_type = "t3.medium"
  region        = "us-east-1"

  source_ami_filter {
    filters = {
      name                = "amzn2-ami-hvm-2.0.*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
      architecture        = "x86_64"
    }
    most_recent = true
    owners      = ["amazon"]
  }

  ssh_username                = "ec2-user"
  vpc_id                      = "vpc-0ca801feb79f22108"
  subnet_id                   = "subnet-0a058fdbd4cf00890"
  security_group_id           = "sg-076308b4ea635bb92"
  associate_public_ip_address = true
  kms_key_id                  = "alias/default_ebs"
  encrypt_boot                = true

  launch_block_device_mappings {
    device_name = "/dev/xvda"
    encrypted   = true
    kms_key_id  = "alias/default_ebs"
    volume_size = 60
  }

  tags = {
    platform                 = "Delphi"
    environment              = "Common"
    project                  = "Infrastructure"
    service                  = "EC2"
    plat_env_project_service = "DLP_CMN_INFRA_EC2"
  }
  run_tags = {
    platform                 = "Delphi"
    environment              = "Common"
    project                  = "Infrastructure"
    service                  = "EC2"
    plat_env_project_service = "DLP_CMN_INFRA_EC2"
  }
  run_volume_tags = {
    platform                 = "Delphi"
    environment              = "Common"
    project                  = "Infrastructure"
    service                  = "EC2"
    plat_env_project_service = "DLP_CMN_INFRA_EC2"
  }
  snapshot_tags = {
    platform                 = "Delphi"
    environment              = "Common"
    project                  = "Infrastructure"
    service                  = "EC2"
    plat_env_project_service = "DLP_CMN_INFRA_EC2"
    Name                     = "Common Agent"
  }
}

source "amazon-ebs" "amazon_linux_2_arm64" {
  ami_name      = "Jenkins common agent arm64 v{{timestamp}}"
  instance_type = "t4g.medium"
  region        = "us-east-1"

  source_ami_filter {
    filters = {
      name                = "amzn2-ami-hvm-2.0.*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
      architecture        = "arm64"
    }
    most_recent = true
    owners      = ["amazon"]
  }

  ssh_username                = "ec2-user"
  vpc_id                      = "vpc-0ca801feb79f22108"
  subnet_id                   = "subnet-0a058fdbd4cf00890"
  security_group_id           = "sg-076308b4ea635bb92"
  associate_public_ip_address = true
  kms_key_id                  = "alias/default_ebs"
  encrypt_boot                = true

  launch_block_device_mappings {
    device_name = "/dev/xvda"
    encrypted   = true
    kms_key_id  = "alias/default_ebs"
    volume_size = 60
  }

  tags = {
    platform                 = "Delphi"
    environment              = "Common"
    project                  = "Infrastructure"
    service                  = "EC2"
    plat_env_project_service = "DLP_CMN_INFRA_EC2"
  }
  run_tags = {
    platform                 = "Delphi"
    environment              = "Common"
    project                  = "Infrastructure"
    service                  = "EC2"
    plat_env_project_service = "DLP_CMN_INFRA_EC2"
  }
  run_volume_tags = {
    platform                 = "Delphi"
    environment              = "Common"
    project                  = "Infrastructure"
    service                  = "EC2"
    plat_env_project_service = "DLP_CMN_INFRA_EC2"
  }
  snapshot_tags = {
    platform                 = "Delphi"
    environment              = "Common"
    project                  = "Infrastructure"
    service                  = "EC2"
    plat_env_project_service = "DLP_CMN_INFRA_EC2"
    Name                     = "Common Agent"
  }
}

build {
  name = "Jenkins common agent v{{timestamp}}"
  sources = [
    //"source.amazon-ebs.amazon_linux_2_x86",
    "source.amazon-ebs.amazon_linux_2_arm64",
  ]

  provisioner "ansible" {
    playbook_file = "./common.yml"
    use_proxy     = false
  }
}
