Cloud Computing 10 min read

How Packer and UCloud Simplify Automated Multi‑Cloud Image Building

This article explains how UCloud’s Packer builder automates the creation of consistent, versioned machine images across multiple cloud platforms, compares it with manual console methods, outlines the full lifecycle, provides practical JSON examples, and demonstrates integration with Terraform for seamless DevOps workflows.

UCloud Tech
UCloud Tech
UCloud Tech
How Packer and UCloud Simplify Automated Multi‑Cloud Image Building

Background

Cloud hosts are among the most frequently used cloud products. As the number of cloud hosts grows, ensuring consistent versioned deployments becomes a common challenge. Traditional manual or scripted deployments are complex and hard to guarantee consistency and availability.

What is Packer?

Packer is an open‑source tool from HashiCorp that automates the creation of immutable machine images for multiple cloud platforms using a single configuration file. It integrates with providers via custom builders, allowing fast, parallel, and consistent image generation.

UCloud Packer Builder

UCloud developed a builder that was adopted by the official Packer repository. Using Packer to create custom images reduces deployment time, improves reliability, and enhances automated deployment capabilities. HashiCorp released version 1.4.3 on August 14, which includes the UCloud builder.

Key Advantages

Rapid infrastructure deployment : Images can launch fully configured hosts in seconds.

Multi‑provider portability : The same image works across different clouds.

High stability : Errors are caught during image build rather than at runtime.

High testability : Built images can be smoke‑tested before production use.

High extensibility : Plugins allow integration of additional tools.

Comparison with Console‑Based Image Creation

Console creation requires manual steps, has low reusability, high operational complexity, and longer creation time. Packer uses configuration files, offers high reusability, low complexity, and faster automated workflows.

Lifecycle

The full Packer image‑building cycle includes: creating a JSON template, running packer build, validating parameters, provisioning temporary resources, executing provisioners via SSH/WinRM, shutting down the host, creating and copying the image, cleaning up resources, and performing post‑processing.

Usage Example

A sample command to build an image with Packer:

packer build test.json

Sample Builder Configuration

The following JSON defines a UCloud UHost builder that creates a CentOS 7 VM, runs three shell scripts, and produces an image named consul‑server‑{{user `consul_version`}}:

{
  "variables": {
    "ucloud_public_key": "{{env `UCLOUD_PUBKEY`}}",
    "ucloud_private_key": "{{env `UCLOUD_SECRET`}}",
    "ssh_user": "root",
    "ssh_password": "password",
    "ucloud_project_id": "org-projectid",
    "image_id": "uimage-dpdgyw",
    "consul_version": "1.5.1",
    "region": "cn-bj2",
    "az": "cn-bj2-02"
  },
  "builders": [
    {
      "type": "ucloud-uhost",
      "public_key": "{{user `ucloud_public_key`}}",
      "private_key": "{{user `ucloud_private_key`}}",
      "project_id": "{{user `ucloud_project_id`}}",
      "region": "{{user `region`}}",
      "availability_zone": "{{user `az`}}",
      "instance_type": "n-basic-2",
      "source_image_id": "{{user `image_id`}}",
      "ssh_username": "{{user `ssh_user`}}",
      "ssh_password": "{{user `ssh_password`}}",
      "image_name": "consul-server-{{user `consul_version`}}"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "scripts": [
        "scripts/config-yum.sh",
        "scripts/consul-service.sh",
        "scripts/consul-server.sh"
      ],
      "environment_vars": [
        "CONSUL_VERSION={{user `consul_version`}}"
      ]
    }
  ]
}

Extending to Docker

By changing the builder type to docker, the same provisioning scripts can produce a Docker image, which is then tagged and pushed to a registry automatically.

{
  "variables": {
    "consul_version": "1.5.1"
  },
  "builders": [
    {
      "type": "docker",
      "image": "centos:7",
      "commit": true,
      "changes": [
        "CMD [\"tail -f /dev/null\"]",
        "ENTRYPOINT [\"\"]"
      ]
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "scripts": [
        "scripts/config-yum.sh",
        "scripts/consul-service.sh",
        "scripts/consul-server.sh"
      ],
      "environment_vars": [
        "CONSUL_VERSION={{user `consul_version`}}"
      ]
    }
  ],
  "post-processors": [
    [
      {
        "type": "docker-tag",
        "repository": "lonegunmanb/consulServer",
        "tag": "0.1"
      },
      "docker-push"
    ]
  ]
}

Packer + Terraform

Combining Packer with Git, Terraform, and UCloud CLI enables full IaC workflows: version‑controlled image definitions trigger Terraform to run Packer, Terraform consumes the built image, and then replaces old instances with new ones for continuous delivery.

Conclusion

UCloud’s integration of Packer provides automated environment configuration for cloud hosts, works with cloud‑init and other automation tools, and, together with Terraform and CLI, supports versioned, code‑driven infrastructure for precise, rapid, and consistent deployments.

Official documentation: https://www.packer.io/docs/builders/ucloud-uhost.html

DevOpsiacImage BuildingUCloudPacker
UCloud Tech
Written by

UCloud Tech

UCloud is a leading neutral cloud provider in China, developing its own IaaS, PaaS, AI service platform, and big data exchange platform, and delivering comprehensive industry solutions for public, private, hybrid, and dedicated clouds.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.