Getting started Huawei Cloud + Terraform
Tags: terraform, devops, hcloud, tutorial
Published: 2024-04-14 01:55:55

🌐 Huawei Cloud is a cloud platform by Huawei, offering various services like computing, storage, and more.

🛠️ Integration with Terraform allows users to automate resource management on Huawei Cloud.

📝 With Terraform, users define infrastructure using configuration files, making resource management consistent and repeatable.

💻 Users can leverage Terraform features like state management and change planning for efficient cloud infrastructure management.

🚀 This integration provides a flexible and automated way to manage resources on Huawei Cloud.

## 1. Introduction to Huawei Cloud 🌐

Huawei Cloud is a global cloud computing platform that offers a wide range of cloud services. It is one of the leading cloud providers in the world, offering solutions for businesses of all sizes, from startups to large enterprises.

Huawei Cloud provides a variety of services, including:

- Compute: Elastic Compute Service (ECS), Bare Metal Server, and more. 💻

- Storage: Elastic Volume Service (EVS), Object Storage Service (OBS), and more. 📦

- Networking: Virtual Private Cloud (VPC), Elastic Load Balancing (ELB), and more. 🌐

- Databases: Relational Database Service (RDS), NoSQL Database Service (GaussDB), and more. 🗃️

- Security: Web Application Firewall (WAF), Cloud Trace Service (CTS), and more. 🔒

Some regions:

| **Región**              | **Code Name**  |
| ----------------------- | -------------- |
| China Beijing           | cn-north-1     |
| China Shanghai          | cn-east-2      |
| China Guangzhou         | cn-south-1     |
| Asia Pacific Singapore  | ap-southeast-1 |
| Asia Pacific Hong Kong  | ap-southeast-2 |
| Asia Pacific Bangkok    | ap-southeast-3 |
| Europe Frankfurt        | eu-central-1   |
| Latin America Sao Paulo | sa-east-1      |
| Russia Moscow           | ru-central-1   |
| Middle East Dubai       | me-east-1      |

## 2. Installing KooCLI on Linux 💻
KooCLI is a command-line interface (CLI) tool provided by Huawei Cloud that allows you to interact with the cloud platform directly from your local machine. KooCLI is an important tool for managing and automating your Huawei Cloud resources.

In this section, we'll go through the steps to install KooCLI on a Linux operating system, such as Ubuntu or CentOS.

