Creating a VPC Peering Connection to Connect Two VPCs in Different Accounts
Scenarios
Two VPCs from the same region cannot communicate with each other by default, but you can use a VPC peering connection to connect them.
The following describes how to create a VPC peering connection to connect two VPCs, vpc-A in one account and vpc-B in another account. In this way, instances (ECS-A01 and RDS-B01 in this example) in the two VPCs can communicate with each other.
The procedure is as follows:
Step 1: Create a VPC Peering Connection
Step 2: Peer Account Accepts the VPC Peering Connection Request
Step 3: Add Routes for the VPC Peering Connection
Step 4: Configure Security Group Rules for Instances in Local and Peer VPCs
Step 5: Verify Network Connectivity
Currently, VPC peering connections are free.
Notes and Constraints
- Only one VPC peering connection can be created between two VPCs at the same time.
- A VPC peering connection can only connect VPCs in the same region.
- A VPC peering connection can enable a VPC created on the Huawei Cloud Chinese Mainland website to connect to one created on the Huawei Cloud International website, but the VPCs must be in the same region. For example, if the VPC created on the Chinese Mainland website is in the CN-Hong Kong region, then the VPC created on the International website must also be in the CN-Hong Kong region.
- If you want to connect VPCs in different regions, you can use Cloud Connect.
- If you only need a few ECSs in different regions to communicate with each other, you can assign and bind EIPs to the ECSs.
- If the local and peer VPCs have overlapping CIDR blocks, the VPC peering connection may not be usable.
In this case, you can configure the network by referring to VPC Peering Connection Usage Examples.
- For a VPC peering connection between VPCs in different accounts:
- If account A initiates a request to create a VPC peering connection with a VPC in account B, the VPC peering connection takes effect only after account B accepts the request.
- To ensure network security, do not accept VPC peering connections from unknown accounts.
Prerequisites
You have two VPCs in the same region, but they are from different accounts. If you want to create one, see Creating a VPC and Subnet.
Step 1: Create a VPC Peering Connection
- Go to the VPC peering connection list page.
- In the upper right corner of the page, click Create VPC Peering Connection.
The Create VPC Peering Connection page is displayed.
- Configure the parameters as prompted.
For details, see Table 1.
Figure 2 Creating a VPC peering connection
Table 1 Parameters for creating a VPC peering connection Parameter
Description
Example Value
Region
Mandatory
The region where the VPC peering connection is created. Select the region nearest to you to ensure the lowest latency possible.
CN-Hong Kong
VPC Peering Connection Name
Mandatory
Enter a name for the VPC peering connection.
The name can contain a maximum of 64 characters, including letters, digits, hyphens (-), and underscores (_).
peering-AB
Description (Optional)
Optional
Enter a description of the VPC peering connection in the text box as required. The description can contain a maximum of 255 characters and cannot contain angle brackets (< or >).
peering-AB connects vpc-A and vpc-B.
Local VPC
Mandatory
VPC at one end of the VPC peering connection. You can select one from the drop-down list.
vpc-A
Local VPC CIDR Block
CIDR block of the selected local VPC
172.16.0.0/16
Account
Mandatory
- Options: My account and Another account
- Select Another account.
Another account
Peer Project ID
This parameter is mandatory if Account is set to Another account.
The project ID of the region that the peer VPC resides. For details about how to obtain the project ID, see Obtaining the Peer Project ID of a VPC Peering Connection.
Project ID of vpc-B in region A:
067cf8aecf3XXX08322f13b
Peer VPC ID
This parameter is mandatory if Account is set to Another account.
ID of the VPC at the other end of the VPC peering connection. For details about how to obtain the ID, see Obtaining a VPC ID.
vpc-B ID:
17cd7278-XXX-530c952dcf35
- Click Create Now.
- If the message "Invalid VPC ID and project ID." is displayed, check whether the project ID and VPC ID are correct.
- Peer Project ID: The value must be the project ID of the region where the peer VPC resides.
- The local and peer VPCs must be in the same region.
- If the status of the created VPC peering connection is Awaiting acceptance, go to Step 2: Peer Account Accepts the VPC Peering Connection Request.
Figure 3 Awaiting acceptance
- If the message "Invalid VPC ID and project ID." is displayed, check whether the project ID and VPC ID are correct.
Step 2: Peer Account Accepts the VPC Peering Connection Request
After you create a VPC peering connection with a VPC in another account, you need to contact the peer account to accept the VPC peering connection request. In this example, account A notifies account B to accept the request. Account B needs to:
- Log in to the management console.
- Click in the upper left corner and choose Networking > Virtual Private Cloud.
The Virtual Private Cloud page is displayed.
- In the navigation pane on the left, choose Virtual Private Cloud > VPC Peering Connections.
The VPC peering connection list is displayed.
- In the upper part of the VPC peering connection list, locate the VPC peering connection request to be accepted.
Figure 4 Accept Request
- Locate the row that contains the target VPC peering connection and click Accept Request in the Operation column.
After the status of the VPC peering connection changes to Accepted, the VPC peering connection is created.
- Go to Step 3: Add Routes for the VPC Peering Connection.
Step 3: Add Routes for the VPC Peering Connection
To enable communications between VPCs connected by a VPC peering connection, you need to add both forward and return routes to the route tables of the VPCs. For details, see VPC Peering Connection Usage Examples.
Both accounts need to add a route to the route table of their VPC. In this example, account A adds a route to the route table of VPC-A, and account B adds a route to the route table of VPC-B.
- Add routes to the route table of the local VPC:
- In the VPC peering connection list of the local account, click the name of the target VPC peering connection.
The page showing the VPC peering connection details is displayed.
- In the lower part of the VPC peering connection details page, click Add Route.
The Add Route dialog box is displayed.
Figure 5 Add Route
- Add routes to the route tables as prompted.
Table 2 describes the parameters.
Table 2 Parameter description Parameter
Description
Example Value
VPC
By default, the VPC in the current account is selected. You do not need to select a VPC.
vpc-A
Route Table
Select the route table of the VPC. The route will be added to this route table.
Each VPC comes with a default route table to control the outbound traffic from the subnets in the VPC. In addition to the default route table, you can also create a custom route table and associate it with the subnets in the VPC. Then, the custom route table controls outbound traffic of the subnets.- If there is only the default route table in the drop-down list, select the default route table.
- If there are both default and custom route tables in drop-down list, select the route table associated with the subnet connected by the VPC peering connection.
rtb-vpc-A (Default route table)
Destination
An IP address or address range in the VPC being connected by the VPC peering connection. The value can be a VPC CIDR block, subnet CIDR block, or ECS IP address. For details about the route configuration example, see VPC Peering Connection Usage Examples.
vpc-B CIDR block: 172.17.0.0/16
Next Hop
The default value is the current VPC peering connection. You do not need to specify this parameter.
peering-AB
Description
Supplementary information about the route. This parameter is optional.
The description can contain a maximum of 255 characters and cannot contain angle brackets (< or >).
Route from vpc-A to vpc-B
- Click OK.
You can view the routes in the route list.
- In the VPC peering connection list of the local account, click the name of the target VPC peering connection.
- Add routes to the route table of the peer VPC:
- In the VPC peering connection list of the peer account, click the name of the target VPC peering connection.
The page showing the VPC peering connection details is displayed.
- In the lower part of the VPC peering connection details page, click Add Route.
The Add Route dialog box is displayed.
Figure 6 Add Route
- Add routes to the route table as prompted.
Table 3 describes the parameters.
Table 3 Parameter description Parameter
Description
Example Value
VPC
By default, the VPC in the current account is selected. You do not need to select a VPC.
vpc-B
Route Table
Select the route table of the VPC. The route will be added to this route table.
Each VPC comes with a default route table to control the outbound traffic from the subnets in the VPC. In addition to the default route table, you can also create a custom route table and associate it with the subnets in the VPC. Then, the custom route table controls outbound traffic of the subnets.- If there is only the default route table in the drop-down list, select the default route table.
- If there are both default and custom route tables in drop-down list, select the route table associated with the subnet connected by the VPC peering connection.
rtb-vpc-B (Default route table)
Destination
An IP address or address range in the VPC being connected by the VPC peering connection. The value can be a VPC CIDR block, subnet CIDR block, or ECS IP address. For details about the route configuration example, see VPC Peering Connection Usage Examples.
vpc-A CIDR block: 172.16.0.0/16
Next Hop
The default value is the current VPC peering connection. You do not need to specify this parameter.
peering-AB
Description
Supplementary information about the route. This parameter is optional.
The description can contain a maximum of 255 characters and cannot contain angle brackets (< or >).
Route from vpc-B to vpc-A.
- Click OK.
You can view the routes in the route list.
- In the VPC peering connection list of the peer account, click the name of the target VPC peering connection.
Step 4: Configure Security Group Rules for Instances in Local and Peer VPCs
Direction |
Action |
Type |
Protocol & Port |
Source/Destination |
Description |
---|---|---|---|---|---|
Inbound |
Allow |
IPv4 |
All |
Source: current security group |
Allows the instances in the security group to communicate with each other over any IPv4 protocol and port. |
Inbound |
Allow |
IPv6 |
All |
Source: current security group |
Allows the instances in the security group to communicate with each other over any IPv6 protocol and port. |
Outbound |
Allow |
IPv4 |
All |
Destination: 0.0.0.0/0 |
Allows all IPv4 traffic from the instances in the security group to external resources over any protocol and port. |
Outbound |
Allow |
IPv6 |
All |
Destination: ::/0 |
Allows all IPv6 traffic from the instances in the security group to external resources over any protocol and port. |
As you can see, the above preset rules only allow instances in the security group to communicate with each other. To allow external traffic to access the instances in the security group, add inbound rules by referring to Adding a Security Group Rule.
The instances in the local and peer VPCs in different accounts are associated with different security groups, so they cannot communicate with each other unless you add rules to allow them to. For example, if ECS-A01 is associated with security group Sg-A, and RDS-B01 is associated with security group Sg-B, you need to perform the following operations to allow remote logins:
- Add security group rules in Table 5 to allow remote logins.
Table 5 Security group rules for remote logins Direction
Action
Type
Protocol & Port
Source
Description
Inbound
Allow
IPv4
TCP: 22
IP address: 0.0.0.0/0
Allows any IPv4 address to remotely log in to the Linux instances in Sg-AB over SSH port 22.
Inbound
Allow
IPv4
TCP: 3389
IP address: 0.0.0.0/0
Allows any IPv4 address to remotely log in to the Windows instances in Sg-AB over RDP port 3389.
If the source of an inbound rule is set to 0.0.0.0/0, all external IP addresses are allowed to remotely log in to your cloud server. Exposing port 22 or 3389 to the public network will leave your instances vulnerable to network risks. To address this issue, set the source to a known IP address, for example, the IP address of your local PC.
- Add security group rules in Table 6 to enable instances in Sg-A and Sg-B to communicate with each other.
Set Source to the CIDR block of peer VPC or subnet.
Table 6 Security group rules (CIDR block as the source) Security Group
Direction
Action
Type
Protocol & Port
Source
Description
Sg-A
Inbound
Allow
IPv4
All
IP address: 172.17.0.0/16 (vpc-B CIDR block)
Allows traffic from 172.17.0.0/16 to access instances in Sg-A over any IPv4 protocol and port.
Sg-B
Inbound
Allow
IPv4
All
IP address: 172.16.0.0/16 (vpc-A CIDR block)
Allows traffic from 172.16.0.0/16 to access instances in Sg-B over any IPv4 protocol and port.
Step 5: Verify Network Connectivity
- Log in to ECS-A01 in the local VPC.
Multiple methods are available for logging in to an ECS. For details, see Logging In to an ECS.
- Check whether ECS-A01 can communicate with RDS-B01.
Example command:
ping 172.17.0.21
If information similar to the following is displayed, ECS-A01 and RDS-B01 can communicate with each other, and the VPC peering connection between VPC-A and VPC-B is successfully created.[root@ecs-A01 ~]# ping 172.17.0.21 PING 172.17.0.21 (172.17.0.21) 56(84) bytes of data. 64 bytes from 172.17.0.21: icmp_seq=1 ttl=64 time=0.849 ms 64 bytes from 172.17.0.21: icmp_seq=2 ttl=64 time=0.455 ms 64 bytes from 172.17.0.21: icmp_seq=3 ttl=64 time=0.385 ms 64 bytes from 172.17.0.21: icmp_seq=4 ttl=64 time=0.372 ms ... --- 172.17.0.21 ping statistics ---
If VPCs connected by a VPC peering connection cannot communicate with each other, refer to Why Did Communication Fail Between VPCs That Were Connected by a VPC Peering Connection?
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot