Help Center/ Terraform/ User Guide/ Relational Database Service (RDS)/ Binding an EIP to an RDS DB Instance
Updated on 2023-12-22 GMT+08:00

Binding an EIP to an RDS DB Instance

Application Scenario

After an RDS DB instance is created, you can bind an EIP to it so that you can access the DB instance through the public network. This section describes how to use the Terraform scripts to bind or unbind an EIP from an RDS DB instance.

An EIP cannot be bound to or unbound from a DB instance that is being created, modified, restored, frozen, or rebooted.

Procedure

  1. For details about how to create a MySQL database instance, see Creating an RDS MySQL DB Instance.
  2. Add a security group rule to allow the specified network to access the port of the RDS DB instance.

    resource "huaweicloud_networking_secgroup_rule" "allow_rds" {
      direction         = "ingress"
      ethertype         = "IPv4"
      protocol          = "tcp"
      port_range_min    = 3306
      port_range_max    = 3306
      remote_ip_prefix  = var.allow_cidr
      security_group_id = huaweicloud_networking_secgroup.mysecgroup.id
    }

  3. Create an EIP and bind it to the private IP address of the RDS DB instance.

    # Creating an EIP
    resource "huaweicloud_vpc_eip" "myeip" {
      publicip {
        type = "5_bgp"
      }
      bandwidth {
        name        = "test"
        size        = 5
        share_type  = "PER"
        charge_mode = "traffic"
      }
    }
    # Querying the private network port of the RDS DB instance
    data "huaweicloud_networking_port" "rds_port" {
      network_id = huaweicloud_vpc_subnet.mysubnet.id
      fixed_ip   = huaweicloud_rds_instance.myinstance.private_ips[0]
    }
    # Binding an EIP
    resource "huaweicloud_vpc_eip_associate" "associated" {
      public_ip = huaweicloud_vpc_eip.myeip.address
      port_id   = data.huaweicloud_networking_port.rds_port.id
    }
    1. Run terraform plan to view resources.
    2. After you confirm that the resource information is correct, run terraform apply to start resource creation.
    3. Run terraform show to view binding information about the created EIP.

Table 1 Parameter description

Resource Name

Parameter

  

Description

huaweicloud_vpc_eip

publicip

type

(Mandatory) IP address type. Currently, only 5_bgp is supported.

bandwidth

name

(Optional) Bandwidth configuration name.

size

(Optional) IP bandwidth. The value ranges from 1 to 300 Mbit/s.

share_type

(Mandatory) Add the IP address to a shared bandwidth or an exclusive bandwidth.

huaweicloud_networking_port

fixed_ip

(Mandatory) Private IP address of the RDS DB instance.

network_id

(Mandatory) Network ID of the subnet to which the RDS instance belongs.

huaweicloud_vpc_eip_associate

public_ip

(Mandatory) EIP.

port_id

(Mandatory) ID of the port corresponding to the RDS DB instance.