Help Center> Terraform> User Guide> Virtual Private Cloud (VPC)> Binding a Virtual IP Address
Updated on 2023-12-21 GMT+08:00

Binding a Virtual IP Address

Application Scenario

Virtual IP addresses are used for high availability (HA) as they make active/standby ECS switchover possible. If the active ECS becomes faulty and cannot provide services, the virtual IP address is dynamically re-assigned to the standby ECS so services can continue uninterrupted.

Related Resources

Procedure

  1. Configure the network.

    Create the main.tf file, enter the following information, and save the file:

    resource "huaweicloud_vpc" "vpc_1" {
      name = var.vpc_name
      cidr = var.vpc_cidr
    }
    
    resource "huaweicloud_vpc_subnet" "subnet_1" {
      vpc_id      = huaweicloud_vpc.vpc_1.id
      name        = var.subnet_name
      cidr        = var.subnet_cidr
      gateway_ip  = var.subnet_gateway
      primary_dns = var.primary_dns
    }

  2. Create two ECSs.

    Add the following information to the main.tf file:

    data "huaweicloud_availability_zones" "myaz" {}
    
    data "huaweicloud_compute_flavors" "myflavor" {
      availability_zone = data.huaweicloud_availability_zones.myaz.names[0]
      performance_type  = "normal"
      cpu_core_count    = 2
      memory_size       = 4
    }
    
    data "huaweicloud_images_image" "myimage" {
      name        = "Ubuntu 18.04 server 64bit"
      most_recent = true
    }
    
    data "huaweicloud_networking_secgroup" "mysecgroup" {
      name = "default"
    }
    
    resource "huaweicloud_compute_instance" "mycompute" {
      name               = "mycompute_${count.index}"
      image_id           = data.huaweicloud_images_image.myimage.id
      flavor_id          = data.huaweicloud_compute_flavors.myflavor.ids[0]
      availability_zone  = data.huaweicloud_availability_zones.myaz.names[0]
      security_group_ids = [data.huaweicloud_networking_secgroup.mysecgroup.id]
      network {
        uuid = huaweicloud_vpc_subnet.subnet_1.id
      }
      count = 2
    }

  3. Apply for a virtual IP address and bind it to the ECS ports.

    Add the following information to the main.tf file:

    resource "huaweicloud_networking_vip" "vip_1" {
      network_id = huaweicloud_vpc_subnet.subnet_1.id
    }
    
    # associate ports to the vip
    resource "huaweicloud_networking_vip_associate" "vip_associated" {
      vip_id   = huaweicloud_networking_vip.vip_1.id
      port_ids = [
        huaweicloud_compute_instance.mycompute[0].network.0.port,
        huaweicloud_compute_instance.mycompute[1].network.0.port
      ]
    }

  4. Configure variables.

    Create the variables.tf file, enter the following information, and save the file. You can change the variable values based on your needs.

    variable "vpc_name" {
      default = "vpc-basic"
    }
    variable "vpc_cidr" {
      default = "172.16.0.0/16"
    }
    variable "subnet_name" {
      default = "subent-basic"
    }
    variable "subnet_cidr" {
      default = "172.16.10.0/24"
    }
    variable "subnet_gateway" {
      default = "172.16.10.1"
    }
    variable "primary_dns" {
      default = "100.125.1.250"
    }

  5. Create resources.

    1. Run terraform init to initialize the environment.
    2. Run terraform plan to view resources.
    3. After you confirm that the resource information is correct, run terraform apply to start resource creation.
    4. Run terraform show to view the created resources.