Hugo Future Imperfect Slim

Tech Notes

Linux,MAC,Docker,Kubernetes,
CloudStack,AWS,GCP,AZURE,
Raspberrypi,VMWARE,GO,BOOKS,
NETFLIX,Movies,Seasons

7-Minute Read

Packer is an open source tool that enables you to create identical machine images for multiple platforms from a single source template. A common use case is creating “golden images” that teams across an organization can use in cloud infrastructure.

There 2 ways to write the packer code to build the image

 Using JSON formatted code
 Using HCL2 formatted code

In order to interact with Google cloud we have to create a service account in GCP

  1. Create a service account in google cloud, we will use the service account to deploy the images in Google cloud

Identify the project associated with your google cloud account


~❯ gcloud config get-value project                                                                                                                                                           
terraform-325207
  1. Replace the project id in the below commands

1 .gcloud iam service-accounts create packer   --project terraform-325207  --description="Packer Service Account"   --display-name="Packer Service Account"

Created service account [packer]


2. gcloud projects add-iam-policy-binding terraform-325207     --member=serviceAccount:packer@terraform-325207.iam.gserviceaccount.com    --role=roles/compute.instanceAdmin.v1

Updated IAM policy for project [terraform-325207].
bindings:
- members:
  - serviceAccount:packer@terraform-325207.iam.gserviceaccount.com
  role: roles/compute.instanceAdmin.v1
- members:
  - serviceAccount:service-1065616354729@compute-system.iam.gserviceaccount.com
  role: roles/compute.serviceAgent
- members:
  - serviceAccount:1065616354729-compute@developer.gserviceaccount.com
  - serviceAccount:1065616354729@cloudservices.gserviceaccount.com
  role: roles/editor
- members:
  - user:kiran.sit@gmail.com
  role: roles/owner
etag: BwXQ5hY047U=
version: 1


3. gcloud projects add-iam-policy-binding terraform-325207      --member=serviceAccount:packer@terraform-325207.iam.gserviceaccount.com     --role=roles/iam.serviceAccountUser              

Updated IAM policy for project [terraform-325207].
bindings:
- members:
  - serviceAccount:packer@terraform-325207.iam.gserviceaccount.com
  role: roles/compute.instanceAdmin.v1
- members:
  - serviceAccount:service-1065616354729@compute-system.iam.gserviceaccount.com
  role: roles/compute.serviceAgent
- members:
  - serviceAccount:1065616354729-compute@developer.gserviceaccount.com
  - serviceAccount:1065616354729@cloudservices.gserviceaccount.com
  role: roles/editor
- members:
  - serviceAccount:packer@terraform-325207.iam.gserviceaccount.com
  role: roles/iam.serviceAccountUser
- members:
  - user:kiran.sit@gmail.com
  role: roles/owner
etag: BwXQ5iqxahY=
version: 1



4. gcloud projects add-iam-policy-binding terraform-325207    --member=serviceAccount:packer@terraform-325207.iam.gserviceaccount.com    --role=roles/iap.tunnelResourceAccessor

Updated IAM policy for project [terraform-325207].
bindings:
- members:
  - serviceAccount:packer@terraform-325207.iam.gserviceaccount.com
  role: roles/compute.instanceAdmin.v1
- members:
  - serviceAccount:service-1065616354729@compute-system.iam.gserviceaccount.com
  role: roles/compute.serviceAgent
- members:
  - serviceAccount:1065616354729-compute@developer.gserviceaccount.com
  - serviceAccount:1065616354729@cloudservices.gserviceaccount.com
  role: roles/editor
- members:
  - serviceAccount:packer@terraform-325207.iam.gserviceaccount.com
  role: roles/iam.serviceAccountUser
- members:
  - serviceAccount:packer@terraform-325207.iam.gserviceaccount.com
  role: roles/iap.tunnelResourceAccessor
- members:
  - user:kiran.sit@gmail.com
  role: roles/owner
etag: BwXQ5jBDseY=
version: 1
  1. Create an API key for the service account

Home>IAM >Service account> click on the service account > ADD key > Save the json file in the folder

Using json format to create an image in Google cloud


~❯ mkdir -p  packer_templates
~❯ cd packer_templates
~❯ mkdir -p gcp

~❯ vi gcp-webserver.json

