Free consultation

Guides

Guide for org admins

A VPC with public and private subnets

The classic layout for a web application, the same way you would build it in a public cloud. The web server faces the internet; the application server never does.

You need: the org Admin role (to switch on internet access), and two free public IPs in your org: one for the NAT gateway and one for the web server. Networking › Public IPs shows yours; your provider can allocate more.

Part Name in this guide Example
VPC shop 10.20.0.0/16
Public subnet web 10.20.1.0/24, routed to the internet gateway
Private subnet app 10.20.2.0/24, routed to the NAT gateway
NAT gateway shop-nat One public IP, shared by the private VMs for outbound traffic
Security groups web, app HTTP and HTTPS to the web server; only the web server reaches the app server

1. Create the VPC

  1. Open Networking › VPCs and choose Create VPC.
  2. Enter the Name shop. Leave CIDR empty to take the free range host42 suggests, or enter your own private range such as 10.20.0.0/16.
  3. Choose Create.

A new VPC is private: nothing in it reaches the internet yet.

2. Add a NAT gateway and switch on internet access

  1. Open Networking › NAT gateways and choose Create NAT gateway.
  2. Choose the VPC shop, the Name shop-nat and one of your free Public IPs. Choose Create.
  3. Open the VPC (Networking › VPCs › shop) and turn on Internet access.

With internet access on, each subnet reaches the internet the way its route table says. Turn it off and every route to the internet drops at once.

3. Route tables

Each subnet uses one route table, and each table has at most one way out to the internet.

  1. Open Networking › Route tables. The VPC already has its main table.
  2. Open main and add a route: Destination 0.0.0.0/0, Target NAT gateway (shared outbound address), and under Which, shop-nat. Choose Add route.
  3. Choose Create route table, pick the VPC shop and name it public. Open it and add the route 0.0.0.0/0 with the target Internet gateway (VMs with a public IP).

The longest matching destination wins, and traffic inside the VPC always stays local.

4. Subnets

Open Networking › Subnets and choose Create subnet twice:

Name CIDR Route table
web 10.20.1.0/24 public
app 10.20.2.0/24 main

Each subnet hands out its addresses by DHCP, with its gateway at the first address. You can move a subnet to another table later from the route table's page (Use for subnet, Associate).

5. Security groups

Create two groups in the VPC shop under Networking › Security groups:

  1. web: under Inbound, use Quick add for HTTP and HTTPS (from anywhere), and add tcp port 22 from your office's address range for SSH. Choose Save rules.
  2. app: under Inbound, set Protocol to tcp and the port your application listens on (for example 8080), switch the source from CIDR to Group and pick web. Choose Add, then Save rules.

Only VMs in the group web can now reach the application port, wherever their addresses are.

6. The servers

Create two VMs under Compute › Virtual machines › Create VM, as in your first virtual machine:

VM Subnet Security group Public IP
shop-web web web Yes, 1:1 NAT
shop-app app app None

7. Check it

  • From your browser, the web server answers on its public IP.
  • From shop-web, the application answers on shop-app's private address and port.
  • shop-app installs updates through the NAT gateway, but nothing from the internet reaches it.

Going further

  • Network ACLs (Networking › Network ACLs) add ordered allow and deny rules at the edge of a subnet, checked before security groups.
  • Peering (Networking › Peering) connects two VPCs of your org, without transit.
  • Port forwards publish one port of a public IP to a private VM, without giving it its own address.
  • To inspect all traffic with a firewall, see put gate42 in front of your org.