DRY HCL Files

20 views
Skip to first unread message

Dave Gilmore

unread,
Nov 30, 2020, 9:48:47 AM11/30/20
to Packer
Hi Folks,

I've taken over a project that has been using Packer JSON files and I have converted them to HCL. I am not new to HCL as I use it for Terraform etc...

I would like to understand if there is a better way to define my sources, I am building both AWS and Azure Images with Packer. 

At the moment there are repeated definitions for each source, and the only difference is the name, the provisioner is use to pass in the configuration differences.

```
source "amazon-ebs" "aws-100" {
  access_key    = "${var.aws_access_key}"
  ami_name      = "Name 100 ${local.timestamp}"
  instance_type = "${var.aws_instance_type}"
  launch_block_device_mappings {
    delete_on_termination = "${var.ec2_os_volume_delete_on_termination}"
    device_name           = "${var.ec2_os_volume_device_name}"
    volume_size           = "${var.ec2_os_volume_size}"
    volume_type           = "${var.ec2_os_volume_type}"
  }
  launch_block_device_mappings {
    delete_on_termination = "${var.ec2_additional_volume_delete_on_termination}"
    device_name           = "${var.ec2_additional_volume_device_name}"
    volume_size           = "${var.ec2_additional_volume_size}"
    volume_type           = "${var.ec2_additional_volume_type}"
  }

  region       = "${var.aws_region}"
  secret_key   = "${var.aws_secret_key}"
  source_ami   = "${var.aws_src_ami}"
  ssh_pty      = true
  ssh_username = "${var.aws_ssh_user}"
  subnet_id    = "${var.aws_subnet}"
  vpc_id       = "${var.aws_vpc}"
}

source "amazon-ebs" "aws-1000" {
  access_key    = "${var.aws_access_key}"
  ami_name      = "Name 1000 ${local.timestamp}"
  instance_type = "${var.aws_instance_type}"
  launch_block_device_mappings {
    delete_on_termination = "${var.ec2_os_volume_delete_on_termination}"
    device_name           = "${var.ec2_os_volume_device_name}"
    volume_size           = "${var.ec2_os_volume_size}"
    volume_type           = "${var.ec2_os_volume_type}"
  }
  launch_block_device_mappings {
    delete_on_termination = "${var.ec2_additional_volume_delete_on_termination}"
    device_name           = "${var.ec2_additional_volume_device_name}"
    volume_size           = "${var.ec2_additional_volume_size}"
    volume_type           = "${var.ec2_additional_volume_type}"
  }
  region       = "${var.aws_region}"
  secret_key   = "${var.aws_secret_key}"
  source_ami   = "${var.aws_src_ami}"
  ssh_pty      = true
  ssh_username = "${var.aws_ssh_user}"
  subnet_id    = "${var.aws_subnet}"
  vpc_id       = "${var.aws_vpc}"
}
```
I'm certain there must be a more succinct way to achieve this, any tips?

Would be interested to see how I can keep these DRY and more succinct 

Thanks
Reply all
Reply to author
Forward
0 new messages