DEV Community

Cover image for Building Your Own Image with Packer on Huawei Cloud
francotel
francotel

Posted on

Building Your Own Image with Packer on Huawei Cloud

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! πŸš€

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 website or using package managers like Homebrew on macOS or Chocolatey on Windows.

# MacOS
brew tap hashicorp/tap
brew install hashicorp/tap/packer
Enter fullscreen mode Exit fullscreen mode
# 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
Enter fullscreen mode Exit fullscreen mode

Checking Packer:

> 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
Enter fullscreen mode Exit fullscreen mode

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

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

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:

export HW_ACCESS_KEY=<AccessKey ID>
export HW_SECRET_KEY=<AccessKey Secret>
Enter fullscreen mode Exit fullscreen mode

Edit the file hwcloud-image.pkr.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"
  }
}
Enter fullscreen mode Exit fullscreen mode

Run the following command to create an image:

packer init hwcloud.pkr.hcl
Enter fullscreen mode Exit fullscreen mode
packer build hwcloud.pkr.hcl
Enter fullscreen mode Exit fullscreen mode

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

Build terminal

Result Image ID

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

Creating Image 2

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.

Top comments (0)