Thuta Learning
AWS Advanced
Advanced NetworkingDevOpsAdvanced

Hybrid Connectivity: Site-to-Site VPN, Direct Connect and Hybrid DNS

AWS AdvancedLesson 850 min

What you'll walk away with

  • Configure a Site-to-Site VPN with both tunnels and BGP, on a virtual private gateway or a Transit Gateway
  • Choose between dedicated and hosted Direct Connect and the right virtual interface type
  • Design a resilient hybrid network with Direct Connect redundancy and VPN as backup
  • Use Route 53 Resolver inbound and outbound endpoints for hybrid DNS

Explain it simply

Explain it simply

Connecting your office to AWS is like connecting two cities. A Site-to-Site VPN is an armoured car on the public highway: quick to arrange and encrypted, but it shares the road with everyone, so speed and travel time vary. Direct Connect is a private railway line between the two cities: it takes weeks to build, but it carries much more, at a steady speed.

Serious companies keep both: the railway for daily traffic and the armoured car on standby for when the railway is closed. Finally, both cities need a shared phone book. Route 53 Resolver endpoints let servers on each side look up names that live on the other side.

Problem → Why → Solution

The problem: one tunnel, one link, no DNS

A company moves its order service to AWS but keeps the ERP database in its own data center. The first connection is a quick VPN: the network engineer configures one tunnel with a static route and calls it done. A month later AWS performs routine maintenance on that tunnel endpoint, the tunnel drops for several minutes, and every order fails because the application cannot reach the database.

Then a second problem appears: the application is configured with erp.corp.example.internal, a name only the on-premises DNS servers know. Instances in the VPC use the VPC resolver, which has never heard of that domain, so engineers start hard-coding IP addresses into configuration files.

the-fragile-setup.sh
# A single static route and (on the office router) only tunnel 1 configured
aws ec2 create-vpn-connection --type ipsec.1 \
  --customer-gateway-id cgw-0a1b2c3d4e5f67890 \
  --vpn-gateway-id vgw-0a1b2c3d4e5f67890 \
  --options StaticRoutesOnly=true
aws ec2 create-vpn-connection-route \
  --vpn-connection-id vpn-0a1b2c3d4e5f67890 \
  --destination-cidr-block 172.16.0.0/12

# From an EC2 instance in the VPC:
#   $ nslookup erp.corp.example.internal   -> NXDOMAIN

Why it fails

Every VPN connection comes with two tunnels precisely because AWS maintains them one at a time; using only one guarantees planned outages. Static routes cannot steer around failures, and a hybrid network without hybrid DNS pushes teams into fragile hard-coded IPs. Resilience has to be designed for links, routing and names together.

Core concept: links, routing and names

Site-to-Site VPN. You create a customer gateway resource describing your router (its public IP and BGP ASN) and a VPN connection that terminates either on a virtual private gateway attached to one VPC or on a Transit Gateway. Every VPN connection has two IPsec tunnels to two different AWS endpoints; configure both. With BGP, routes are exchanged dynamically and traffic shifts to the healthy tunnel within seconds. Static routing is only for devices that cannot run BGP.

Terminating on a Transit Gateway is the default choice at scale: one VPN reaches every attached VPC, and you can run several VPN connections with ECMP to add bandwidth, since a standard tunnel tops out at roughly 1.25 Gbps. A virtual private gateway is simpler but serves a single VPC and does not load-balance across tunnels.

Direct Connect. A physical connection from your router to AWS at a Direct Connect location. Dedicated connections are whole ports (1, 10, 100 Gbps and higher) ordered from AWS; hosted connections are smaller capacities provisioned through a Direct Connect Partner, often the faster option when you are not in a colocation facility. On top of the connection you create virtual interfaces (VIFs):

VIF typeReaches
Private VIFVPCs through a virtual private gateway or a Direct Connect gateway
Transit VIFTransit Gateways through a Direct Connect gateway
Public VIFAWS public endpoints such as S3 public IP ranges, without the internet

A Direct Connect gateway is a global object: one gateway can connect your VIFs to virtual private gateways or Transit Gateways in many Regions. Direct Connect traffic is private but not encrypted by default; MACsec adds layer-2 encryption on supported dedicated connections, and running an IPsec VPN over Direct Connect is the other common option.

