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.

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:

  1. Save the above content as terraform.tfvars file in the working directory (this file name allows users to automatically import the content of this tfvars file when executing terraform commands; for other names, .auto needs to be added before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values as needed

  3. When executing terraform plan or terraform 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:

  1. Command line parameters: terraform apply -var="vpc_name=my-vpc" -var="instance_name=my-instance"

  2. Environment variables: export TF_VAR_vpc_name=my-vpc

  3. Custom 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:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming the resource plan is correct, run terraform apply to start creating MySQL single instances with EIP binding

  4. Run terraform show to view the created MySQL single instance with EIP binding details

Reference Information

Last updated