The cat inside in the box
I find the need to make the same stage of a monolithic system for different stages,environments and customers. So what can I do to accelerate and standardize the process ?
The answer I find in the Hashicorp products as Terraform and Packer. I’m not going to talk about Terraform, because it is for another post, but we are going to talk about Packer. This is a solution for automating the making of images for the operating system for our infrastructure. That is, Packer is another tool for creating our infrastructure as code.
As a user of AWS, when I talk about images, I talk about AMI (Amazon Machine Image ). Our EC2, be to create an AMI as Ubuntu, Debian or Amazon Linux among many others AMI on the marketplace. Packer allows us from an AMI present or the scratch, for custom made to our needs.
Lets to code!
I recommend that you use Visual Code as IDE and that you install Terraform extension, to avoid mistakes in your code. Packer used HCL as a language, that base to “go templates”, before used to JSON but the version 1.6.2 was deprecated.
For this POC, I base it on Ubuntu Focal (20.04), in the zone us-east-1. First you have created an user of IAM with the roles required, generate your credential to configure the aws config in your home directory.
Second, let’s create a file with the name main.pkr.hcl, explain to the extension HCL, because the extension .pkr or .pkr.json to mention the JSON extension. Inside of that file, it has three blocks of code by packer, source and build. In the packer block you define the AWS plugin with your version, in the source block define the specification for your instance and the build you should define that script or binary are going to install.
For example:
Later to create the file, we install the latest packer version. Now when I am writing this post, the latest version is v1.7.8. To verifique the installation, let’s go to execute the next command.
packer version
This returns us the version if the installation is correct. After, let’s go to the folder of our code.
packer validate .
The command validate, allow us validate the syntax if not return any error we can continue or if return one or many error, you can look the message to identify the error.
Now, you can create the AMI. If you did the steps previous , you would not have any problem. But if you have some problem, you can use google-fu to do troubleshooting. Now, execute to ..
packer build .
So right now we are creating an AMI that is defined in the code. First wake up an instance and install the package, after the packer creates a snapshot from the EBS and for the creation of AMI, the process would be a few minutes and we return an ID of the new AMI.
i hope you liked it, thanks for reading 🤓