Resilience and DNS. Connections fail, so production designs use at least two connections, ideally at two Direct Connect locations, and many add a VPN on the same Transit Gateway as a backup path. Finally, Route 53 Resolver joins the two DNS worlds: an inbound endpoint gives on-premises DNS servers IP addresses in your VPC to forward AWS private zones to, and an outbound endpoint plus forwarding rules sends queries for corporate domains to on-premises DNS servers. Remote users who need access from laptops are a different problem, handled by AWS Client VPN.

text
HYBRID EDGE ON A TRANSIT GATEWAY
--------------------------------
  ON-PREMISES 172.16.0.0/12                         AWS us-east-1
  +----------------------+
  | router (ASN 65010)   |==[ Direct Connect: transit VIF ]==> [DX gateway]
  |                      |                                         |
  |                      |--[ VPN tunnel 1 ]--+                    |
  |                      |--[ VPN tunnel 2 ]--+--> [VPN attach]    |
  | DNS 172.16.0.10      |                           |             |
  +----------------------+                           v             v
                                               +-------------------------+
                                               |     Transit Gateway     |
                                               |  DX preferred, VPN = B  |
                                               +-------------------------+
                                                   |               |
                                               prod VPC        shared VPC
                                                            (Resolver in/out)
Mental model
  1. VPN: 2 tunnels + BGP on the TGW
  2. DX: bandwidth and steady latency
  3. Second link: VPN or another DX
  4. Resolver endpoints join DNS

A hybrid design is only as resilient as its weakest layer: two links with one router, or two links with no DNS fallback, still fail together.

Working example: VPN on the Transit Gateway plus hybrid DNS

This CloudFormation template builds the AWS side of a small hybrid edge: a customer gateway for the office router, a BGP VPN connection on an existing Transit Gateway, and Route 53 Resolver inbound and outbound endpoints in the shared-services VPC, with a rule that forwards corp.example.internal to the on-premises DNS server.

hybrid-edge.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Hybrid edge - BGP VPN on a Transit Gateway plus hybrid DNS

Parameters:
  TransitGatewayId:
    Type: String
    Description: Existing Transit Gateway (tgw-...)
  VpcId:
    Type: AWS::EC2::VPC::Id
    Description: Shared-services VPC that hosts the Resolver endpoints
  SubnetA:
    Type: AWS::EC2::Subnet::Id
  SubnetB:
    Type: AWS::EC2::Subnet::Id
  OfficeRouterIp:
    Type: String
    Default: 203.0.113.10
  OnPremCidr:
    Type: String
    Default: 172.16.0.0/12
  OnPremDnsIp:
    Type: String
    Default: 172.16.0.10

Resources:
  OfficeRouter:
    Type: AWS::EC2::CustomerGateway
    Properties:
      Type: ipsec.1
      BgpAsn: 65010
      IpAddress: !Ref OfficeRouterIp
      Tags:
        - Key: Name
          Value: yangon-office

  OfficeVpn:
    Type: AWS::EC2::VPNConnection
    Properties:
      Type: ipsec.1
      CustomerGatewayId: !Ref OfficeRouter
      TransitGatewayId: !Ref TransitGatewayId
      StaticRoutesOnly: false # BGP: routes and failover are dynamic
      Tags:
        - Key: Name
          Value: yangon-office-vpn

  ResolverSg:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: DNS between on-premises and the Resolver endpoints
      VpcId: !Ref VpcId
      SecurityGroupIngress:
        - { IpProtocol: udp, FromPort: 53, ToPort: 53, CidrIp: !Ref OnPremCidr }
        - { IpProtocol: tcp, FromPort: 53, ToPort: 53, CidrIp: !Ref OnPremCidr }
      SecurityGroupEgress:
        - { IpProtocol: udp, FromPort: 53, ToPort: 53, CidrIp: !Ref OnPremCidr }
        - { IpProtocol: tcp, FromPort: 53, ToPort: 53, CidrIp: !Ref OnPremCidr }

  InboundEndpoint: # on-prem DNS forwards AWS private zones to these IPs
    Type: AWS::Route53Resolver::ResolverEndpoint
    Properties:
      Name: from-on-prem
      Direction: INBOUND
      SecurityGroupIds: [!Ref ResolverSg]
      IpAddresses:
        - SubnetId: !Ref SubnetA
        - SubnetId: !Ref SubnetB

  OutboundEndpoint: # VPC queries for corporate domains leave through these
    Type: AWS::Route53Resolver::ResolverEndpoint
    Properties:
      Name: to-on-prem
      Direction: OUTBOUND
      SecurityGroupIds: [!Ref ResolverSg]
      IpAddresses:
        - SubnetId: !Ref SubnetA
        - SubnetId: !Ref SubnetB

  CorpForwardRule:
    Type: AWS::Route53Resolver::ResolverRule
    Properties:
      Name: corp-example-internal
      DomainName: corp.example.internal
      RuleType: FORWARD
      ResolverEndpointId: !GetAtt OutboundEndpoint.ResolverEndpointId
      TargetIps:
        - Ip: !Ref OnPremDnsIp
          Port: "53"

  CorpForwardRuleAssociation:
    Type: AWS::Route53Resolver::ResolverRuleAssociation
    Properties:
      ResolverRuleId: !GetAtt CorpForwardRule.ResolverRuleId
      VPCId: !Ref VpcId

