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 fi 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 ff 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 |
nfi |
n 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 |
||
6.2 |
nfi |
n Static Website Hosting............................................................................................................................... |
20 |
|
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 |
1HUAWEI CLOUD Provider Authentication
HUAWEI CLOUD Provider uses AK/SK for authentication. You can provide credentials as either static credentials or environment variables.
Static Credentials
nfi 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 nfi n fi in plaintext, which risks secret leakage. It is recommended that you provide credentials as environment variables.
Environment Variables
nfi |
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 |
Environment |
Description |
|
|
tory |
Variable |
|
|
|
|
|
|
|
region |
Yes |
HW_REGION_NAME |
Region where the HUAWEI |
|
|
|
|
CLOUD service is located. For |
|
|
|
|
details, see Regions and |
|
|
|
|
Endpoints. |
|
|
|
|
If you want to create cloud |
|
|
|
|
services in ff n regions, |
|
|
|
|
nfi |
parameter alias or |
|
|
|
region for the resource |
|
|
|
|
corresponding to the cloud |
|
|
|
|
service. |
|
|
|
|
|
|
access_key |
Yes |
HW_ACCESS_KEY |
Access key ID of a user. For |
|
|
|
|
details on how to obtain an |
|
|
|
|
access key ID, see Access Keys. |
|
|
|
|
|
|
secret_key |
Yes |
HW_SECRET_KEY |
Secret access key of a user. For |
|
|
|
|
details on how to obtain a secret |
|
|
|
|
access key, see Access Keys. |
|
|
|
|
|
|
domain_na |
No |
HW_DOMAIN_NAM |
HUAWEI CLOUD account name. |
|
me |
|
E |
For details on how to obtain an |
|
|
|
|
account name, see API |
|
|
|
|
Credentials. |
|
|
|
|
|
|
project_nam |
No |
HW_PROJECT_NAM |
HUAWEI CLOUD project name. |
|
e |
|
E |
For details on how to obtain a |
|
|
|
|
project name, see API |
|
|
|
|
Credentials. |
|
|
|
|
|
|
enterprise_p |
No |
HW_ENTERPRISE_P |
Enterprise project ID. For more |
|
roject_id |
|
ROJECT_ID |
information about enterprise |
|
|
|
|
projects and how to obtain |
|
|
|
|
enterprise project IDs, see |
|
|
|
|
Enterprise Management User |
|
|
|
|
Guide. |
|
|
|
|
|
|
max_retries |
No |
HW_MAX_RETRIES |
Maximum number of retries |
|
|
|
|
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) |
2Elastic Cloud Server (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 ff n scenario requirements. When creating an ECS, select fi n image type, and disk type and nfi 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 |
fi |
n image, and network |
||||
parameters. |
|
|
|
|
|
|
Create the main.tf fi |
enter the following information, and save the fi |
|||||
data "huaweicloud_availability_zones" "myaz" {} |
|
|
||||
data |
w |
m |
fl v |
myfl v { |
|
|
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" 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 fi
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 |
||
fl v |
= data. w |
m |
fl v .myfl v .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 nfi m 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
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 fi
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
}
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 nfi m 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
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 fi
resource "huaweicloud_vpc_eip" "myeip" { publicip {
type = "5_bgp"
} |
|
|
|
bandwidth { |
|
|
|
name |
|
= "mybandwidth" |
|
size |
= 8 |
|
|
share_type |
= "PER" |
ffi |
|
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. |
|
Step 3 |
After you |
nfi m 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) |
3Auto Scaling (AS)
Application Scenarios
AS automatically adjusts service resources to keep up with your demand based on nfi AS policies. With automatic resource adjustment, you can enjoy
reduced costs, improved availability, and high fault tolerance. AS applies to the following scenarios:
● |
vy |
ffi forums: Service load changes of a |
vy |
ffi forum website |
are |
ffi |
to predict. AS dynamically adjusts the number of cloud servers |
based on monitored ECS metrics, such as vCPU Usage and Memory Usage.
●E-commerce: Large-scale e-commerce promotions can attract visits that may break your website. AS automatically adds ECSs and increases bandwidth to ensure that promotions will go smoothly.
●Live streaming: A live streaming website broadcasts popular programs from 14:00 to 16:00 every day. AS automatically adds ECSs and increases bandwidth during this period to ensure smooth viewer experience.
Related Resources
● w fi
●huaweicloud_as_group
●huaweicloud_as_policy
●huaweicloud_ces_alarmrule
Procedure
Step 1 Create an AS |
nfi |
|
n |
||
Create the main.tf fi |
enter the following information, and save the fi |
||||
|
data "huaweicloud_availability_zones" "myaz" {} |
||||
|
data |
w |
m |
fl v |
myfl v { |
|
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 |
w |
|
nfi |
n |
my |
nfi |
{ |
|
|
|
|
n |
nfi |
n n m = |
my |
nfi |
|
|
|
|
|
n |
n |
nfi { |
|
|
|
|
|
|
|
|
fl |
v |
= |
w |
|
m |
fl v |
myfl v |
|
|
|
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 fi |
|
|||||||||
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" |
|
|
|
|||||
|
|
n |
nfi |
n |
= |
w |
|
nfi |
n.my |
nfi .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"
}
}
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 fi 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 |
fi |
|
= "average" |
comparison_operator = ">=" |
||
value |
|
= 80 |
unit |
|
= "%" |
count |
|
= 1 |
} |
|
|
alarm_actions { |
||
type |
|
= "autoscaling" |
n fi |
n |
= [] |
} |
|
|
} |
|
|
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 fi 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 |
|
fi |
|
= "average" |
|
|
comparison_operator = "<=" |
|||
|
value |
|
|
= 20 |
|
unit |
|
= "%" |
|
|
count |
|
|
= 1 |
|
} |
|
|
|
|
alarm_actions { |
|
||
|
type |
|
= "autoscaling" |
|
|
n fi |
n |
|
= [] |
|
} |
|
|
|
|
} |
|
|
|
|
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 |
nfi |
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 fi |
enter the following information, and save the fi 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 nfi m that the resource information is correct, run terraform apply to start resource creation.
4. Run terraform show to view the created resources.
----End
Sample Code
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 |