Deploy MySQL Single Instance with EIP
Application Scenario
Huawei Cloud Relational Database Service (RDS) MySQL single instance with EIP binding functionality provides MySQL database services with public network access capabilities, supporting access to database instances from the internet through Elastic IP (EIP). By configuring EIP binding, you can provide public network connectivity for RDS instances, meeting requirements for cross-region access, remote development, data synchronization, and other scenarios.
This best practice is particularly suitable for scenarios that require public network access to MySQL databases, implementing cross-region data synchronization, supporting remote development and testing, such as multi-region application deployment, remote work, third-party system integration, etc. This best practice will introduce how to use Terraform to automatically deploy RDS MySQL single instances with EIP binding, including VPC network, security group, RDS instance, EIP, and EIP binding creation, implementing a complete public network accessible MySQL database solution.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
Operation Steps
1. Script Preparation
Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, 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. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.
2. Prerequisite Resource Preparation
This best practice requires creating prerequisite resources such as VPC, subnet, security group, and RDS instance first. Please prepare according to the following steps in the Deploy MySQL Single Instance best practice:
Step 2: Create VPC resource
Step 3: Query availability zone information
Step 4: Create VPC subnet
Step 5: Query RDS flavor information
Step 6: Create security group
Step 7: Create security group rules
Step 8: Create random password
Step 9: Create RDS instance
After completing the above steps, continue with the subsequent steps of this best practice.
3. Create Elastic IP
Add the following script to the TF file to instruct Terraform to create an Elastic IP resource:
Parameter Description:
count: Conditional creation, creates this resource when associate_eip_address variable is an empty string
publicip: Public IP configuration block
type: EIP type, assigned by referencing the input variable eip_type, default value is "5_bgp"
bandwidth: Bandwidth configuration block
name: Bandwidth name, assigned by referencing the input variable bandwidth_name, default value is empty string
size: Bandwidth size, assigned by referencing the input variable bandwidth_size, default value is 5 (Mbps)
share_type: Bandwidth sharing type, assigned by referencing the input variable bandwidth_share_type, default value is "PER"
charge_mode: Bandwidth billing mode, assigned by referencing the input variable bandwidth_charge_mode, default value is "traffic"
4. Query Network Port Information
Add the following script to the TF file to instruct Terraform to query network port information:
Parameter Description:
network_id: Network ID, referencing the ID of the previously created VPC subnet resource
fixed_ip: Fixed IP address, referencing the fixed IP of the previously created RDS instance resource
depends_on: Explicit dependency relationship, ensures RDS instance is created before port query
5. Create EIP Association
Add the following script to the TF file to instruct Terraform to create an EIP association resource:
Parameter Description:
public_ip: Public IP address, prioritizes using associate_eip_address variable, uses newly created EIP address if empty
port_id: Port ID, referencing the ID of the queried network port resource
6. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. At the same time, Terraform provides a method to preset these configurations through .tfvars files, which can avoid repeated input during each execution.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as
terraform.tfvarsfile in the working directory (this file name allows users to automatically import the content of thistfvarsfile when executing terraform commands; for other names,.autoneeds to be added before tfvars, such asvariables.auto.tfvars)Modify parameter values as needed
When executing
terraform planorterraform apply, Terraform will automatically read the variable values from this file
In addition to using terraform.tfvars file, variable values can also be set in the following ways:
Command line parameters:
terraform apply -var="vpc_name=my-vpc" -var="instance_name=my-instance"Environment variables:
export TF_VAR_vpc_name=my-vpcCustom named variable files:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.
7. 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 the resource plan is correct, run
terraform applyto start creating MySQL single instances with EIP bindingRun
terraform showto view the created MySQL single instance with EIP binding details
Reference Information
Last updated