Huawei Cloud Terraform Provider User Manual

HuaweiCloud Terraform Provider
User Guide
Issue 01
Date 2021-03-11
HUAWEI TECHNOLOGIES CO., LTD.
Copyright © Huawei Technologies Co., Ltd. 2021. All rights reserved.
No part of this document may be reproduced or transmitted in any form or by any means without prior written consent of Huawei Technologies Co., Ltd.
Trademarks and Permissions
and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd. All other trademarks and trade names mentioned in this document are the property of their respective holders.
Notice
The purchased products, services and features are stipulated by the contract made between Huawei and the customer. All or part of the products, services and features described in this document may not be within the purchase scope or the usage scope. Unless otherwise specied in the contract, all statements, information, and recommendations in this document are provided "AS IS" without warranties, guarantees or representations of any kind, either express or implied.
The information in this document is subject to change without notice. Every eort has been made in the preparation of this document to ensure accuracy of the contents, but all statements, information, and recommendations in this document do not constitute a warranty of any kind, express or implied.
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. i
HuaweiCloud Terraform Provider User Guide Contents

Contents

1 HUAWEI CLOUD Provider Authentication......................................................................... 1
2 Elastic Cloud Server (ECS)..................................................................................................... 3
2.1 Creating an ECS....................................................................................................................................................................... 3
2.2 Adding an EVS Disk................................................................................................................................................................ 4
2.3 Binding an EIP.......................................................................................................................................................................... 5
3 Auto Scaling (AS).................................................................................................................... 6
4 Virtual Private Cloud (VPC)................................................................................................10
4.1
Conguring the Network................................................................................................................................................... 10
4.2 Binding a Virtual IP Address..............................................................................................................................................12
5 NAT Gateway......................................................................................................................... 15
6 Object Storage Service (OBS).............................................................................................18
6.1 Performing Basic Operations............................................................................................................................................ 18
Conguring Static Website Hosting............................................................................................................................... 20
6.2
7 Cloud Container Engine (CCE)........................................................................................... 23
7.1 Creating a CCE Cluster........................................................................................................................................................ 23
7.2 Creating a CCE Node........................................................................................................................................................... 26
8 Relational Database Service (RDS)...................................................................................28
8.1 Creating an RDS MySQL DB Instance............................................................................................................................ 28
8.2 Binding an EIP to an RDS DB Instance.......................................................................................................................... 31
8.3 Adding a Read Replica........................................................................................................................................................ 33
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. ii
HuaweiCloud Terraform Provider User Guide 1 HUAWEI CLOUD Provider Authentication

1 HUAWEI CLOUD Provider Authentication

HUAWEI CLOUD Provider uses AK/SK for authentication. You can provide credentials as either static credentials or environment variables.
Static Credentials
Congure 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" }
Static credentials are simple to use. However, they require AKs and SKs to be stored in recommended that you provide credentials as environment variables.
conguration les in plaintext, which risks secret leakage. It is
Environment Variables
Congure 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"
After setting the environment variables, declare the HUAWEI CLOUD provider.
provider "huaweicloud" {}
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 1
HuaweiCloud Terraform Provider User Guide 1 HUAWEI CLOUD Provider Authentication
Parameter Description
Table 1-1 Provider authentication parameters
Parameter Manda
tory
region Yes HW_REGION_NAME Region where the HUAWEI
access_key Yes HW_ACCESS_KEY Access key ID of a user. For
secret_key Yes HW_SECRET_KEY Secret access key of a user. For
domain_nameNo HW_DOMAIN_NAMEHUAWEI CLOUD account name.
Environment Variable
Description
CLOUD service is located. For details, see Regions and
Endpoints.
If you want to create cloud services in
congure parameter alias or region for the resource
corresponding to the cloud service.
details on how to obtain an access key ID, see Access Keys.
details on how to obtain a secret access key, see Access Keys.
For details on how to obtain an account name, see API
Credentials.
dierent regions,
project_nameNo HW_PROJECT_NAMEHUAWEI CLOUD project name.
For details on how to obtain a project name, see API
Credentials.
enterprise_p roject_id
max_retries No HW_MAX_RETRIES Maximum number of retries
No HW_ENTERPRISE_P
ROJECT_ID
Enterprise project ID. For more information about enterprise projects and how to obtain enterprise project IDs, see
Enterprise Management User Guide.
allowed when a network transmission problem occurs. The default value is 5.
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 2
HuaweiCloud Terraform Provider User Guide 2 Elastic Cloud Server (ECS)

2 Elastic Cloud Server (ECS)

2.1 Creating an ECS

