Hello Readers, We’ll see how we can create a Custom Image Using Packer In GCP in this blog. Before starting you must have to install Packer and we will use a service account to connect my google account, so make sure you have both things ready.
What is Packer?
Packer is a simple tool for building customized images. It is an open-source DevOps tool by Hashicorp to create identical machine images for multiple platforms from a single JSON config file. A common use case is to create a golden image that teams can use across an organization on a cloud infrastructure. We can use packer for AWS, GCP, AZURE, etc.
If you don’t have a packer on your machine refer to the official packer documents and Install the packer.
https://www.packer.io/downloads
Create A Service Account in GCP
A service account is a special type of Google account that is associated with an application or VM, instead of an individual end-user.
- From the search box search IAM & admin.
- Under IAM sections select Service Accounts
- Click on Create Service Account
- Give the name of the Service Account and press CREATE AND CONTINUE.
- Now give the following permission .
Now Download the ServiceAccount key for authentication.
Create a Sample Packer Template for Google Cloud
The packer templates are written in JSON format. So create a .json file and configure the custom image.
{
"builders": [
{
"type": "googlecompute",
"account_file": "/home/knoldus/Downloads/mythical-willow-342005-ed6b45a7f0d1.json",
"project_id": "mythical-willow-342005",
"source_image": "debian-9-stretch-v20200805",
"ssh_username": "packer",
"zone": "us-central1-a"
}
],
"provisioners": [
{
"type": "file",
"source": "hello.txt",
"destination": "/tmp/hello.txt"
}
]
}
Builders: The builder component let the packer know what platform or vendor we are building an image of, including API key information, a source image, and other information like region, zone, instance name, etc. For this blog, we are using google compute
builder.
Provisioners: Provisioner is here to tell packer what it has to install and configure inside VM after booting.
NOTE: Change the service account key file name,project_id, and give the right path of the service account key file.
Now, check whether the template is valid or not for validating run the below command:
$ packer validate <.jsonfile>
The configuration is valid.
Create a Custom image:
Run the following command to create a custom image:
$ packer build <.jsonfile>
You can see in the above image the custom image has been created and It does all the things we do manually for creating a custom image. When image creation will be finish it will delete the instance , you’ll be able to see your machine image Compute Engine->Images.
References:
https://www.packer.io/plugins/builders/googlecompute
Conclusion:
In this blog, we saw how to create a custom image using packer. I hope you liked it. Any suggestions or feedback let me know in the comment section. Thank you for sticking to the end. Please clap it, if you find it useful.