Falling back to source_ami when source_ami_filter is empty?

Hello everybody!

I have packer happily building AMIs as part of my CI pipeline, however I need to modify the behavior a bit and am not sure whether this can be solved natively in packer. Bit of a chicken-and-egg problem.

What I would like to happen is that when packer runs, it’ll use a source_ami_filter (or something similar) to see if there’s an existing AMI. If there is not, then it should fall back on either a different source_ami_filter, or a hard-coded source_ami.

The usecase is this:

  1. Packer runs for the first time in a new environment. It checks if there is an existing AMI built by us. There is not. It builds a new AMI based off of the latest ubuntu AMI instead.
  2. Packer runs for the second time in this environment. It checks if there is an existing AMI built by us. There is. It builds a new AMI based off of the latest packer-created AMI (from step 1).

If this isn’t possible, I could add a step in my pipeline, before packer, to do this logic via a bash script, and then when I do call packer, I could pass in -only to choose an appropriate source, or a -var with the source_ami to use.

Thank you!