Application Scenario
An Elastic Cloud Server (ECS) is a basic computing unit that consists of vCPUs, memory, OS, and Elastic Volume Service (EVS) disks. After creating an ECS, you can use it like using your local computer or physical server. HUAWEI CLOUD provides a variety of ECS types for an ECS, select specications, image type, and disk type and congure network parameters and security group rules based on your scenario requirements.
Related Resources
huaweicloud_compute_instance
Procedure
Step 1 Use data source to query the AZ, ECS
parameters.
Create the main.tf le, enter the following information, and save the le:
data "huaweicloud_availability_zones" "myaz" {}
data "huaweicloud_compute_avors" "myavor" { availability_zone = data.huaweicloud_availability_zones.myaz.names[0] performance_type = "normal" cpu_core_count = 2 memory_size = 4 }
dierent scenario requirements. When creating
specications, image, and network
data "huaweicloud_images_image" "myimage" { name = "Ubuntu 18.04 server 64bit" most_recent = true }
data "huaweicloud_vpc_subnet" "mynet" { name = "subnet-default" }
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 3
HuaweiCloud Terraform Provider User Guide 2 Elastic Cloud Server (ECS)
Step 2 Create an ECS that supports login with a random password.
1. Add the following information to the main.tf le:
resource "random_password" "password" { length = 16 special = true override_special = "!@#$%*" }
resource "huaweicloud_compute_instance" "basic" { name = "basic" admin_pass = random_password.password.result image_id = data.huaweicloud_images_image.myimage.id
avor_id = data.huaweicloud_compute_avors.myavor.ids[0] availability_zone = data.huaweicloud_availability_zones.myaz.names[0] security_groups = ["default"]
network { uuid = data.huaweicloud_vpc_subnet.mynet.id } }
2. Run terraform init to initialize the environment.
3. Run terraform plan to view resources.
4. After you conrm that the resource information is correct, run terraform apply to start ECS creation.
5. Run terraform show to view the created ECS.
----End
Sample Code
https://github.com/huaweicloud/terraform-provider-huaweicloud/blob/ master/examples/ecs/basic/main.tf

2.2 Adding an EVS Disk

Application Scenario
Create an EVS disk and attach it to the ECS.
Related Resources
huaweicloud_evs_volume
huaweicloud_compute_volume_attach
Procedure
Step 1 Add the following information to the main.tf
resource "huaweicloud_evs_volume" "myvolume" { name = "myvolume" availability_zone = data.huaweicloud_availability_zones.myaz.names[0] volume_type = "SAS" size = 10 } resource "huaweicloud_compute_volume_attach" "attached" { instance_id = huaweicloud_compute_instance.myinstance.id volume_id = huaweicloud_evs_volume.myvolume.id }
le:
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 4
HuaweiCloud Terraform Provider User Guide 2 Elastic Cloud Server (ECS)
Step 2 Run terraform plan to view resources.
Step 3 After you conrm that the resource information is correct, run terraform apply to
start EVS creation.
Step 4 After the EVS disk is attached to the ECS, you need to initialize the disk before you
use it.
----End
Sample Code
https://github.com/huaweicloud/terraform-provider-huaweicloud/blob/ master/examples/ecs/attached-volume/main.tf

2.3 Binding an EIP

Application Scenario
Purchase an EIP and bind it to the ECS.
Related Resources
huaweicloud_vpc_eip
huaweicloud_compute_eip_associate
Procedure
Step 1 Add the following information to the main.tf
resource "huaweicloud_vpc_eip" "myeip" { publicip { type = "5_bgp" } bandwidth { name = "mybandwidth" size = 8 share_type = "PER" charge_mode = } } resource "huaweicloud_compute_eip_associate" "associated" { public_ip = huaweicloud_vpc_eip.myeip.address instance_id = huaweicloud_compute_instance.myinstance.id }
Step 2 Run terraform plan to view resources.
le:
"trac"
Step 3 After you
conrm that the resource information is correct, run terraform apply to
purchase the EIP and bind the EIP to the ECS.
----End
Sample Code
https://github.com/huaweicloud/terraform-provider-huaweicloud/blob/ master/examples/ecs/associated-eip/main.tf
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 5
HuaweiCloud Terraform Provider User Guide 3 Auto Scaling (AS)

3 Auto Scaling (AS)