Outputs:
  VpnConnectionId:
    Value: !Ref OfficeVpn
  InboundEndpointId:
    Value: !GetAtt InboundEndpoint.ResolverEndpointId
  CorpRuleId:
    Description: Share this rule with other accounts through AWS RAM
    Value: !GetAtt CorpForwardRule.ResolverRuleId

Code breakdown

  1. Customer gatewayBgpAsn: 65010

    Describes your router to AWS: its public IP and private ASN. It must differ from the Transit Gateway's Amazon-side ASN so BGP can tell the two sides apart.

  2. BGP VPN on the TGWStaticRoutesOnly: false

    The connection gets two tunnels automatically. With BGP, the office advertises 172.16.0.0/12, the TGW advertises the VPC ranges it knows, and a failed tunnel is withdrawn without anyone editing routes. The VPN becomes a TGW attachment you associate and propagate like any other.

  3. Two AZs per endpointIpAddresses: - SubnetId: !Ref SubnetA - SubnetId: !Ref SubnetB

    Each Resolver endpoint needs at least two IP addresses; putting them in different AZs keeps DNS working through an AZ failure. Fixed IPs are optional; without them AWS assigns addresses you look up afterwards.

  4. Forwarding ruleDomainName: corp.example.internal

    Queries for this domain (and its subdomains) from associated VPCs go out through the outbound endpoint to 172.16.0.10. Everything else is still answered by the normal VPC resolver.

You should see
After aws cloudformation deploy, the stack outputs a VpnConnectionId (vpn-...), an InboundEndpointId and a CorpRuleId. Both tunnels show DOWN until the office router is configured from the downloaded configuration; once BGP is up, describe-vpn-connections reports both tunnels UP, and nslookup erp.corp.example.internal from an EC2 instance in the shared VPC returns the on-premises address.
DNS lookup from a VPC instance to an on-prem name
  1. App asks the VPC resolver (.2)
  2. Rule matches corp.example.internal?
  3. Outbound endpoint forwards query
  4. On-prem DNS 172.16.0.10 answers

Name under corp.example.internal

Answered by on-prem DNS

Any other name

Answered by the VPC resolver

Direct Connect with a Transit Gateway

Once a connection is provisioned (a letter of authorization, a cross-connect in the colocation facility, and usually a few weeks), the AWS side is three objects: a Direct Connect gateway, an association between that gateway and your Transit Gateway listing the prefixes to advertise to on-premises, and a transit VIF on the connection that terminates on the gateway.

The allowed prefixes on the association are what your data center learns, so advertise summaries such as 10.0.0.0/8 rather than every VPC. Use different private ASNs for the Direct Connect gateway, the Transit Gateway and your router.

direct-connect.sh
TGW_ID=tgw-0a1b2c3d4e5f67890

