|
| 1 | +# Building AMI with Packer |
| 2 | + |
| 3 | +This directory contains the files for building AMI using [Packer](https://github.com/hashicorp/packer) that is later published as a AWS Marketplace asset. |
| 4 | + |
| 5 | + |
| 6 | +## Folder Structure |
| 7 | + |
| 8 | +- [hcl2-files](./hcl2-files/) - Includes different files which are used by a Packer pipeline to build an AMI. The files are: |
| 9 | + - [build.pkr.hcl](./hcl2-files/build.pkr.hcl): contains the [build](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/blocks/build) block, defining the builders to start, provisioning them using [provisioner](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/blocks/build/provisioner), and specifying actions to take with the built artifacts using `post-process`. |
| 10 | + - [variables.pkr.hcl](./hcl2-files/variables.pkr.hcl): contains the [variables](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/blocks/variable) block, defining variables within your Packer configuration. |
| 11 | + - [sources.pkr.hcl](./hcl2-files/sources.pkr.hcl): contains the [source](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/blocks/source) block, defining reusable builder configuration blocks. |
| 12 | + - [packer.pkr.hcl](./hcl2-files/packer.pkr.hcl): contains the [packer](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/blocks/packer) block, used to configure some behaviors of Packer itself, such as the minimum required Packer version needed to apply to your configuration. |
| 13 | +- [scripts](./scripts): contains scripts used by [provisioner](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/blocks/build/provisioner) for installing additonal packages/softwares. |
| 14 | + |
| 15 | + |
| 16 | +### Prerequisites |
| 17 | + - [Packer](https://developer.hashicorp.com/packer/docs/intro): Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration. |
| 18 | + |
| 19 | + - AWS Credentials: You need to have AWS credentials configured on your machine. You can configure AWS credentials using [AWS CLI](https://github.com/aws/aws-cli) or by setting environment variables. |
| 20 | + |
| 21 | + #### Install Packer on Ubuntu/Debian |
| 22 | + ```bash |
| 23 | + curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - |
| 24 | + sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" |
| 25 | + sudo apt-get update && sudo apt-get install packer |
| 26 | + ``` |
| 27 | + |
| 28 | +You can also install Packer for other OS from [here](https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli). |
| 29 | + |
| 30 | +#### Configure AWS Credentials |
| 31 | + |
| 32 | +Using Environment Variables: |
| 33 | +```bash |
| 34 | +export AWS_ACCESS_KEY_ID=<access_key> |
| 35 | +export AWS_SECRET_ACCESS_KEY=<secret_key> |
| 36 | +``` |
| 37 | + |
| 38 | +Using AWS CLI: |
| 39 | +```bash |
| 40 | +aws configure sso |
| 41 | +``` |
| 42 | + |
| 43 | +There are other ways to configure AWS credentials. You can read more about it [here](https://github.com/aws/aws-cli?tab=readme-ov-file#configuration). |
| 44 | + |
| 45 | +### Build AMI |
| 46 | + |
| 47 | +#### Format Packer blocks |
| 48 | +You can format your HCL2 files locally. This command will update your files in place. |
| 49 | + |
| 50 | +Format a single file: |
| 51 | +```bash |
| 52 | +packer fmt build.pkr.hcl |
| 53 | +``` |
| 54 | + |
| 55 | +Format all files in a directory: |
| 56 | +```bash |
| 57 | +packer fmt ./hcl2-files |
| 58 | +``` |
| 59 | + |
| 60 | +#### Validate Packer blocks |
| 61 | +You can validate the syntax and configuration of your files locally. This command will return a zero exit status on success, and a non-zero exit status on failure. |
| 62 | + |
| 63 | +```bash |
| 64 | +packer validate -var 'region=us-west-2' -var 'optimum_version=v0.0.17' ./hcl2-files |
| 65 | +``` |
| 66 | + |
| 67 | +#### Run Packer build |
| 68 | +You can run Packer locally. This command will build the AMI and upload it to AWS. |
| 69 | + |
| 70 | +You need to set variables with no default values using `-var` flag. For example: |
| 71 | +```bash |
| 72 | +packer build -var 'region=us-west-2' -var 'optimum_version=v0.0.17' ./hcl2-files |
| 73 | +``` |
| 74 | + |
| 75 | +To trigger a github action workflow manually, you can use GitHub CLI: |
| 76 | +```bash |
| 77 | +gh workflow run build-ami.yml -f tag=<tag> |
| 78 | +``` |
0 commit comments