Skip to content

Instantly share code, notes, and snippets.

@SwampDragons
Last active June 17, 2020 21:29
Show Gist options
  • Save SwampDragons/76e3067ef4fca6d26b1d1ae07fd3ad84 to your computer and use it in GitHub Desktop.
Save SwampDragons/76e3067ef4fca6d26b1d1ae07fd3ad84 to your computer and use it in GitHub Desktop.

To run this example, clone all files into their own directory, change directories into that directory, and run:

packer build -var-file="variables.pkrvars.hcl" .

If you modify the source_ami variable to be an empty string "", then the filter chosen will be different.

build {
sources = [
"source.amazon-ebs.first",
]
provisioner "shell" {
inline = [
"sleep 5"
]
}
post-processor "shell-local" {
inline = [
"sleep 5"
]
}
}
source "amazon-ebs" "first" {
// omg comments
ami_name = "hcl2-test"
region = "us-east-1"
instance_type = "t2.micro"
force_deregister = true
source_ami_filter {
filters = var.source_ami == "" ? var.filters : var.filters_source_ami
most_recent = true
owners = ["amazon"]
}
source_ami = var.source_ami
launch_block_device_mappings {
device_name = "/dev/xvda"
volume_size = 20
volume_type = "gp2"
delete_on_termination = "true"
}
launch_block_device_mappings {
device_name = "/dev/xvdf"
volume_size = 500
volume_type = "gp2"
delete_on_termination = true
encrypted = true
}
ami_regions = ["eu-central-1"]
run_tags = {
Name = "packer-something"
stack-name = "DevOps Tools"
}
communicator = "ssh"
ssh_pty = true
ssh_username = "ec2-user"
associate_public_ip_address = true
}
variable "filters" {}
variable "filters_source_ami" {}
variable "source_ami" {}
filters = {
"virtualization-type" = "hvm"
"name" = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2"
"root-device-type" = "ebs"
"tag:TemplateStatus" = "partytag"
}
filters_source_ami = {
"virtualization-type" = "hvm"
"name" = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2"
"root-device-type" = "ebs"
}
source_ami = "ami-00eb20669e0990cb4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment