This topic describes how to configure DNAT for a NAT gateway. DNAT allows Elastic Compute Service (ECS) instances to provide Internet-facing services.

Scenarios

The scenario in the following figure is used as an example. A company has created an ECS instance on Alibaba Cloud and deployed applications on the ECS instance. The ECS instance is not assigned a static public IP address. In addition, no elastic IP address (EIP) is associated with the ECS instance. To meet business requirements, the company wants the applications to be accessible over the Internet.

You can use DNAT to map an EIP that is associated with the NAT gateway to the private IP address of the ECS instance. This way, the ECS instances can provide Internet-facing services. Scenarios

Prerequisites

Make sure that the following requirements are met:
  • An Alibaba Cloud account is created. If you do not have an Alibaba Cloud account, create one.
  • A virtual private cloud (VPC) and a vSwitch are created. For more information, see Create a VPC and Create a vSwitch.
  • An ECS instance is created and attached to the vSwitch. Applications are deployed on the ECS instance. For more information, see Create an instance by using the wizard.

Procedure

Procedure

Step 1: Create a NAT gateway

  1. Log on to the NAT Gateway console.
  2. On the NAT Gateway page, click Create NAT Gateway.
  3. The first time you purchase an enhanced NAT gateway, you must create a service-linked role for NAT Gateway. At the bottom of the Create NAT Gateway panel, click Create in the Notes on Creating Service-linked Roles section. After the service-linked role is created, you can purchase a NAT gateway.
  4. In the Create NAT Gateway panel, set the following parameters and click Buy Now:
    • Region and Zone: Select the region where you want to deploy the NAT gateway.
    • Zone: Select the zone where you want to deploy the NAT gateway.
    • VPC ID: Select the VPC where you want to deploy the NAT gateway. After the NAT gateway is created, you cannot change the VPC where the NAT gateway is deployed.
    • VSwitch ID: Select the vSwitch to which the NAT gateway is attached.
    • Gateway Type: By default, Enhanced is selected.
    • Billing Method: Select a billing method for the NAT gateway.

      Only the pay-by-data-transfer billing method is supported. For more information, see Pay-by-data-transfer.

    • Billing Cycle: By default, By Hour is selected. Bills are generated on an hourly basis. If you use a NAT gateway for less than one hour, the usage duration is rounded up to one hour.
  5. On the Confirm Order page, confirm the configuration of the NAT gateway, select the service agreement, and then click Activate Now.
    When the message Order complete. appears, the purchase is completed.

After you create a NAT gateway, you can find the NAT gateway on the NAT Gateway page.

Step 2: Associate the NAT gateway with an EIP

A NAT gateway works as expected only when you associate an EIP with the NAT gateway. After you create a NAT gateway, you can associate EIPs with the NAT gateway.

  1. Log on to the NAT Gateway console.
  2. In the top navigation bar, select the region where the NAT gateway is deployed.
  3. On the NAT Gateway page, find the NAT gateway that you want to manage and click Associate Now in the Elastic IP Address column.
  4. In the Associate EIP dialog box, set the parameters and click OK. The following table describes the parameters.
    Parameter Description
    Resource Group Select the resource group to which the EIP belongs.
    EIPs Select the EIP that you want to associate with the NAT gateway.

    Purchase EIPs is selected in this example. The system automatically creates a pay-by-data-transfer EIP and associates the EIP with the NAT gateway.

    After you associate an EIP with the NAT gateway, the EIP is displayed in the Elastic IP Address column.

Step 3: Create a DNAT entry

A DNAT entry maps an EIP of a NAT gateway to an ECS instance. This allows the ECS instance to provide Internet-facing services.

  1. Log on to the NAT Gateway console.
  2. In the top navigation bar, select the region where the NAT gateway is deployed.
  3. On the NAT Gateway page, find the NAT gateway that you want to manage, and click Configure DNAT in the Actions column.
  4. On the DNAT Management tab, click Create DNAT Entry.
  5. On the Create DNAT Entry page, set the parameters that are described in the following table and click Confirm.
    Parameter Description
    Select Public IP Address Select an EIP from the drop-down list. The EIP is used to communicate with the Internet.
    Note
    • For standard NAT gateways, you cannot specify an EIP in both an SNAT entry and a DNAT entry.
    • For enhanced NAT gateways, you can specify an EIP in both an SNAT entry and a DNAT entry.
    Select Private IP Address Select the ECS instance that uses the DNAT entry to communicate with the Internet. You can use one of the following methods to specify the private IP address of the ECS instance:
    • Select by ECS or ENI: Select the ECS instance or the elastic network interface (ENI) associated with the ECS instance from the drop-down list.
    • Manual Input: Enter the private IP address of the destination ECS instance.
    Port Settings Select a DNAT mapping method:
    • Any Port: specifies IP mapping. The requests destined for the EIP are forwarded to the specified ECS instance. The specified ECS instance can use the EIP to access the Internet.
      Note
      • If IP mapping is configured for an EIP in a DNAT entry, the EIP cannot be used in another DNAT entry or SNAT entry.
      • If a NAT gateway is configured with an SNAT entry and a DNAT entry that uses IP mapping, the ECS instance preferentially uses the DNAT entry to access the Internet.
    • Specific Port: specifies port mapping. The NAT gateway forwards requests to the specified ECS instance based on the specified protocol and ports.
      After you select Specific Port, set the following parameters based on your business requirements:
      • Public Port: the external port that is used in port forwarding.
      • Private Port: the internal port that is used in port forwarding.
      • Protocol Type: the protocol used by the ports.
    Entry Name Enter a name for the DNAT entry.

    The name must be 2 to 128 characters in length, and can contain digits, underscores (_), and hyphens (-). It must start with a letter.

Step 4: Verify network connectivity

After you create a DNAT entry, you can verify the network connectivity by using a computer to access an application that runs on the ECS instance.
Note Make sure that the security group rules of the ECS instance allow the ECS instance to receive requests from the Internet. For more information, see Overview.
  1. Open a browser on a computer.
  2. Enter the EIP that is associated with the NAT gateway into the address bar of the browser and access an application that runs on the ECS instance.
    The test result shows that the ECS instance can receive requests from the Internet. Test result 1