Application Scenarios
AS automatically adjusts service resources to keep up with your demand based on pre-congured AS policies. With automatic resource adjustment, you can enjoy reduced costs, improved availability, and high fault tolerance. AS applies to the following scenarios:
E-commerce: Large-scale e-commerce promotions can attract visits that may
Live streaming: A live streaming website broadcasts popular programs from
Related Resources
huaweicloud_as_group
huaweicloud_as_policy
huaweicloud_ces_alarmrule
Procedure
Step 1 Create an AS conguration.
Create the main.tf le, enter the following information, and save the le:
data "huaweicloud_availability_zones" "myaz" {}
Heavy-trac forums: Service load changes of a heavy-trac forum website are dicult to predict. AS dynamically adjusts the number of cloud servers based on monitored ECS metrics, such as vCPU Usage and Memory Usage.
break your website. AS automatically adds ECSs and increases bandwidth to ensure that promotions will go smoothly.
14:00 to 16:00 every day. AS automatically adds ECSs and increases bandwidth during this period to ensure smooth viewer experience.
huaweicloud_as_conguration
data "huaweicloud_compute_avors" "myavor" { availability_zone = data.huaweicloud_availability_zones.myaz.names[0] performance_type = "normal" cpu_core_count = 2 memory_size = 4 } data "huaweicloud_images_image" "myimage" { name = "Ubuntu 18.04 server 64bit"
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 6
HuaweiCloud Terraform Provider User Guide 3 Auto Scaling (AS)
most_recent = true }
resource "huaweicloud_as_conguration" "my_as_cong" { scaling_conguration_name = "my_as_cong"
instance_cong {
avor = data.huaweicloud_compute_avors.myavor.ids[0] image = data.huaweicloud_images_image.myimage.id key_name = var.my_keypair disk { size = 40 volume_type = "SSD" disk_type = "SYS" } } }
Step 2 Create an AS group.
Add the following information to the main.tf
data "huaweicloud_vpc" "vpc_1" { name = var.vpc_name } data "huaweicloud_vpc_subnet" "subnet_1" { name = var.subnet_name vpc_id = data.huaweicloud_vpc.vpc_1.id } data "huaweicloud_networking_secgroup" "secgroup_1" { name = var.secgroup_name }
resource "huaweicloud_as_group" "my_as_group" { scaling_group_name = "my_as_group"
scaling_conguration_id = huaweicloud_as_conguration.my_as_cong.id desire_instance_number = 2 min_instance_number = 0 max_instance_number = 10 vpc_id = data.huaweicloud_vpc.vpc_1.id delete_publicip = true delete_instances = "yes" networks { id = data.huaweicloud_vpc_subnet.subnet_1.id } security_groups { id = data.huaweicloud_networking_secgroup.secgroup_1.id } tags = { owner = "AutoScaling" } }
le:
Step 3 Add a scale-out policy.
In this example, add a metric-based policy. The following content that you will add to the main.tf
le indicates that when the average CPU usage is greater than or
equal to 80%, an ECS is automatically added.
resource "huaweicloud_ces_alarmrule" "scaling_up_rule" { alarm_name = "scaling_up_rule" metric { namespace = "SYS.AS" metric_name = "cpu_util" dimensions { name = "AutoScalingGroup" value = huaweicloud_as_group.my_as_group.id }
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 7
HuaweiCloud Terraform Provider User Guide 3 Auto Scaling (AS)
} condition { period = 300 lter = "average" comparison_operator = ">=" value = 80 unit = "%" count = 1 } alarm_actions { type = "autoscaling"
notication_list = [] } } resource "huaweicloud_as_policy" "scaling_up_policy" { scaling_policy_name = "scaling_up_policy" scaling_policy_type = "ALARM" scaling_group_id = huaweicloud_as_group.my_as_group.id alarm_id = huaweicloud_ces_alarmrule.scaling_up_rule.id cool_down_time = 300 scaling_policy_action { operation = "ADD" instance_number = 1 } }
Step 4 Add a scale-in policy.
In this example, add a metric-based policy. The following content that you will add to the main.tf
le indicates that when the average CPU usage is equal to or lower
than 20%, an ECS is automatically reduced.
resource "huaweicloud_ces_alarmrule" "scaling_down_rule" { alarm_name = "scaling_down_rule" metric { namespace = "SYS.AS" metric_name = "cpu_util" dimensions { name = "AutoScalingGroup" value = huaweicloud_as_group.my_as_group.id } } condition { period = 300
lter = "average" comparison_operator = "<=" value = 20 unit = "%" count = 1 } alarm_actions { type = "autoscaling"
notication_list = [] } } resource "huaweicloud_as_policy" "scaling_down_policy" { scaling_policy_name = "scaling_down_policy" scaling_policy_type = "ALARM" scaling_group_id = huaweicloud_as_group.my_as_group.id alarm_id = huaweicloud_ces_alarmrule.scaling_down_rule.id cool_down_time = 300 scaling_policy_action { operation = "REMOVE" instance_number = 1 } }
Step 5
Congure variables.
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 8
HuaweiCloud Terraform Provider User Guide 3 Auto Scaling (AS)
Create the variables.tf le, enter the following information, and save the le. You can change the variable values based on your needs.
variable "my_keypair" { default = "default" } variable "vpc_name" { default = "vpc-default" } variable "subnet_name" { default = "subnet-default" } variable "secgroup_name" { default = "default" }
Step 6 Create resources.
1. Run terraform init to initialize the environment.
2. Run terraform plan to view resources.
3. After you
conrm that the resource information is correct, run terraform
apply to start resource creation.
4. Run terraform show to view the created resources.
Sample Code
----End
https://github.com/huaweicloud/terraform-provider-huaweicloud/tree/ master/examples/auto-scaling/alarm_policy
Issue 01 (2021-03-11) Copyright © Huawei Technologies Co., Ltd. 9
Loading...
+ 26 hidden pages