4

I would like to define a single Packer template that consists of 3 builds; each one building upon the previous:

source "amazon-ebs" "base" {
  source_ami_filter {
    filters {
      ...
    }
}

build {
  name = "build_1"
  source "amazon-ebs.base" {
    ami_name = "build-1-ami"
  }
}

build {
  name = "build_2"
  source "amazon-ebs.base" {
    ami_name = "build-2-ami
    source_ami = build_1.output.ami_id
    source_ami_filter { filters = {} }
  }
}

build {
  name = "build_3"
  source "amazon-ebs.base" {
    ami_name = "base-3-ami
    source_ami = build_2.output.ami_id
    source_ami_filter { filters = {} }
  }
}

I know that it is possible to chain builders in some way (https://www.packer.io/guides/packer-on-cicd/pipelineing-builds#chaining-together-several-of-the-same-builders-to-make-save-points). However, that example is using Docker and doing it a little more indirectly.

Is it possible to refer directly to the AMI ID produced by a previous build stage? I know build_x.output.ami_id is incorrect but is there a syntax to allow it?

If so, I think I also need to override/unset the source_ami_filter from the source because the documentation says when source_ami and source_ami_filter are used together then source_ami has to meet the other criteria of the filter which won't necessarily be the case.

1 Answer 1

1

After trying in vain to do this same "chaining" myself, I finally believe the claim made in Packer's documentation that you link to:

Packer templates don't come with a custom "glue" to bind them together. We recommend using your CI system or wrapping scripts to connect the templates into a chain.

The largest problem lies in the fact that Packer builds for AWS AMIs require the source AMI ID in order to do pre-build validations. So if a single template will build two AMIs, neither of them can reference the AMI ID of the other, because that's unknown during pre-build validations.

2
  • Would it be better to build a raw image locally then upload to AWS?
    – Peter Turner
    Apr 20, 2023 at 12:43
  • @PeterTurner Yes, that would be an option. AWS accepts imports of several VM formats that packer supports. This would open up some chaining possibilities within packer, though again, there would still need to be some "glue" logic in a script outside packer for importing to AWS. Apr 20, 2023 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.