**Installing KooCLI:** [Installing hcloud](https://support.huaweicloud.com/intl/en-us/qs-hcli/hcli_02_003_02.html)


Verify the installation: After the installation is complete, you can verify that KooCLI is installed correctly by running the following command:

```bash
> hcloud version
Current KooCLI version: 5.1.2
```

**Configuring KooCLI**
To use KooCLI, you'll need to configure it with your Huawei Cloud account credentials. Follow these steps:

Obtain your API credentials: Log in to the Huawei Cloud console and navigate to the "My Credentials" section to find your **access key ID and secret access key**.

![IAM Users](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xke5cji6qgzdodday7b7.png)

![Get Access Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3af785z4eahas0f6bwfl.png)

Table 1 Initialized parameters

| Parameter         | **Description**                                                        |
| ----------------- | ---------------------------------------|
| Access Key ID     | (Required) Access key ID (AK) of the access key (permanent AK/SK).     |
| Secret Access Key | (Required) Secret access key (SK) of the access key (permanent AK/SK). |
| Region            | (Required) Secret access key (SK) of the access key (permanent AK/SK). |

After typing in this command, press Enter to go to the interactive mode, and set the parameters as prompted.

```bash
hcloud configure init
```

```bash
hcloud configure init
Starting initialization. 'Secret Access Key' is anonymized. To obtain the parameter, see 'https://support.huaweicloud.com/intl/en-us/usermanual-hcli/hcli_09.html'.
Access Key ID [required]: H9NNF********SG65MXW
Secret Access Key [required]: ****
Secret Access Key (again): ****
Region: ap-southeast-1

********************************************************
*****                                              *****
*****           Initialization successful          *****
*****                                              *****
********************************************************

```

Testing commando hcloud:

```bash
> hcloud ecs ListServersDetails
{
  "count": 0,
  "servers": []
}
```

Setting hcloud profile, in this case "demo":

```bash
hcloud configure set --cli-profile=demo \
--cli-mode=AKSK --cli-region=ap-southeast-1 \
--cli-access-key=AccessKey --cli-secret-key=SecretKey \
--cli-project-id=ProjectID
```

more info about config profile in this [link](https://support.huaweicloud.com/intl/en-us/usermanual-hcli/hcli_03_003_01.html).

Now that you have KooCLI installed and configured, you can start using it to manage your Huawei Cloud resources from the command line. In the next section, we'll explore how to use Terraform with the Huawei Cloud provider.

## 3. Using Terraform with the Huawei Cloud Provider 🛠️

In this section, we'll explore how to use Terraform, a popular infrastructure-as-code (IaC) tool, to manage your resources on Huawei Cloud. Terraform allows you to define and provision your cloud infrastructure in a declarative way, making it easier to manage, version, and collaborate on your cloud deployments.

**Setting up the Huawei Cloud Provider in Terraform**
Terraform supports a wide range of cloud providers, including Huawei Cloud. To use Terraform with Huawei Cloud, you'll need to configure the Huawei Cloud provider plugin.

- Install Terraform: If you haven't already, download and install Terraform from the [official website](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli). Follow the instructions for your operating system.
- Configure the Huawei Cloud provider: Create a new Terraform configuration file (e.g., main.tf) and add the following provider block:

```hcl
provider "huaweicloud" {
  access_key = "your_access_key"
  secret_key = "your_secret_key"
  region     = "your_region"
}
```
Replace "your_access_key", "your_secret_key", and "your_region" with your actual Huawei Cloud credentials and the desired region or using the profile default in this case.

**Creating a Basic VPC:**
To establish the foundation of your network infrastructure on Huawei Cloud, we'll start by creating a Virtual Private Cloud (VPC) using Terraform.

Define the VPC: Begin by specifying the VPC configuration in your Terraform code. Use the following resource block in your vpc.tf and provider.tf files:

**provider.tf**

```hcl
terraform {
  required_providers {
    huaweicloud = {
      source  = "huaweicloud/huaweicloud" # https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs
      version = ">= 1.36.0"
    }
  }
}

provider "huaweicloud" {
  region             = "ap-southeast-1"
  shared_config_file = "/Users/$NAME/.hcloud/config.json" # Change Access Key and Secret Key decrypt
  profile            = "demo"
}
```
**vpc.tf**
```hcl
# Create a VPC
resource "huaweicloud_vpc" "vpc" {
  name = "vpc-web"
  cidr = "192.168.0.0/16" # Define the IP address range for your VPC
}
resource "huaweicloud_vpc_subnet" "subnet1" {
  name       = "subnet-web"
  cidr       = "192.168.10.0/24" # Define the IP address range for your subnet
  gateway_ip = "192.168.10.1"
  vpc_id     = huaweicloud_vpc.vpc.id
}
```

**Initialize Terraform:** Run the following command to initialize the Terraform working directory:

```hcl
terraform init
```

This will download the necessary provider plugins and prepare your Terraform environment.

Apply the Terraform configuration: Once the initialization is complete, you can create the VPC by running:

```hcl
terraform apply
```

Terraform will show you a preview of the resources it plans to create. Review the changes and type yes to confirm.

After the apply process is finished, Terraform will have created the VPC on Huawei Cloud according to your configuration.

![Terraform Apply](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k4q28y3by5miaw0a1gih.png)


![VPC Huawei Cloud 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90dyokb0ft7u9dofbq4o.png)


![VPC Huawei Cloud Description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gx3h1g7y7a0pdge3ysjm.png)


![VPC Huawei Cloud Topology](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31h5cvewpoh83ao6ugvr.png)


This is just a basic example to get you started. Terraform provides a wide range of resources and features for managing complex cloud infrastructures on Huawei Cloud. You can explore the Huawei Cloud provider documentation to learn more about the available resources and how to configure them.

**Note:**
**Running terraform destroy 🛠️**
To prevent unexpected costs and clean up resources, use terraform destroy. This command removes all provisioned infrastructure, ensuring no lingering resources remain. Execute it in your project directory:

```hcl
terraform destroy
```

Thank you for reading the post, and feel free to reach out if you have any questions! I'm here to assist. 🙌