It seems that Packer was only designed for specific build artifacts, Docker images and AMIs for example.
What I want is for Packer to spin up an instance with a specific IAM role in my own VPC, build Docker images and push to ECR. Also copy the compiled source code to an s3 bucket. Even copy certain files back to the CI tool to be used on subsequent builds.
This is all very manageable with Packer, except it annoyingly insists on saving an AMI at the end. Why is there no way of skipping that final step!? This has got me thinking that maybe I am using the wrong tool.
If so what is the correct tool? Is there maybe a Terraform module that can do the same? Years ago, I hacked around and ended up making a set of Terraform templates that worked with Gitlab CI for example.
Here is an example of a Gitlab CI template that works:
stages:
- build
.ci: &ci |
wget https://raw.githubusercontent.com/dnk8n/remote-provisioner/master/src/terraform/terraform.aws.main.tf
terraform init
terraform apply -auto-approve=true || JOB_STATUS=$?
terraform destroy -auto-approve=true
exit ${JOB_STATUS:-0}
variables:
TF_VAR_region: eu-west-1
TF_VAR_iam_instance_profile: builder
build-example:
stage: build
image:
name: hashicorp/terraform:0.12.5
entrypoint: [""]
variables:
TF_VAR_file_or_dir_source: ${CI_PROJECT_DIR}/
TF_VAR_file_or_dir_dest: /home/ubuntu/
TF_VAR_remote_command: '["chmod +x /home/ubuntu/path/to/build.sh", "/home/ubuntu/path/to/build.sh"]'
TF_VAR_instance_type: t3a.micro
TF_VAR_timeout_minutes: '10'
script:
- *ci
The following requirements are assumed:
-
${CI_PROJECT_DIR}/path/to/build.sh exists, where ${CI_PROJECT_DIR} is the root of your repo
- You want to send all the contents of your repo (
TF_VAR_file_or_dir_source) to /home/ubuntu on the remote instance (
TF_VAR_file_or_dir_dest) and you don't need anything else for the build
More explanation at
https://github.com/dnk8n/remote-provisionerI would honestly love to use Packer instead, but then skip the creation of AMIs which are unnecessary for my use case. I could use Packer directly in the CI tool to create a Docker image, but often that gives issues (because of CI tools running as docker containers themselves. It would be nice to have a 'remote-docker' Packer builder which works in a similar way to the ebs-builder but results in a Docker image in ECR instead of an AMI.