Deploy Endpoint
Application Scenario
VPC Endpoint (VPCEP) is a VPC internal resource mutual access service provided by Huawei Cloud, supporting the creation of endpoints and endpoint services within VPCs to achieve private network access to VPC resources. Endpoint is a core function of VPCEP service, used to create endpoints within VPCs, connecting to endpoint services, achieving cross-VPC private network access. Through endpoints, endpoint services published in other VPCs can be accessed, achieving secure private network communication, avoiding public network access, improving access security and stability. This best practice introduces how to use Terraform to automatically deploy endpoints, including availability zone query, ECS flavor query, image query, VPC, subnet, security group, ECS instance, endpoint service, and endpoint creation.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
Note: Endpoint depends on endpoint service, endpoint service depends on ECS instance, and ECS instance depends on VPC, subnet, security group, availability zone, flavor, and image resources. Endpoint connects to endpoint service through association, achieving cross-VPC private network access.
Operation Steps
1. Script Preparation
Prepare the TF file (such as main.tf) for writing the current best practice script in the specified workspace, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. For configuration details, refer to the introduction in Preparation Before Deploying Huawei Cloud Resources.
2. Query Availability Zones
Add the following script to the TF file (such as main.tf) to query availability zones:
3. Query ECS Flavors
Add the following script to the TF file (such as main.tf) to query ECS flavors:
Parameter Description:
availability_zone: Availability zone name, assigned by referencing the availability zone query data source results
performance_type: Flavor performance type, assigned by referencing the input variable
instance_flavor_performance_typecpu_core_count: CPU core count, assigned by referencing the input variable
instance_flavor_cpu_core_countmemory_size: Memory size, assigned by referencing the input variable
instance_flavor_memory_size
4. Query Image
Add the following script to the TF file (such as main.tf) to query image:
Parameter Description:
name: Image name, assigned by referencing the input variable
instance_image_namemost_recent: Whether to use the most recent image, assigned by referencing the input variable
instance_image_most_recent
5. Create VPC and Subnet
Add the following script to the TF file (such as main.tf) to create VPC and subnet:
Parameter Description:
name: VPC name, assigned by referencing the input variable
vpc_namecidr: VPC CIDR block, assigned by referencing the input variable
vpc_cidrvpc_id: VPC ID to which the subnet belongs, assigned by referencing the VPC resource ID
cidr: Subnet CIDR block, automatically calculated if the input variable is empty, otherwise uses the input variable value
gateway_ip: Subnet gateway IP address, automatically calculated if the input variable is empty, otherwise uses the input variable value
6. Create Security Group
Add the following script to the TF file (such as main.tf) to create security group:
Parameter Description:
name: Security group name, assigned by referencing the input variable
security_group_name
7. Create ECS Instance
Add the following script to the TF file (such as main.tf) to create ECS instance:
Parameter Description:
name: ECS instance name, assigned by referencing the input variable
instance_nameimage_id: Image ID, assigned by referencing the image query data source ID
flavor_id: Flavor ID, uses the flavor query data source result if the input variable is empty, otherwise uses the input variable value
security_group_ids: Security group ID list, assigned by referencing the security group resource ID
availability_zone: Availability zone name, assigned by referencing the availability zone query data source results
network: Network configuration, assigned by referencing the subnet resource ID
8. Create Endpoint Service
Add the following script to the TF file (such as main.tf) to create endpoint service:
Parameter Description:
name: Endpoint service name, assigned by referencing the input variable
endpoint_service_nameserver_type: Server type, assigned by referencing the input variable
endpoint_service_typevpc_id: VPC ID to which the endpoint service belongs, assigned by referencing the VPC resource ID
port_id: Port ID, assigned by referencing the network port ID of the ECS instance
port_mapping: Port mapping list, creates port mappings through dynamic block
dynamic "port_mapping"based on input variableendpoint_service_port_mappingservice_port: Service port, assigned by referencing the
service_portin the input variableterminal_port: Terminal port, assigned by referencing the
terminal_portin the input variable
9. Create Endpoint
Add the following script to the TF file (such as main.tf) to create endpoint:
Parameter Description:
service_id: Endpoint service ID, assigned by referencing the endpoint service resource ID
vpc_id: VPC ID to which the endpoint belongs, assigned by referencing the VPC resource ID
network_id: Subnet ID to which the endpoint belongs, assigned by referencing the subnet resource ID
Note: Endpoint is used to connect to endpoint service, achieving cross-VPC private network access. After creating an endpoint, service resources provided by the endpoint service can be accessed through the endpoint.
10. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources and data sources use input variables to assign configuration content. These input parameters need to be manually entered during subsequent deployment. Terraform also provides a method to preset these configurations through tfvars files, which can avoid repeated input each time.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows Terraform to automatically import the variable values in thistfvarsfile when executing terraform commands. For other names, you need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs
When executing
terraform planorterraform apply, Terraform will automatically read the variable values in this file
In addition to using the terraform.tfvars file, you can also set variable values through the following methods:
Command-line parameters:
terraform apply -var="vpc_name=tf_test_vpc"Environment variables:
export TF_VAR_vpc_name=tf_test_vpcCustom-named variable files:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set through multiple methods, Terraform will use variable values according to the following priority: command-line parameters > variable files > environment variables > default values.
11. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create resources:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming that the resource plan is correct, run
terraform applyto start creating endpoint and related resourcesRun
terraform showto view the created endpoint
Reference Information
Last updated