Help Center> API Gateway> Best Practices> Exposing Backend Services Across VPCs

Exposing Backend Services Across VPCs

If the VPC of my backend server is different from that of my gateway, how do I configure cross-VPC interconnection? This section uses Elastic Load Balance (ELB) as an example to describe how to expose services in a private network load balancer using API Gateway.

Figure 1 Exposing backend services across VPCs

Procedure

  1. Creating a VPC

    Create two VPCs, one for API Gateway and the other for backend services.

  2. Buying a Dedicated API Gateway

    Buy a dedicated gateway in VPC 1.

  3. Buying a Load Balancer

    Buy a load balancer in VPC 2.

  4. Creating a VPC Peering Connection

    Create a VPC peering connection to connect VPC 1 and VPC 2.

  5. Configuring a Route

    Configure a route for the dedicated API gateway by setting the IP address to the IPv4 CIDR block of VPC 2.

  6. Creating an API

    Create an API and set the backend service address to the IP address of the load balancer.

  7. Buying an ECS

    Buy an ECS, start the backend service, and mount the ECS to the load balancer.

  8. Debugging the API

    Verify that the connection to the private network load balancer is successful.

Creating a VPC

  1. Log in to the network console.
  2. On the Virtual Private Cloud page, click Create VPC, and configure the parameters by referring to Table 1 and Table 2.

    Table 1 Configuration information

    Parameter

    Description

    Region

    Select a region. For this example, select CN North-Beijing4.

    Name

    Enter VPC1. This VPC will be used to run an API gateway.

    Enterprise Project

    Select default.

    AZ

    The AZ to which the subnet belongs. Select AZ1.

    Table 2 VPC planning

    VPC 1

    System VPC of API Gateway

    VPC 2

    10.X

    172.31.0.0/16

    Must be different from VPC 1 and the system VPC of the API gateway.

    172.X

    192.168.0.0/16

    192.X

    172.31.0.0/16

  3. Click Create Now.
  4. Repeat 2 to 3 to create VPC2 for running your backend services.

Buying a Dedicated API Gateway

  1. Log in to the API Gateway console.
  2. In the navigation pane, choose Dedicated Gateways.
  3. Click Buy Dedicated Gateway.

    Table 3 Gateway information

    Parameter

    Description

    Billing Mode

    Billing mode of the gateway. Select Pay-per-use.

    Region

    Select a region where the gateway is deployed.

    AZ

    The AZ where the gateway is located. Select AZ1.

    Gateway Name

    Name of the gateway. It is recommended that you enter a name based on certain naming rules to facilitate identification and search.

    Edition

    Select Professional. The edition cannot be changed after the gateway is created.

    Scheduled Maintenance

    Select a time period when the gateway can be maintained by technical support engineers. A period with low service traffic is recommended. For this example, retain the default value 22:00:00---02:00:00.

    Enterprise Project

    Select the enterprise project to which the gateway belongs. For this example, retain the default value default.

    Network

    Select VPC1 and a subnet.

    Security Group

    Click Manage Security Group and create a security group. Ensure that you have selected default for Enterprise Project.

    Description

    Description of the gateway.

  4. Click Next.
  5. If the gateway configurations are correct, read and confirm your acceptance of the customer agreement and privacy statement, and click Pay Now.

Buying a Load Balancer

  1. Log in to the network console.
  2. In the navigation pane, choose Elastic Load Balance > Load Balancers.
  3. Click Buy Elastic Load Balancer.
  4. Configure the load balancer information.

    Table 4 Load balancer parameters

    Parameter

    Description

    Type

    Type of the load balancer.

    Billing Mode

    By default, Pay-per-use is selected.

    Region

    The region where the gateway is located. Select CN North-Beijing4.

    AZ

    The AZ where the gateway is located. Select AZ1.

    Network Type

    Select Private IPv4 Network.

    VPC

    Select VPC2.

    Subnet

    Select a subnet.

    Specification

    Select Network load balancing.

    Name

    Name of the load balancer. It is recommended that you enter a name based on certain naming rules to facilitate identification and search.

    Enterprise Project

    Select default.

  5. Click Next.
  6. Confirm the configuration and click Submit.
  7. Add a listener.

    1. Click the name of the load balancer. On the Listeners tab page, click Add Listener.
    2. Configure the listener name, frontend protocol, and port, and click Next.
    3. Configure the backend server group name, backend protocol, and load balancing algorithm.
    4. Click Finish. The following figures show the configuration.
    Figure 2 Listener information
    Figure 3 Backend server group

