Skip to content

chore(examples/cbh): adjust script and readme of the best practices #7311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 76 additions & 22 deletions examples/cbh/basic-instance/README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,91 @@
# Create a CBH basic instance

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

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

## The CBH instance configuration
* A Huawei Cloud account
* Terraform installed
* Huawei Cloud access key and secret key (AK/SK)

| Attributes | Value |
|---------------|-----------------|
| name | Cbh_demo |
| flavor_id | cbh.basic.50 |
| password | Cbh@Huawei123 |
| charging_mode | prePaid |
| period_unit | mouth |
| period | 1 |
## Required Variables

### Authentication Variables

* `region_name` - The region where the CBH instance is located
* `access_key` - The access key of the IAM user
* `secret_key` - The secret key of the IAM user

### Resource Variables

#### Required Variables

* `vpc_name` - The name of the VPC
* `subnet_name` - The name of the subnet
* `security_group_name` - The name of the security group
* `instance_name` - The name of the CBH instance
* `instance_flavor_id` - The flavor ID of the CBH instance
* `instance_flavor_type` - The flavor type of the CBH instance
* `instance_password` - The login password of the CBH instance

#### Optional Variables

* `availability_zone` - The availability zone of the CBH instance (default: "")
* `vpc_cidr` - The CIDR block of the VPC (default: "192.168.0.0/16")
* `subnet_cidr` - The CIDR block of the subnet (default: "")
* `subnet_gateway_ip` - The gateway IP of the subnet (default: "")
* `charging_mode` - The charging mode of the CBH instance (default: "prePaid")
* `period_unit` - The charging period unit of the CBH instance (default: "month")
* `period` - The charging period of the CBH instance (default: 1)
* `auto_renew` - Whether to enable auto-renew for the CBH instance (default: "false")

## Usage

```shell
terraform init
terraform plan
terraform apply
terraform destroy
```
* Copy this example script to your `main.tf`.
* Create a `terraform.tfvars` file and fill in the required variables:

```hcl
vpc_name = "your_vpc_name"
subnet_name = "your_subnet_name"
security_group_name = "your_security_group_name"
instance_name = "your_cbh_instance_name"
instance_flavor_id = "your_cbh_instance_flavor_id"
instance_password = "your_cbh_instance_password"
```

* Initialize Terraform:

```bash
terraform init
```

* Review the Terraform plan:

```bash
terraform plan
```

* Apply the configuration:

```bash
terraform apply
```

* To clean up the resources:

```bash
terraform destroy
```

## Notes

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

## Requirements

| Name | Version |
|-------------|-----------|
| terraform | >= 0.12.0 |
| huaweicloud | >= 1.40.0 |
| huaweicloud | >= 1.64.3 |
72 changes: 37 additions & 35 deletions examples/cbh/basic-instance/main.tf
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
# This example demonstrates how to create a CBH basic instance in Huawei Cloud using Terraform.
# It includes the creation of a VPC, subnet, security group, and the CBH instance itself.
# The example also includes the necessary variables and outputs for the created resources.

#List the availability zones
data "huaweicloud_availability_zones" "default" {}
data "huaweicloud_cbh_availability_zones" "test" {
count = var.availability_zone == "" ? 1 : 0
}

