DEV Community

Cover image for Getting started Huawei Cloud + Terraform
francotel
francotel

Posted on

Getting started Huawei Cloud + Terraform

🌐 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

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

> hcloud version
Current KooCLI version: 5.1.2
Enter fullscreen mode Exit fullscreen mode

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

Get Access Key

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.

hcloud configure init
Enter fullscreen mode Exit fullscreen mode
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          *****
*****                                              *****
********************************************************

Enter fullscreen mode Exit fullscreen mode

Testing commando hcloud:

> hcloud ecs ListServersDetails
{
  "count": 0,
  "servers": []
}
Enter fullscreen mode Exit fullscreen mode

Setting hcloud profile, in this case "demo":

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

more info about config profile in this link.

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. 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:
provider "huaweicloud" {
  access_key = "your_access_key"
  secret_key = "your_secret_key"
  region     = "your_region"
}
Enter fullscreen mode Exit fullscreen mode

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

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

vpc.tf

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

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

terraform init
Enter fullscreen mode Exit fullscreen mode

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:

terraform apply
Enter fullscreen mode Exit fullscreen mode

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

VPC Huawei Cloud 1

VPC Huawei Cloud Description

VPC Huawei Cloud Topology

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:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)