{
    "builders": [{
      "type": "googlecompute",
      "account_file": "terraform-325207-39ea61c5c3ce.json",
      "project_id": "terraform-325207",
      "source_image": "ubuntu-1804-bionic-v20211021",
      "ssh_username": "packer",    
      "zone": "us-central1-a",
      "region": "us-central1",
      "machine_type": "n1-standard-1",
      "image_description": "custom machine image",
      "image_name": "mypackerimage",
      "disk_size": 10,
      "network": "kiran-vpc2",
      "subnetwork": "kiran-vpc2"
      
    }],
    "provisioners": [
        {
          "type": "file",
          "source": "index.html",
          "destination": "/tmp/index.html"
        },
        {
         "type": "shell",
         "scripts": [
           "install.sh",
           "post-install.sh"
         ]
       }
     ]
  }

~❯ vi install.sh

 

 


#!/bin/sh -x

# Install nginx
sudo apt-get -yqq install nginx

# Create website directory
sudo mkdir -p /var/www/website

# Move index.html to directory
sudo mv /tmp/index.html /var/www/website/


 

~❯ cat post-install.sh

#!/bin/sh -x

# Run nginx at boot
sudo systemctl enable nginx

 

~❯ cat index.html   

 

<html>

 <head>

  <title>This is a website</title>

 </head>

 <body>

  <h1>This is a website</h1>

 </body>
</html

Validate the code


~/packer_templates/gcp ❯ packer validate gcp-webserver.json                                                                                                                                                                 
 
The configuration is valid.

Build The image

~/packer_templates/gcp ❯ packer build gcp-webserver.json


Using HCL 2 format to create an image in Google cloud

~❯vi gcp-webserver.pkr.hcl

packer {
  required_plugins {
    googlecompute = {
      version = ">= 0.0.1"
      source = "github.com/hashicorp/googlecompute"
    }
  }
}


source "googlecompute" "ubuntu" {
 account_file = "terraform-325207-39ea61c5c3ce.json"
 project_id  = "terraform-325207"
 source_image = "ubuntu-1804-bionic-v20211021"
 ssh_username = "packer"    
 zone ="us-central1-a"
 region = "us-central1"
 machine_type = "n1-standard-1"
 image_description = "custom machine image"
 image_name = "mypackerimage"
 disk_size = 10
 network = "kiran-vpc2"
 subnetwork = "kiran-vpc2" 




  
}

build {
  sources = ["sources.googlecompute.ubuntu"]


provisioner "file" {
    destination = "/tmp/index.html"
    source      = "index.html"
  }

  provisioner "shell" {
    scripts = ["install.sh", "post-install.sh"]
  }

    }

Validate the code


~/packer_templates/gcp ❯ packer validate gcp-webserver.pkr.hcl                                                                                                                                                               
The configuration is valid.

Initialize Packer configuration

~/packer_templates/gcp ❯ packer init 
.                                                                                                                                                                               
 
Installed plugin github.com/hashicorp/googlecompute v1.0.6 in "/opt/homebrew/bin/github.com/hashicorp/googlecompute/packer-plugin-googlecompute_v1.0.6_x5.0_darwin_arm64"

Build the Image

~/packer_templates/gcp ❯ packer build gcp-webserver.pkr.hcl                                                                                                                                                                     
googlecompute.ubuntu: output will be in this color.

==> googlecompute.ubuntu: Checking image does not exist...
==> googlecompute.ubuntu: Creating temporary RSA SSH key for instance...
==> googlecompute.ubuntu: Using image: ubuntu-1804-bionic-v20211021
==> googlecompute.ubuntu: Creating instance...
    googlecompute.ubuntu: Loading zone: us-central1-a
    googlecompute.ubuntu: Loading machine type: n1-standard-1
    googlecompute.ubuntu: Requesting instance creation...
    googlecompute.ubuntu: Waiting for creation operation to complete...
    googlecompute.ubuntu: Instance has been created!
==> googlecompute.ubuntu: Waiting for the instance to become running...
    googlecompute.ubuntu: IP: 34.121.55.19
==> googlecompute.ubuntu: Using SSH communicator to connect: 34.121.55.19
==> googlecompute.ubuntu: Waiting for SSH to become available...
==> googlecompute.ubuntu: Connected to SSH!
==> googlecompute.ubuntu: Uploading index.html => /tmp/index.html
    googlecompute.ubuntu: index.html 119 B / 119 B [======================================================================================================================================================================] 100.00% 1s
