Building Your Own Image with Packer on Huawei Cloud
Tags: tutorial, hcloud, devops, packer
Published: 2024-04-14 21:43:19

## Introduction:
In this post, we'll explore how to use Packer to create custom images on Huawei Cloud. Custom images are invaluable for replicating specific environments, configurations, and software setups across instances. Let's dive in! :rocket:




## What is Packer?
**🌟 Origin:** Packer is an open-source tool developed by HashiCorp for automating the creation of machine images.

**🎯 Purpose:** It simplifies the creation of "golden images", which are pre-configured virtual machine images with specific software and settings, ideal for future deployments.

**🚀 Installation:** Packer can be easily installed on popular operating systems like Windows, macOS, and Linux by downloading the installation package from the [official Packer](https://developer.hashicorp.com/packer/install) website or using package managers like Homebrew on macOS or Chocolatey on Windows.

```bash
# MacOS
brew tap hashicorp/tap
brew install hashicorp/tap/packer
```

```bash
# Ubuntu/Debian
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install packer
```

Checking Packer:

```bash
> packer version
Packer v1.9.4

Your version of Packer is out of date! The latest version
is 1.10.2. You can update by downloading from www.packer.io/downloads
```

## ECS on Huawei Cloud:

Elastic Compute Service (ECS) is a core component of Huawei Cloud, offering scalable computing resources for deploying applications quickly and efficiently. With ECS, users can launch virtual servers on-demand, scale resources up or down as needed, and pay only for the resources they use. ECS provides a variety of instance types and configurations to suit different workloads and applications.

**ECS Flavor Naming Rules**
Figure 1 shows a flavor name, which consists of the instance generation, instance size, and memory/vCPU ratio.

![Flavor Naming](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7x13t1stjvjfhilbz0s.png)

**Instance types**
The instance type is named in the following format: CPU architecture + instance family + instance generation + additional capabilities.

This table describes the naming rule for the instance type.

| Application Scenarios | Segmented Scenarios | Instance Family | Description |
| --- | --- | --- | --- |
| General-purpose | General Computing-Basic | t | Turbo |
|  | General Computing | s | Standard |
|  | General Computing-plus | c | Compute |
| High-performance computing | High-Performance Computing | h | High Performance |
| Big data | Disk-intensive | d | Disk |
|  | Ultra-high I/O (large-capacity local disks) | i | IOPS |
|  | Ultra-high I/O (small-capacity local disks) | ir | IOPS Raid |
| Memory-intensive | Memory-optimized | m | Memory |
|  | Large-Memory | e | Enhanced Memory |
| Computing-accelerated | GPU computing-accelerated | p | Parallel |
|  | GPU graphics-accelerated | g | Graphic |
|  | GPU inference-accelerated | pl | Parallel Inference |
|  | FPGA-accelerated | fp | FPGA Performance |
|  | AI inference-accelerated | al | Ascend Inference |

Commonly Used Instance Types:

| **Type VM** | **vCPU** | **RAM (GB)** | **Price Aprox (USD/mes)** |
| --------------------- | -------- | ------------ | ------------------------------- |
| s6.small.1            | 1        | 2            | $7.93 - $14.23                  |
| s6.medium.2           | 2        | 4            | $15.86 - $28.46                 |
| s6.large.4            | 4        | 8            | $31.72 - $56.92                 |
| c6.large.2            | 2        | 4            | $21.58 - $38.84                 |
| c6.xlarge.4           | 4        | 8            | $43.16 - $77.68                 |
| m6.large.4            | 4        | 16           | $29.31 - $52.66                 |
| m6.xlarge.8           | 8        | 32           | $58.62 - $105.32                |
| p2.2xlarge.8          | 8        | 64           | $258.48 - $464.64               |
| p6.2xlarge.8          | 8        | 64           | $389.40 - $700.92               |
| g6.2xlarge.8          | 8        | 32           | $258.48 - $464.64               |

These are just a few examples of ECS instance types available on Huawei Cloud. Depending on your application requirements, you can choose the appropriate instance type to optimize performance and cost-effectiveness.

**Access Huawei Cloud Programmatically**
To interact with Huawei Cloud services programmatically, we'll use the KooCLI command-line tool. KooCLI is the official CLI client provided by Huawei Cloud, which allows you to manage and automate cloud resources more efficiently.

In this tutorial, we'll guide you through the installation and configuration of KooCLI. However, if you'd like more details on the process, we recommend visiting the "Getting Started with Huawei Cloud & Terraform" article on Dev.to, which explains the process in more depth:

[Install KooCli](https://dev.to/francotel/getting-started-huawei-cloud-terraform-47oc)

Once you've installed and configured KooCLI, you'll be able to start using the Huawei Cloud API to automate tasks, provision resources, and manage your cloud infrastructure more efficiently.

Let's proceed and see how to install and set up KooCLI on your operating system.

## Creating a Huawei Cloud Image with Packer 🛠️
Let's explore the steps to create a custom Huawei Cloud image using Packer.

**Create a Packer Configuration File 📝**
Start by creating a new file (e.g., hwcloud.pkr.hcl) and adding the following configuration:

After the Packer template is created, run the following command to import AK/SK:

```bash
export HW_ACCESS_KEY=<AccessKey ID>
export HW_SECRET_KEY=<AccessKey Secret>
```

Edit the file hwcloud-image.pkr.hcl:

```hcl
packer {
  required_plugins {
    huaweicloud = {
      version = ">= 1.0.0"
      source  = "github.com/huaweicloud/huaweicloud"
    }
  }
}

source "huaweicloud-ecs" "artifact" {
  region            = "ap-southeast-1"
  flavor            = "s6.medium.2"
  source_image_name = "Ubuntu 22.04 server 64bit"
  image_name        = "Ubuntu-2204-image-powered-by-Packer"
  image_tags = {
    builder = "packer"
    os      = "Ubuntu-22.04-server"
  }

  ssh_username       = "root"
  eip_type           = "5_bgp"
  eip_bandwidth_size = 5
}

build {
  sources = ["source.huaweicloud-ecs.artifact"]

  provisioner "shell" {
    inline = [
      "echo \"start install nginx, sleep 20s first\"",
      "sleep 20",
      "echo \"run install\"",
      "apt install -y nginx",
      "echo \"enable nginx\"",
      "systemctl enable nginx.service",
      "echo \"install nginx done\""
    ]
  }

  post-processor "manifest" {
    strip_path = true
    output     = "packer-result.json"
  }
}
```

Run the following command to create an image:

```bash
packer init hwcloud.pkr.hcl
```

```bash
packer build hwcloud.pkr.hcl
```

Packer will start the image creation process, which may take a few minutes to complete. During this time, Packer will:


![Build terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60ah9shtsgdp03ni41d1.png)


![Result Image ID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htpqayhdvelp4l5abe8i.png)


**Create a temporary Huawei Cloud instance.**
Provision the instance by running the specified shell commands (in this case, updating the package list and installing Nginx).
Create a new custom image from the provisioned instance.
Clean up the temporary resources.

**Verify the Created Image 👀**
Once the build process is finished, you can verify the created image in the Huawei Cloud console. Navigate to the "Images" section and you should see your custom image, ready to be used for launching new instances.

![Creating Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmls88wo37i3jez17izi.png)

![Creating Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq8b0fpfo82j1au4amet.png)

Congratulations! 🎉 You have successfully created a custom Huawei Cloud image using Packer. You can now use this image as a base for launching new instances or share it with your team.