Creating a VPC Peering Connection

  1. Log in to the network console.
  2. In the navigation pane, click VPC Peering.
  3. Click Create VPC Peering Connection and configure the parameters.

    Table 5 Configuring a VPC peering connection

    Parameter

    Description

    Name

    Name of the VPC peering connection. It is recommended that you enter a name based on certain naming rules to facilitate identification and search.

    Local VPC

    Select VPC1.

    Account

    By default, My account is selected.

    Peer Project

    Select a project

    Peer VPC

    Select VPC2.

  4. Click OK.
  5. In the displayed dialog box, click Add Route to go to the VPC peering connection details page.
  6. On the Local Routes tab page, click Route Tables.

    1. Under Routes, click Add Route.
    2. In the displayed dialog box, enter the route information.
      • Destination: Enter the service address displayed on the details page of the load balancer you purchased.
      • Next Hop Type: Select VPC peering connection.
    3. Click OK.
    Figure 4 Local routes

  7. Go to the Peer Routes tab page, and click Route Tables.

    1. Under Routes, click Add Route.
    2. In the displayed dialog box, enter the route information.
      • Destination: Enter the private egress address displayed on the details page of the dedicated gateway you purchased.
      • Next Hop Type: Select VPC peering connection.
    3. Click OK.
    Figure 5 Peer routes

Configuring a Route

  1. Log in to the API Gateway console.
  2. In the navigation pane, choose Dedicated Gateways.
  3. Go to the details page of the gateway you purchased.
  4. On the Basic Information tab page, click Edit in the Routes area, enter the IPv4 CIDR block of VPC 2 where the load balancer you purchased is located.

  5. Click Save.

Creating an API

  1. Log in to the API Gateway console.
  2. In the navigation pane, choose Dedicated Gateways.
  3. Go to the details page of the gateway you purchased.
  4. On the Create API page, configure the basic information and click Next.

    Table 6 Setting basic information

    Parameter

    Description

    Name

    API name. It is recommended that you enter a name based on certain naming rules to facilitate identification and search.

    API Group

    The default option is DEFAULT.

    Gateway Response

    Select a response to be displayed if API Gateway fails to process an API request.

    The default gateway response is default.

    Security Authentication

    API authentication mode. Select None.

  5. Define the API request parameters and click Next.

    Table 7 Parameters for defining API requests

    Parameter

    Description

    Protocol

    Request protocol of the API. Set this parameter to HTTPS.

    Path

    The path for requesting the API.

  6. Define the backend service parameters and click Next.

    Table 8 Parameters for defining an HTTP/HTTPS backend service

    Parameter

    Description

    Protocol

    Set this parameter to HTTP.

    Method

    Request method of the API. Set this parameter to GET.

    VPC Channel

    Determine whether the backend service will be accessed using a VPC channel. For this example, select Skip.

    Backend Address

    Enter the service address of the load balancer you purchased.

    Path

    Path of the backend service.

  7. Define the response and click Finish.

Buying an ECS

  1. Log in to the cloud server console.
  2. Click Buy ECS.
  3. Configure the basic settings and click Next: Configure Network.

    Table 9 Basic settings

    Parameter

    Description

    Billing Mode

    Select Pay-per-use.

    Region

    Select the region where the ECS is located.

    AZ

    Select the AZ where the ECS is located.

    CPU Architecture

    The default option is x86.

    Specifications

    Select specifications that match your service planning.

    Image

    Select an image that matches your service planning.

  4. Configure the network settings and click Next: Configure Advanced Settings.

    Table 10 Network settings

    Parameter

    Description

    Network

    Select VPC2 and a subnet.

    Security Group

    Select the security group created for the dedicated gateway you purchased.

    EIP

    Select Not required.

  5. Configure advanced settings and click Next: Confirm.

    Table 11 Advanced settings

    Parameter

    Description

    ECS Name

    ECS name. It is recommended that you enter a name based on certain naming rules to facilitate identification and search.

    Login Mode

    Credential for logging in to the ECS. The default option is Password.

    Username

    The default user is root.

    Password

    Set a password for logging in to the ECS.

    Confirm Password

    Enter the password again.

  6. Confirm the configuration and select enterprise project default.
  7. Select the check box next to Agreement and click Submit.

Debugging the API

  1. On the Backend Server Groups tab page of the load balancer you purchased, add the ECS you purchased.

  2. Start the ECS.
  3. Go to the APIs page of the dedicated gateway you purchased, choose More > Debug in the row that contains the API you created.
  4. Enter the request parameters and click Send Request.