#create a VPC
resource "huaweicloud_vpc" "default" {
name = "cbh_vpc"
cidr = "192.168.0.0/16"
data "huaweicloud_cbh_flavors" "test" {
count = var.instance_flavor_id == "" ? 1 : 0
type = var.instance_flavor_type
}

#Create a subnet
resource "huaweicloud_vpc_subnet" "default" {
name = "cbh_subnet"
cidr = "192.168.0.0/20"
gateway_ip = "192.168.0.1"
vpc_id = huaweicloud_vpc.default.id
resource "huaweicloud_vpc" "test" {
name = var.vpc_name
cidr = var.vpc_cidr
}

#create a security group
resource "huaweicloud_networking_secgroup" "default" {
name = "cbh_security_group"
resource "huaweicloud_vpc_subnet" "test" {
vpc_id = huaweicloud_vpc.test.id
name = var.subnet_name
cidr = var.subnet_cidr == "" ? cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0) : var.subnet_cidr
gateway_ip = var.subnet_gateway_ip == "" ? cidrhost(cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0), 1) : var.subnet_gateway_ip
}

#create a CBH basic instance
resource "huaweicloud_cbh_instance" "cbh_demo" {
name = var.name
flavor_id = var.flavor_id
vpc_id = huaweicloud_vpc.default.id
subnet_id = huaweicloud_vpc_subnet.default.id
security_group_id = huaweicloud_networking_secgroup.default.id
availability_zone = data.huaweicloud_availability_zones.default.names[0]
password = var.password
charging_mode = var.charging_mode
period_unit = var.period_unit
period = var.period
resource "huaweicloud_networking_secgroup" "test" {
name = var.security_group_name
delete_default_rules = true
}

#output the CBH instance ID
output "cbh_instance_id" {
value = huaweicloud_cbh_instance.cbh_demo.id
description = "The ID of the CBH instance."
resource "huaweicloud_cbh_instance" "test" {
name = var.instance_name
flavor_id = var.instance_flavor_id == "" ? try(data.huaweicloud_cbh_flavors.test[0].flavors[0].id, null) : var.instance_flavor_id
vpc_id = huaweicloud_vpc.test.id
subnet_id = huaweicloud_vpc_subnet.test.id
security_group_id = huaweicloud_networking_secgroup.test.id
availability_zone = var.availability_zone == "" ? try(data.huaweicloud_cbh_availability_zones.test[0].availability_zones[0].name, null) : var.availability_zone
password = var.instance_password
charging_mode = var.charging_mode
period_unit = var.period_unit
period = var.period
auto_renew = var.auto_renew

# If you want to change some of the following parameters, you need to remove the corresponding fields from "lifecycle.ignore_changes".
lifecycle {
ignore_changes = [
flavor_id,
availability_zone,
]
}
}
14 changes: 14 additions & 0 deletions examples/cbh/basic-instance/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_providers {
huaweicloud = {
source = "huaweicloud/huaweicloud"
version = ">=1.64.3"
}
}
}

provider "huaweicloud" {
region = var.region_name
access_key = var.access_key
secret_key = var.secret_key
}
5 changes: 5 additions & 0 deletions examples/cbh/basic-instance/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vpc_name = "tf_test_cbh_instance_vpc"
subnet_name = "tf_test_cbh_instance_subnet"
security_group_name = "tf_test_cbh_instance_security_group"
instance_name = "tf_test_cbh_instance"
instance_password = "YourCBHInstancePassword!"
102 changes: 102 additions & 0 deletions examples/cbh/basic-instance/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Variable definitions for authentication
variable "region_name" {
description = "The region where the CBH instance is located"
type = string
}

variable "access_key" {
description = "The access key of the IAM user"
type = string
}

variable "secret_key" {
description = "The secret key of the IAM user"
type = string
}

# Variable definitions for resources/data sources
variable "availability_zone" {
description = "The availability zone to which the CBH instance belongs"
type = string
default = ""
}

variable "instance_flavor_id" {
description = "The flavor ID of the CBH instance"
type = string
default = ""
}

variable "instance_flavor_type" {
description = "The flavor type of the CBH instance"
type = string
default = "basic"
}

variable "vpc_name" {
description = "The name of the VPC"
type = string
}

variable "vpc_cidr" {
description = "The CIDR block of the VPC"
type = string
default = "192.168.0.0/16"
}

variable "subnet_name" {
description = "The name of the subnet"
type = string
}

variable "subnet_cidr" {
description = "The CIDR block of the subnet"
type = string
default = ""
}

variable "subnet_gateway_ip" {
description = "The gateway IP of the subnet"
type = string
default = ""
}

variable "security_group_name" {
description = "The name of the security group"
type = string
}

variable "instance_name" {
description = "The name of the CBH instance"
type = string
}

variable "instance_password" {
description = "The login password of the CBH instance"
type = string
sensitive = true
}

variable "charging_mode" {
description = "The charging mode of the CBH instance"
type = string
default = "prePaid"
}

variable "period_unit" {
description = "The charging period unit of the CBH instance"
type = string
default = "month"
}

variable "period" {
description = "The charging period of the CBH instance"
type = number
default = 1
}

variable "auto_renew" {
description = "Whether to enable auto-renew for the CBH instance"
type = string
default = "false"
}
36 changes: 0 additions & 36 deletions examples/cbh/basic-instance/varible.tf

This file was deleted.

Loading
Loading