Skip to content

Commit 1e1c105

Browse files
committed
chore(examples/cbh): adjust script and readme of the best practices
1 parent 11a6800 commit 1e1c105

File tree

12 files changed

+453
-210
lines changed

12 files changed

+453
-210
lines changed

examples/cbh/basic-instance/README.md

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,90 @@
11
# Create a CBH basic instance
22

3-
This example creates a CBH basic instance. The CBH dedicated instance requires a VPC,
4-
Subnet and security group. In this example, we all create them in the simplest
5-
way. You can replace them with resources already created in Huawei Cloud.
3+
This example provides best practice code for using Terraform to create a CBH basic instance in HuaweiCloud.
64

7-
To run, configure your HuaweiCloud provider as described in the
8-
[document](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs).
5+
## Prerequisites
96

10-
## The CBH instance configuration
7+
* A Huawei Cloud account
8+
* Terraform installed
9+
* Huawei Cloud access key and secret key (AK/SK)
1110

12-
| Attributes | Value |
13-
|---------------|-----------------|
14-
| name | Cbh_demo |
15-
| flavor_id | cbh.basic.50 |
16-
| password | Cbh@Huawei123 |
17-
| charging_mode | prePaid |
18-
| period_unit | mouth |
19-
| period | 1 |
11+
## Required Variables
12+
13+
### Authentication Variables
14+
15+
* `region_name` - The region where the CBH instance is located
16+
* `access_key` - The access key of the IAM user
17+
* `secret_key` - The secret key of the IAM user
18+
19+
### Resource Variables
20+
21+
#### Required Variables
22+
23+
* `vpc_name` - The name of the VPC
24+
* `subnet_name` - The name of the subnet
25+
* `security_group_name` - The name of the security group
26+
* `instance_name` - The name of the CBH instance
27+
* `instance_flavor_id` - The flavor ID of the CBH instance
28+
* `instance_password` - The login password of the CBH instance
29+
30+
#### Optional Variables
31+
32+
* `availability_zone` - The availability zone of the CBH instance (default: "")
33+
* `vpc_cidr` - The CIDR block of the VPC (default: "192.168.0.0/16")
34+
* `subnet_cidr` - The CIDR block of the subnet (default: "")
35+
* `subnet_gateway_ip` - The gateway IP of the subnet (default: "")
36+
* `charging_mode` - The charging mode of the CBH instance (default: "prePaid")
37+
* `period_unit` - The charging period unit of the CBH instance (default: "month")
38+
* `period` - The charging period of the CBH instance (default: 1)
39+
* `auto_renew` - Whether to enable auto-renew for the CBH instance (default: "false")
2040

2141
## Usage
2242

23-
```shell
24-
terraform init
25-
terraform plan
26-
terraform apply
27-
terraform destroy
28-
```
43+
* Copy this example script to your `main.tf`.
44+
* Create a `terraform.tfvars` file and fill in the required variables:
45+
46+
```hcl
47+
vpc_name = "your_vpc_name"
48+
subnet_name = "your_subnet_name"
49+
security_group_name = "your_security_group_name"
50+
instance_name = "your_cbh_instance_name"
51+
instance_flavor_id = "your_cbh_instance_flavor_id"
52+
instance_password = "your_cbh_instance_password"
53+
```
54+
55+
* Initialize Terraform:
56+
57+
```bash
58+
terraform init
59+
```
60+
61+
* Review the Terraform plan:
62+
63+
```bash
64+
terraform plan
65+
```
66+
67+
* Apply the configuration:
68+
69+
```bash
70+
terraform apply
71+
```
72+
73+
* To clean up the resources:
74+
75+
```bash
76+
terraform destroy
77+
```
78+
79+
## Notes
2980

30-
It takes about 15 minutes to create a CBH basic instance.
81+
* Make sure to keep your credentials secure and never commit them to version control
82+
* It takes about 15 minutes to create a CBH basic instance
83+
* All resources will be created in the specified region
3184

3285
## Requirements
3386

3487
| Name | Version |
3588
|-------------|-----------|
3689
| terraform | >= 0.12.0 |
37-
| huaweicloud | >= 1.40.0 |
90+
| huaweicloud | >= 1.64.3 |

examples/cbh/basic-instance/main.tf

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
1-
# This example demonstrates how to create a CBH basic instance in Huawei Cloud using Terraform.
2-
# It includes the creation of a VPC, subnet, security group, and the CBH instance itself.
3-
# The example also includes the necessary variables and outputs for the created resources.
4-
5-
#List the availability zones
6-
data "huaweicloud_availability_zones" "default" {}
1+
data "huaweicloud_cbh_availability_zones" "test" {
2+
count = var.availability_zone == "" ? 1 : 0
3+
}
74