# 1. Global Direct Connect gateway (its own private ASN)
DXGW_ID=$(aws directconnect create-direct-connect-gateway \
  --direct-connect-gateway-name core-dxgw --amazon-side-asn 64600 \
  --query 'directConnectGateway.directConnectGatewayId' --output text)

# 2. Associate it with the Transit Gateway; these prefixes go to on-prem
aws directconnect create-direct-connect-gateway-association \
  --direct-connect-gateway-id $DXGW_ID --gateway-id $TGW_ID \
  --add-allowed-prefixes-to-direct-connect-gateway cidr=10.0.0.0/8

# 3. Transit VIF on the physical connection, landing on the DX gateway
VIF="virtualInterfaceName=prod-transit,vlan=101,asn=65010,mtu=8500"
VIF="$VIF,addressFamily=ipv4,directConnectGatewayId=$DXGW_ID"
aws directconnect create-transit-virtual-interface \
  --connection-id dxcon-fg5678gh --new-transit-virtual-interface "$VIF"

# 4. Health checks you will run often
aws directconnect describe-virtual-interfaces --connection-id dxcon-fg5678gh \
  --query 'virtualInterfaces[].[virtualInterfaceName,virtualInterfaceState]'
aws ec2 describe-vpn-connections --vpn-connection-ids vpn-0a1b2c3d4e5f67890 \
  --query 'VpnConnections[0].VgwTelemetry[].[OutsideIpAddress,Status]' \
  --output table

Resiliency models and which path wins

ModelShapeSurvives
Development and testTwo connections at one location, separate devicesOne device or port failure
High resiliencyOne connection at each of two locationsA whole location failure
Maximum resiliencyTwo connections at each of two locationsA location failure plus a device failure
DX plus VPN backupOne or more DX links, VPN on the same TGWDX loss, at reduced bandwidth

When the same destination is learned over Direct Connect and over VPN, the Transit Gateway first applies longest-prefix match, and only for equal prefixes does it prefer Direct Connect routes over VPN routes. That is how a VPN backup stays idle until the Direct Connect route is withdrawn. Advertise identical prefixes over both paths, and use BGP attributes such as AS-path prepending on your router to influence the return direction.

In production

In production

A typical rollout starts with VPN because it is ready the same day, then adds Direct Connect for bandwidth and predictable latency once the migration moves real data. A common end state is two hosted or dedicated connections at two locations into a Direct Connect gateway and transit VIFs, with a BGP VPN on the same Transit Gateway as a last-resort path. The network team tests failover on a schedule by shutting down a BGP session and watching traffic move, because an untested backup path is usually a broken one.

DNS is centralized in the shared-services VPC: one pair of inbound and one pair of outbound endpoints, forwarding rules shared to every account through AWS RAM and associated with each spoke VPC. On-premises DNS servers get conditional forwarders for the AWS private hosted zones pointing at the inbound endpoint IPs.

Staff working from home are handled separately with AWS Client VPN, a managed OpenVPN-based remote-access service that authenticates users against Active Directory or a SAML identity provider and lands them in a VPC attached to the same Transit Gateway.

Common mistakes

Common mistakes

Mistake 1: configuring only one VPN tunnel

AWS updates tunnel endpoints one at a time and notifies you in advance, assuming the second tunnel will carry traffic. A router with only tunnel 1 configured goes dark during every maintenance window. Configure both tunnels, use BGP, and alarm on the TunnelState CloudWatch metric for each tunnel.

Mistake 2: a backup path that wins by accident

If the office advertises 172.16.0.0/16 over VPN but only 172.16.0.0/12 over Direct Connect, longest-prefix match sends traffic over the VPN all the time and the expensive Direct Connect link sits idle. The opposite mistake, a VPN that was never tested, fails exactly when Direct Connect does. Advertise identical prefixes on both and test failover regularly.

Mistake 3: assuming Direct Connect is encrypted

Direct Connect is a private circuit, not an encrypted one. If compliance requires encryption in transit, use TLS end to end, MACsec on supported dedicated connections, or an IPsec VPN over the Direct Connect link, and document which one you chose.

Best practices

Best practices

  • Terminate VPN and Direct Connect on a Transit Gateway so every VPC benefits from one edge.
  • Use BGP everywhere, with distinct private ASNs for your router, the TGW and the Direct Connect gateway.
  • Build at least two physical paths and test failover on a schedule.
  • Centralize Resolver endpoints and share forwarding rules with AWS RAM instead of running DNS servers on EC2.

