Sitemap

How to Setup Transit Gateway + Site to Site VPN and NAT Gateway?

6 min readDec 11, 2024

I have previously published two articles explaining the separate installation of Transit Gateway and IPSec.

Press enter or click to view image in full size
Photo by Thomas Jensen on Unsplash

They are here:

How to Setup IPSec Between 3 Linux Servers?
How to Enable Peering Between Multiple (Inter-Region) VPCs via Transit Gateway?

In this article, I’ll walk through establishing the transit gateway and site-to-site VPN together.

Using the tutorial and a little tweak to the configuration, I set up a Linux virtual machine on Digitalocean and installed Strongswan.

The start parameter will be set to add.

In IPsec, one side of the tunnel should initiate the connection. In this tutorial, the AWS side will establish the connection.

what’s the difference between auto=add, and auto=start?

Network configurations for instance in Digitalocean are as follows:

Public IP Address: 164.92.144.165
Private IP Address: 10.110.0.2
Subnet: 10.110.0.0/20

Create Customer Gateway and Transit Gateway

I’ll create a customer gateway:

Press enter or click to view image in full size

Enter the public IP address of your pair and the name of the customer gateway:

Press enter or click to view image in full size

The customer gateway will be available:

Press enter or click to view image in full size

I’ll create a transit gateway:

Press enter or click to view image in full size

Name your transit gateway:

Press enter or click to view image in full size

The transit gateway will be available after a time;

Press enter or click to view image in full size

Initiate the VPN Connection

We will be able to start the VPN connection using both the customer gateway and the transit gateway:

Press enter or click to view image in full size

Fill in the VPN details:

Press enter or click to view image in full size

Scroll down on the same page to add a random pre-shared key to the tunnels:

Press enter or click to view image in full size

Click the Create VPN connection button.

The VPN connection will be created, and AWS will generate two tunnels with two distinct Public IP addresses:

Press enter or click to view image in full size

Let’s examine the startup action:

Press enter or click to view image in full size

The tunnel’s startup action is Default, which indicates add. As stated at the start of the article, the AWS side will initiate the VPN connection. Therefore, the startup action need to be set to start.

(what’s the difference between auto=add, and auto=start?)

Let’s change the tunnel’s configuration:

Press enter or click to view image in full size

Select start:

The startup action will be changed:

Press enter or click to view image in full size

Redirect the traffic from the VPC to the VPN

I have an EC2 instance operating on a private subnet.

Public IP Address: N/A
Private IP Address: 172.16.8.157
Private Subnet: 172.16.8.0/24
Network: 172.16.0.0/20

I am going to create another subnet to be utilized for the NAT Gateway: 192.0.11.0/20

What? Your network is 172.16.0.0/20, but will you create a subnet with 192.0.x.x?

Yes, I will add another CIDR to my VPC:

Press enter or click to view image in full size

Add a new IPv4 CIDR:

Press enter or click to view image in full size

Add 192.0.0.0/20:

Press enter or click to view image in full size

Added:

Press enter or click to view image in full size

Why not 192.168.x.x? Because it is restricted by AWS:

https://docs.aws.amazon.com/vpc/latest/userguide/vpc-cidr-blocks.html

Create a VPN subnet

Press enter or click to view image in full size

Fill out the subnet form:

Press enter or click to view image in full size

Click Create subnet.

The subnet gets created:

Press enter or click to view image in full size

Let’s set up a NAT gateway utilizing this subnet:

Press enter or click to view image in full size

It will become available after a while:

Press enter or click to view image in full size

I will update the route table of the subnet of my EC2 instance;

Press enter or click to view image in full size

Route table:

Press enter or click to view image in full size

I will add the Digitalocean subnet (10.110.0.0/20) to the route table and select the NAT Gateway I created for the VPN:

Press enter or click to view image in full size

Added:

Press enter or click to view image in full size

Attach VPC To the Transit Gateway

We have not added the VPC to the transit gateway. However, when we choose Transit Gateway during VPN Creation, VPN is automatically added to the transit gateway.

Go to the Transit Gateway Attachments:

Add the VPC to the Transit Gateway:

Press enter or click to view image in full size

Added:

Press enter or click to view image in full size

Update Transit Gateway Route Table

Press enter or click to view image in full size

Redirect all traffic that arrives at the transit gateway to the VPN:

Press enter or click to view image in full size

Any traffic? I assumed just 10.110.0.0/20 would be forwarded to the VPN.

Correct. Please keep in mind that we routed the traffic (10.110.0.0/20) from our private subnet to Transit Gateway. So, any traffic arriving at TGW will be forwarded to the VPN.

So, in the future, if you want to access another subnet over VPN, you must change the route table in VPC. You do not need to modify TGW.

The route table was updated:

Press enter or click to view image in full size

The route state seems to be a black hole as the VPN connection has not been established.

Return to the VPN subnet page and redirect all VPN subnet traffic to TGW.

Go to the VPN subnet’s route table:

Press enter or click to view image in full size

Edit routes:

Press enter or click to view image in full size

Redirect all traffic arriving on the VPN subnet to Transit Gateway:

Press enter or click to view image in full size

The route table was updated:

Press enter or click to view image in full size

I will update the IPsec configuration in Digitalocean:

root@digitalocean-vpn:~# cat /etc/ipsec.conf
config setup
uniqueids = yes
conn aws-digitalocean-adil
type=tunnel
auto=add
authby=secret
leftsubnet=10.110.0.0/20
left=164.92.144.165
right=34.246.150.189
rightsubnet=192.0.11.0/24

And key;

root@digitalocean-vpn:~# cat /etc/ipsec.secrets
164.92.144.165 : PSK "add_some_long_string_here"

The VPN connection is UP:

Press enter or click to view image in full size

Route state is no longer a blackhole:

Press enter or click to view image in full size

I setup a Nginx server on the Digitalocean VM. I’ll attempt to connect it from EC2:

IP configuration and traceroute output from EC2:

Press enter or click to view image in full size

Remember that the IP address 192.0.11.111 is the IP address of NAT Gateway:

Press enter or click to view image in full size

--

--

No responses yet