Help Center> Terraform> Getting Started
Updated on 2022-02-10 GMT+08:00

Getting Started

This topic walks you through the process of installing Terraform and using Terraform to create HUAWEI CLOUD resources, such as VPC.

Installing Terraform

Terraform is distributed as a single binary. Download a Terraform package and decompress it to a directory included in your system's PATH.

  1. Go to Terraform and download a Terraform package that matches your operating system.
  2. Decompress the package and add the directory where the package is located to the system's PATH.
  3. Run the following command in the command-line interface (CLI) to check whether the path is correctly configured:

    terraform

    If the following information is displayed, the configuration is correct and Terraform is ready to run.

    Usage: terraform [-version] [-help] <command> [args]
    
    ....

Authentication

You can use Terraform to orchestrate diverse cloud resources on HUAWEI CLOUD. Before using Terraform, obtain AK/SK and configure Terraform to complete authentication.

You can provide credentials by using either static credentials or environment variables.

  • Static credentials

    Configure parameters region, access_key, and secret_key in the provider block. For example:

    provider "huaweicloud" {
      region     = "cn-north-1"
      access_key = "my-access-key"
      secret_key = "my-secret-key"
    }
    • region: region where the resources are to be created and managed. For details on regions supported by HUAWEI CLOUD, see Regions and Endpoints.
    • 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. For example:

    $ export HW_REGION_NAME="cn-north-1"
    $ 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. For details on regions supported by HUAWEI CLOUD, see Regions and Endpoints.
    • HW_ACCESS_KEY: access secret ID (AK). For details, see Access Keys.
    • HW_SECRET_KEY: secret access key (SK). For details, see Access Keys.

For details about more parameters, see https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs.

Using Terraform to Create a HUAWEI CLOUD VPC

This example shows how to create a HUAWEI CLOUD VPC using Terraform. The Terraform version is 0.13, and the HUAWEI CLOUD provider version is 1.20.0.

  1. Create the versions.tf file in the working directory and specify the registry source and version of HUAWEI CLOUD provider.

    terraform {
      required_providers {
        huaweicloud = {
          source = "huaweicloud/huaweicloud"
          version = ">= 1.20.0"
        }
      }
    }

    For details on how to use a local registry source, see How Do I Accelerate the Download of a HUAWEI CLOUD Provider.

  2. Create the main.tf file, configure the HUAWEI CLOUD provider, and create a VPC.

    # Configure the HUAWEI CLOUD provider.
    provider "huaweicloud" {
      region     = "cn-north-1"
      access_key = "my-access-key"
      secret_key = "my-secret-key"
    }
    
    # Create a VPC.
    resource "huaweicloud_vpc" "example" {
      name = "terraform_vpc"
      cidr = "192.168.0.0/16"
    }

    The first part configures the HUAWEI CLOUD provider and provides AK/SK for authentication. For details on how to configure these parameters, see Authentication. If you provide credentials using environment variables, omit this part.

    The second part describes a VPC resource named example. The VPC name displayed on HUAWEI CLOUD is terraform_vpc and the CIDR block is 192.168.0.0/16.

  3. Run the following command to perform initialization:

    terraform init

    The following command output is displayed. HUAWEI CLOUD Provider will be downloaded and installed when you run this command for the first time.

    $ terraform init
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Finding latest version of huaweicloud/huaweicloud
    - Installing huaweicloud/huaweicloud v1.20.0...
    ...
    Terraform has been successfully initialized!

  4. Run the following command to view the resources to be created:

    terraform plan

    Terraform printouts the resources to be created as follows:

    ...
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # huaweicloud_vpc.example will be created
      + resource "huaweicloud_vpc" "example" {
          + cidr   = "192.168.0.0/16"
          + id     = (known after apply)
          + name   = "terraform_vpc"
          + region = (known after apply)
          + routes = (known after apply)
          + shared = (known after apply)
          + status = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    ...

  5. Run the following command to create the resources:

    terraform apply

    Enter yes as prompted. The following information is displayed, indicating that the VPC named terraform_vpc has been created. You can check the VPC on the HUAWEI CLOUD console.

    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # huaweicloud_vpc.example will be created
      + resource "huaweicloud_vpc" "example" {
          + cidr   = "192.168.0.0/16"
          + id     = (known after apply)
          + name   = "terraform_vpc"
          + region = (known after apply)
          + routes = (known after apply)
          + shared = (known after apply)
          + status = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    huaweicloud_vpc.example: Creating...
    huaweicloud_vpc.example: Creation complete after 7s [id=ceab8267-38e5-4a4c-8065-12967ad9eb31]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Follow-up Operations

For details on how to create common HUAWEI CLOUD resources, see User Guide.