==> googlecompute.ubuntu: Provisioning with shell script: install.sh
==> googlecompute.ubuntu: + sudo apt-get -yqq install nginx
==> googlecompute.ubuntu: debconf: unable to initialize frontend: Dialog
==> googlecompute.ubuntu: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
==> googlecompute.ubuntu: debconf: falling back to frontend: Readline
==> googlecompute.ubuntu: debconf: unable to initialize frontend: Readline
==> googlecompute.ubuntu: debconf: (This frontend requires a controlling tty.)
==> googlecompute.ubuntu: debconf: falling back to frontend: Teletype
==> googlecompute.ubuntu: dpkg-preconfigure: unable to re-open stdin:
    googlecompute.ubuntu: Selecting previously unselected package libjpeg-turbo8:amd64.
    googlecompute.ubuntu: (Reading database ... 65629 files and directories currently installed.)
    googlecompute.ubuntu: Preparing to unpack .../00-libjpeg-turbo8_1.5.2-0ubuntu5.18.04.4_amd64.deb ...
    googlecompute.ubuntu: Unpacking libjpeg-turbo8:amd64 (1.5.2-0ubuntu5.18.04.4) ...
    googlecompute.ubuntu: Selecting previously unselected package fonts-dejavu-core.
    googlecompute.ubuntu: Preparing to unpack .../01-fonts-dejavu-core_2.37-1_all.deb ...
    googlecompute.ubuntu: Unpacking fonts-dejavu-core (2.37-1) ...
    googlecompute.ubuntu: Selecting previously unselected package fontconfig-config.
    googlecompute.ubuntu: Preparing to unpack .../02-fontconfig-config_2.12.6-0ubuntu2_all.deb ...
    googlecompute.ubuntu: Unpacking fontconfig-config (2.12.6-0ubuntu2) ...
    googlecompute.ubuntu: Selecting previously unselected package libfontconfig1:amd64.
    googlecompute.ubuntu: Preparing to unpack .../03-libfontconfig1_2.12.6-0ubuntu2_amd64.deb ...
    googlecompute.ubuntu: Unpacking libfontconfig1:amd64 (2.12.6-0ubuntu2) ...
    googlecompute.ubuntu: Selecting previously unselected package libjpeg8:amd64.
    googlecompute.ubuntu: Preparing to unpack .../04-libjpeg8_8c-2ubuntu8_amd64.deb ...
    googlecompute.ubuntu: Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
    googlecompute.ubuntu: Selecting previously unselected package libjbig0:amd64.
    googlecompute.ubuntu: Preparing to unpack .../05-libjbig0_2.1-3.1build1_amd64.deb ...
    googlecompute.ubuntu: Unpacking libjbig0:amd64 (2.1-3.1build1) ...
    googlecompute.ubuntu: Selecting previously unselected package libtiff5:amd64.
    googlecompute.ubuntu: Preparing to unpack .../06-libtiff5_4.0.9-5ubuntu0.4_amd64.deb ...
    googlecompute.ubuntu: Unpacking libtiff5:amd64 (4.0.9-5ubuntu0.4) ...
    googlecompute.ubuntu: Selecting previously unselected package libwebp6:amd64.
    googlecompute.ubuntu: Preparing to unpack .../07-libwebp6_0.6.1-2ubuntu0.18.04.1_amd64.deb ...
    googlecompute.ubuntu: Unpacking libwebp6:amd64 (0.6.1-2ubuntu0.18.04.1) ...
    googlecompute.ubuntu: Selecting previously unselected package libxpm4:amd64.
    googlecompute.ubuntu: Preparing to unpack .../08-libxpm4_1%3a3.5.12-1_amd64.deb ...
    googlecompute.ubuntu: Unpacking libxpm4:amd64 (1:3.5.12-1) ...
    googlecompute.ubuntu: Selecting previously unselected package libgd3:amd64.
    googlecompute.ubuntu: Preparing to unpack .../09-libgd3_2.2.5-4ubuntu0.5_amd64.deb ...
    googlecompute.ubuntu: Unpacking libgd3:amd64 (2.2.5-4ubuntu0.5) ...
    googlecompute.ubuntu: Selecting previously unselected package nginx-common.
    googlecompute.ubuntu: Preparing to unpack .../10-nginx-common_1.14.0-0ubuntu1.9_all.deb ...
    googlecompute.ubuntu: Unpacking nginx-common (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Selecting previously unselected package libnginx-mod-http-geoip.
    googlecompute.ubuntu: Preparing to unpack .../11-libnginx-mod-http-geoip_1.14.0-0ubuntu1.9_amd64.deb ...
    googlecompute.ubuntu: Unpacking libnginx-mod-http-geoip (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Selecting previously unselected package libnginx-mod-http-image-filter.
    googlecompute.ubuntu: Preparing to unpack .../12-libnginx-mod-http-image-filter_1.14.0-0ubuntu1.9_amd64.deb ...
    googlecompute.ubuntu: Unpacking libnginx-mod-http-image-filter (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Selecting previously unselected package libnginx-mod-http-xslt-filter.
    googlecompute.ubuntu: Preparing to unpack .../13-libnginx-mod-http-xslt-filter_1.14.0-0ubuntu1.9_amd64.deb ...
    googlecompute.ubuntu: Unpacking libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Selecting previously unselected package libnginx-mod-mail.
    googlecompute.ubuntu: Preparing to unpack .../14-libnginx-mod-mail_1.14.0-0ubuntu1.9_amd64.deb ...
    googlecompute.ubuntu: Unpacking libnginx-mod-mail (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Selecting previously unselected package libnginx-mod-stream.
    googlecompute.ubuntu: Preparing to unpack .../15-libnginx-mod-stream_1.14.0-0ubuntu1.9_amd64.deb ...
    googlecompute.ubuntu: Unpacking libnginx-mod-stream (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Selecting previously unselected package nginx-core.
    googlecompute.ubuntu: Preparing to unpack .../16-nginx-core_1.14.0-0ubuntu1.9_amd64.deb ...
    googlecompute.ubuntu: Unpacking nginx-core (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Selecting previously unselected package nginx.
    googlecompute.ubuntu: Preparing to unpack .../17-nginx_1.14.0-0ubuntu1.9_all.deb ...
    googlecompute.ubuntu: Unpacking nginx (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Setting up libjbig0:amd64 (2.1-3.1build1) ...
    googlecompute.ubuntu: Setting up fonts-dejavu-core (2.37-1) ...
    googlecompute.ubuntu: Setting up nginx-common (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: debconf: unable to initialize frontend: Dialog
    googlecompute.ubuntu: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
    googlecompute.ubuntu: debconf: falling back to frontend: Readline
    googlecompute.ubuntu: Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
    googlecompute.ubuntu: Setting up libjpeg-turbo8:amd64 (1.5.2-0ubuntu5.18.04.4) ...
    googlecompute.ubuntu: Setting up libnginx-mod-mail (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Setting up libxpm4:amd64 (1:3.5.12-1) ...
    googlecompute.ubuntu: Setting up libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Setting up libnginx-mod-http-geoip (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Setting up libwebp6:amd64 (0.6.1-2ubuntu0.18.04.1) ...
    googlecompute.ubuntu: Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
    googlecompute.ubuntu: Setting up fontconfig-config (2.12.6-0ubuntu2) ...
    googlecompute.ubuntu: Setting up libnginx-mod-stream (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Setting up libtiff5:amd64 (4.0.9-5ubuntu0.4) ...
    googlecompute.ubuntu: Setting up libfontconfig1:amd64 (2.12.6-0ubuntu2) ...
    googlecompute.ubuntu: Setting up libgd3:amd64 (2.2.5-4ubuntu0.5) ...
    googlecompute.ubuntu: Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Setting up nginx-core (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Setting up nginx (1.14.0-0ubuntu1.9) ...
    googlecompute.ubuntu: Processing triggers for systemd (237-3ubuntu10.52) ...
    googlecompute.ubuntu: Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
    googlecompute.ubuntu: Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ...
    googlecompute.ubuntu: Processing triggers for ureadahead (0.100.0-21) ...
    googlecompute.ubuntu: Processing triggers for libc-bin (2.27-3ubuntu1.4) ...
==> googlecompute.ubuntu: + sudo mkdir -p /var/www/website
==> googlecompute.ubuntu: + sudo mv /tmp/index.html /var/www/website/
==> googlecompute.ubuntu: Provisioning with shell script: post-install.sh
==> googlecompute.ubuntu: + sudo systemctl enable nginx
==> googlecompute.ubuntu: Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
==> googlecompute.ubuntu: Executing: /lib/systemd/systemd-sysv-install enable nginx
==> googlecompute.ubuntu: Deleting instance...
    googlecompute.ubuntu: Instance has been deleted!
==> googlecompute.ubuntu: Creating image...
==> googlecompute.ubuntu: Deleting disk...
    googlecompute.ubuntu: Disk has been deleted!
Build 'googlecompute.ubuntu' finished after 2 minutes 43 seconds.

==> Wait completed after 2 minutes 43 seconds

==> Builds finished. The artifacts of successful builds are:
--> googlecompute.ubuntu: A disk image was created: mypackerimage

Ref: https://blog.searce.com/build-machine-images-with-packer-on-google-cloud-platform-b43f77f1acd https://www.packer.io/docs/builders/googlecompute

https://www.youtube.com/watch?v=lSumUuZT_B8&t=943s

comments powered by Disqus

Recent Posts

Categories

About

I am a Software Engineer at Persistent Systems, working on Cloud Stack Orchestration and various tech related to Cloud Infra, container technology like Docker, Kubernetes