Updated on 2025-09-29 GMT+08:00

Terraform Configuration Guide

What Is Terraform?

Terraform is an infrastructure as code tool that you can use to build, change, and version CCI resources safely and efficiently. For more information, see Terraform.

Installing Terraform

Terraform is distributed as a single binary. Download a Terraform package and decompress it to a directory specified by the PATH environment variable of the OS.

Terraform supports multiple OSs, such as Linux, macOS, and Windows. You can log in to the Terraform official website, download the installation package of the corresponding OS, and add the executable Terraform file to the environment variable of the corresponding OS.

The following uses Linux as an example to describe how to install Terraform.

  1. Go to Terraform official website and download a Terraform package that matches your OS.
  2. Run the following command to decompress the package, grant the execute permission on Terraform, and save the package to the directory specified by PATH: $PATH indicates the specified directory (for example, /usr/local/bin). Replace it with the actual path.
    unzip terraform_1.xx.x_
    chmod +x ./terraform
    mv ./terraform $PATH
  3. Run the following command in the command-line interface (CLI) to check whether the path is correctly configured:
    terraform -version

    If the following information is displayed, the configuration is correct and the Terraform can run properly.

Authentication

Before using Terraform to manage CCI resources, you need to obtain the access key (AK) and secret key (SK) and configure them on Terraform for authentication.

You can configure Terraform using static credentials or environment variables.

Static credentials are simple to use. However, they require AKs and SKs to be stored in configuration files in plaintext, which risks secret leakage. It is recommended that you provide credentials as environment variables.

  • Static credentials
    provider "huaweicloud" {
      region      = "cn-north-4"
      access_key  = "my-access-key"
      seccret_key = "my-secret-key"
    }

    region: region where the resources are to be created and managed. You can query Huawei Cloud regions.

    access_key: access secret ID (AK). For details, see Access Keys.

    secret_key: secret access key (SK). For details, see Access Keys.

  • Environment variables

    Configure the region, AK, and SK as environment variables.

    export HW_REGION_NAME="cn-north-4"
    export HW_ACCESS_KEY="my-access-key"
    export HW_SECRET_KEY="my-secret-key"

    HW_REGION_NAME: region where the resources are to be created and managed. You can query Huawei Cloud regions.

    HW_ACCESS_KEY: access secret ID (AK). For details, see Access Keys.

    HW_SECRET_KEY: secret access key (SK). For details, see Access Keys.

Initializing the Working Directory

  1. Create a working directory.
    mkdir test
  1. Create the versions.tf file in the working directory and specify Huawei Cloud as the registry source and the version of Huawei Cloud Provider. The file content is as follows:
    terraform {
      required_providers {
        huaweicloud = {
          source = "huaweicloud/huaweicloud"
          version = ">=1.xx.xx" # Version of the provider to be loaded. You can use `=` to specify the version or use `>=` to specify the minimum provider version. The latest version is preferentially loaded.
        }
      }
    }

    For details about how to query the version, see Huawei Cloud Provider.

  1. Create the main.tf file and configure the Huawei Cloud provider. The file content is as follows:
    # Configure the HuaweiCloud Provider
    provider "huaweicloud" {
      region      = "cn-north-4"
      access_key  = "my-access-key"
      secret_key  = "my-secret-key"
    }

    This is an example configuration (including the AK/SK for authentication) of Huawei Cloud provider. For details on how to configure these parameters, see Authentication. If you provide credentials using environment variables, omit this part.

  2. Run the following command to perform initialization:
    terraform init

    If the following command output is displayed, Huawei Cloud Provider has been downloaded and installed when you run this command for the first time:

    If you need to modify the configuration in the versions.tf or main.tf file, run the following command to upgrade the file:

    terraform init -upgrade