What are the possible ways to build a packer image using terraform based on docker and use that image to AWS Lambda functions?
Terraform is designed to manage infrastructure via APIs. That’s a fairly different problem domain to building new versions of images and storing them in an image repository.
You should consider using some kind of CI runner, rather than trying to force Terraform to perform a job outside its intended applications.
Terraform and Packer are separate tools because although they interact with many of the same underlying APIs they have quite different workflow needs:
- Packer is for building immutable virtual machine (or container, etc) images that are constructed once and then used many times without modifying them. If changes need to be made, we typically construct a new image instead of editing the previous ones.
- Terraform is for managing long-lived objects that persist from one run to the next. It remembers a one-to-one relationship between resource instances in your configuration and objects in the underlying API, and so it can propose to update existing objects with new settings as your requirements change over time.
This relationship is an example of the typical distinction between “build” and “deploy”: during a build step we build an immutable artifact containing software to be run, and then during a deploy step we modify some existing infrastructure so that it will run that software. Packer and Terraform are typically used for a different kind of artifact than a traditional build/deploy pipeline would use, but the separation is still important.
With that in mind, you should treat the build step with Packer as something separate from Terraform. Once you’ve built an image you can refer to it in your Terraform configuration. If you are using HCP Packer then you can use
hcp_packer_image from the
hashicorp/hcp provider to declare the dependency on your Packer images. If you are not using HCP Packer then you can achieve a similar effect by using custom scripting to publish the Packer result metadata into a location where you can use a Terraform data source to retrieve it.