We use AWS Image Builder, a lot. It’s a super cool service if you need to build AMI or Container images. We used to have to use a collection of tools like Packer and Ansible to build and share images, but Image Builder has given us a single tool we can use for this purpose.
We provision our Image builder pipelines and resources using Terraform. We dynamically pick the latest AMIs as our base image on our recipes. This introduces a problem, every time the base image is updated we need to update the version number of the recipe, which means either doing code changes or changing parameters in our deployment pipeline.
Terraform’s time provider has a static time resource that can give you the date in various formats. To address the versioning issue, we decided to auto update the patch version with the UNIX timestamp when the latest AMI Id changes.
1 | resource "time_static" "build_number" { |
2 | triggers = { |
3 | ami_id = data.aws_ami.latest.id |
4 | } |
5 | } |
6 | |
7 | resource "aws_imagebuilder_image_recipe" "thing" { |
8 | ... |
9 | version = "1.0.${ceil(time_static.build_number.unix/100)}" |
10 | } |
view raw terraform-build-version-aws-imagebuilder.tf hosted with ❤ by GitHub
The snippet above gives you an idea of how we achieved this. This approach can be taken to update version numbers on various resource types, but ideally you would only update the version numberautomatically for things that you are fairly sure would not cause a breaking change.
We’d love hear how you solved this issue!
Comments