💰 Cost & Cleanup

A Site-to-Site VPN connection is billed per connection-hour plus data transfer out, and on a Transit Gateway it is also an attachment billed hourly and per GB. Route 53 Resolver endpoints are billed per IP address (ENI) per hour, so the two endpoints with two IPs each in this lab bill four ENI-hours every hour, plus a charge per million queries forwarded. Direct Connect bills port-hours for the connection plus reduced-rate data transfer out, and a dedicated port keeps billing until you delete the connection. Nothing here is free tier; delete the stack as soon as you finish, and check each service's pricing page for current rates.

Clean up when you are done

bash
# VPN, customer gateway and Resolver endpoints from the template
aws cloudformation delete-stack --stack-name hybrid-edge
aws cloudformation wait stack-delete-complete --stack-name hybrid-edge

# Only if you created Direct Connect objects:
aws directconnect delete-virtual-interface --virtual-interface-id dxvif-ffabc123
aws directconnect delete-direct-connect-gateway-association \
  --association-id $DXGW_ASSOC_ID
aws directconnect delete-direct-connect-gateway --direct-connect-gateway-id $DXGW_ID

Mini challenge

Mini challenge: make AWS names resolvable on-premises

The template already forwards corp.example.internal outward. Now on-premises servers must resolve names in an AWS private hosted zone, aws.example.internal, that is associated with the shared VPC. Find the inbound endpoint IPs the on-prem DNS team needs and give them a test command.

  • Read the InboundEndpointId from the stack outputs
  • List the endpoint's IP addresses with the route53resolver CLI
  • Write the conditional-forwarder instruction and a dig test for on-prem

Expected: Two private IPs in the shared VPC (one per AZ). After the on-prem team adds a conditional forwarder for aws.example.internal to both, dig from an office server returns the record from the private hosted zone.

Show hint

describe-stacks with a --query on Outputs gets the ID; list-resolver-endpoint-ip-addresses returns one entry per IP. The forwarder target is those IPs, not the VPC resolver at the .2 address, which on-premises hosts cannot reach.

Show solution
bash
INBOUND_ID=$(aws cloudformation describe-stacks --stack-name hybrid-edge \
  --query "Stacks[0].Outputs[?OutputKey=='InboundEndpointId'].OutputValue" \
  --output text)

aws route53resolver list-resolver-endpoint-ip-addresses \
  --resolver-endpoint-id $INBOUND_ID \
  --query 'IpAddresses[].[Ip,SubnetId,Status]' --output table

# Hand to the on-prem DNS team:
#   conditional forwarder: aws.example.internal -> both IPs above
# Test from an office server (replace with one of the listed IPs):
dig +short app.aws.example.internal @10.24.0.37

Quick quiz 1 / 3

An office router has only tunnel 1 of a Site-to-Site VPN configured. What happens when AWS performs maintenance on that tunnel endpoint?

Quick quiz 2 / 3

A Transit Gateway has a Direct Connect path and a VPN backup, both using BGP. On-premises advertises 172.16.0.0/12 over Direct Connect and 172.16.0.0/16 over VPN. Which path carries traffic to 172.16.5.10?

Quick quiz 3 / 3

EC2 instances must resolve erp.corp.example.internal, which only exists on the on-premises DNS servers. What do you set up?

What to remember

  • Every Site-to-Site VPN has two tunnels; configure both and use BGP, preferably on a Transit Gateway.
  • Direct Connect gives private, high-bandwidth links; pick dedicated or hosted, and private, transit or public VIFs.
  • Resilience means two physical paths and tested failover; longest-prefix match beats the DX-over-VPN preference.
  • Route 53 Resolver inbound endpoints serve on-prem lookups of AWS names; outbound endpoints and rules do the reverse.

What is AWS Site-to-Site VPN? — AWS Documentation

What is AWS Direct Connect? — AWS Documentation

What is Route 53 Resolver? — AWS Documentation

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Hybrid Connectivity: Site-to-Site VPN, Direct Connect and Hybrid DNS | Thuta Learning