8-
#create a VPC
9-
resource "huaweicloud_vpc" "default" {
10-
name = "cbh_vpc"
11-
cidr = "192.168.0.0/16"
5+
data "huaweicloud_cbh_flavors" "test" {
6+
count = var.instance_flavor_id == "" ? 1 : 0
7+
type = var.instance_flavor_type
128
}
139

14-
#Create a subnet
15-
resource "huaweicloud_vpc_subnet" "default" {
16-
name = "cbh_subnet"
17-
cidr = "192.168.0.0/20"
18-
gateway_ip = "192.168.0.1"
19-
vpc_id = huaweicloud_vpc.default.id
10+
resource "huaweicloud_vpc" "test" {
11+
name = var.vpc_name
12+
cidr = var.vpc_cidr
2013
}
2114

22-
#create a security group
23-
resource "huaweicloud_networking_secgroup" "default" {
24-
name = "cbh_security_group"
15+
resource "huaweicloud_vpc_subnet" "test" {
16+
vpc_id = huaweicloud_vpc.test.id
17+
name = var.subnet_name
18+
cidr = var.subnet_cidr == "" ? cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0) : var.subnet_cidr
19+
gateway_ip = var.subnet_gateway_ip == "" ? cidrhost(cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0), 1) : var.subnet_gateway_ip
2520
}
2621

27-
#create a CBH basic instance
28-
resource "huaweicloud_cbh_instance" "cbh_demo" {
29-
name = var.name
30-
flavor_id = var.flavor_id
31-
vpc_id = huaweicloud_vpc.default.id
32-
subnet_id = huaweicloud_vpc_subnet.default.id
33-
security_group_id = huaweicloud_networking_secgroup.default.id
34-
availability_zone = data.huaweicloud_availability_zones.default.names[0]
35-
password = var.password
36-
charging_mode = var.charging_mode
37-
period_unit = var.period_unit
38-
period = var.period
22+
resource "huaweicloud_networking_secgroup" "test" {
23+
name = var.security_group_name
24+
delete_default_rules = true
3925
}
4026

41-
#output the CBH instance ID
42-
output "cbh_instance_id" {
43-
value = huaweicloud_cbh_instance.cbh_demo.id
44-
description = "The ID of the CBH instance."
27+
resource "huaweicloud_cbh_instance" "test" {
28+
name = var.instance_name
29+
flavor_id = var.instance_flavor_id == "" ? try(data.huaweicloud_cbh_flavors.test[0].flavors[0].id, null) : var.instance_flavor_id
30+
vpc_id = huaweicloud_vpc.test.id
31+
subnet_id = huaweicloud_vpc_subnet.test.id
32+
security_group_id = huaweicloud_networking_secgroup.test.id
33+
availability_zone = var.availability_zone == "" ? try(data.huaweicloud_cbh_availability_zones.test[0].availability_zones[0].name, null) : var.availability_zone
34+
password = var.instance_password
35+
charging_mode = var.charging_mode
36+
period_unit = var.period_unit
37+
period = var.period
38+
auto_renew = var.auto_renew
4539
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_providers {
3+
huaweicloud = {
4+
source = "huaweicloud/huaweicloud"
5+
version = ">=1.64.3"
6+
}
7+
}
8+
}
9+
10+
provider "huaweicloud" {
11+
region = var.region_name
12+
access_key = var.access_key
13+
secret_key = var.secret_key
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vpc_name = "tf_test_cbh_instance_vpc"
2+
subnet_name = "tf_test_cbh_instance_subnet"
3+
security_group_name = "tf_test_cbh_instance_security_group"
4+
instance_name = "tf_test_cbh_instance"
5+
instance_password = "YourCBHInstancePassword!"
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Variable definitions for authentication
2+
variable "region_name" {
3+
description = "The region where the CBH instance is located"
4+
type = string
5+
}
6+
7+
variable "access_key" {
8+
description = "The access key of the IAM user"
9+
type = string
10+
}
11+
12+
variable "secret_key" {
13+
description = "The secret key of the IAM user"
14+
type = string
15+
}
16+
17+
# Variable definitions for resources/data sources
18+
variable "availability_zone" {
19+
description = "The availability zone to which the CBH instance belongs"
20+
type = string
21+
default = ""
22+
}
23+
24+
variable "instance_flavor_id" {
25+
description = "The flavor ID of the CBH instance"
26+
type = string
27+
default = ""
28+
}
29+
30+
variable "instance_flavor_type" {
31+
description = "The flavor type of the CBH instance"
32+
type = string
33+
default = "basic"
34+
}
35+
36+
variable "vpc_name" {
37+
description = "The name of the VPC"
38+
type = string
39+
}
40+
41+
variable "vpc_cidr" {
42+
description = "The CIDR block of the VPC"
43+
type = string
44+
default = "192.168.0.0/16"
45+
}
46+
47+
variable "subnet_name" {
48+
description = "The name of the subnet"
49+
type = string
50+
}
51+
52+
variable "subnet_cidr" {
53+
description = "The CIDR block of the subnet"
54+
type = string
55+
default = ""
56+
}
57+
58+
variable "subnet_gateway_ip" {
59+
description = "The gateway IP of the subnet"
60+
type = string
61+
default = ""
62+
}
63+
64+
variable "security_group_name" {
65+
description = "The name of the security group"
66+
type = string
67+
}
68+
69+
variable "instance_name" {
70+
description = "The name of the CBH instance"
71+
type = string
72+
}
73+
74+
variable "instance_password" {
75+
description = "The login password of the CBH instance"
76+
type = string
77+
sensitive = true
78+
}
79+
80+
variable "charging_mode" {
81+
description = "The charging mode of the CBH instance"
82+
type = string
83+
default = "prePaid"
84+
}
85+
86+
variable "period_unit" {
87+
description = "The charging period unit of the CBH instance"
88+
type = string
89+
default = "month"
90+
}
91+
92+
variable "period" {
93+
description = "The charging period of the CBH instance"
94+
type = number
95+
default = 1
96+
}
97+
98+
variable "auto_renew" {
99+
description = "Whether to enable auto-renew for the CBH instance"
100+
type = string
101+
default = "false"
102+
}

examples/cbh/basic-instance/varible.tf

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)