AWS

How to Setup VPN on EC2 Instance Without Losing Connection

Virtual Private Network is used to not connect your virtual machine to the internet for various reasons. The user can connect through a private network to secure its multiple virtual machines. All the machines will communicate and get access to each other’s resources using the VPN network. The AWS EC2 instances can be connected through the VPN setup.

Why do EC2 VMs Lose Their Connection During a VPN Setup?

While setting up a VPN connection in the EC2 virtual, it tends to lose the connection. The public IP address through which the VPN is set up is unknown to the user, and the original address is also changed. To avoid this issue, this guide helps the user to set up the connection using the VPC and subnet connection to have knowledge of the IP address for the VPN.

Let’s start setting up the AWS VPN for the Window instance.

Setup AWS VPN for Windows Machine

To set up VPN for the AWS windows machine, click on the “Create VPC” button from the platform:


Provide the name of the VPC with IPv4 CIDR:


Scroll down the page to click on the “Create VPC” button:


Head into the Subnets page from the left panel and click on the “Create subnet” button:


Link the subnet with the VPC and name it:


Scroll down the page to enter the “IPv4 CIDR block” and click on the “Create subnet” button:


Create another subnet with the name of private subnet and linked to the VPC:


Provide the IPv4 CIDR block and click on the “Create subnet” button:


Head into the Internet gateways page and click on the “Create internet gateway” button to create a gateway and attach it to the VPC:


On this page, type the name of the gateway and click on the “Create internet gateway” button to complete its creation:


Once the gateway is created, attach it to the VPC by clicking on the “Attach to a VPC” button:


Choose the VPC id from the available VPCs to communicate with the internet:


Head into the Route tables page and click on the “Create route table” button from the route table page on the platform:


Configure the routing table using the VPC and click on the “Create route table” button:


Select the Routes section of the routing table page, click on the “Edit routes” button:


Add a route on this page to use the internet gateway as the target and then click on the “Save changes” button:


After that, click on the “Edit subnet associations” button from the subnet association section:


Select the checkbox of the public subnet to select it from this page and then save it:


After that, Head onto the EC2 service console page to launch the instance from the platform:


Name the instance and Browse more AMIs by clicking on its link:


Search for the OpenVPN from the marketplace and click on the “Select” button:


After that, select the instance type and key pair file:


Configure the network settings and click on the “Launch instance” button:


Launch another EC2 instance from the console page:


Select Windows as its AMI and type the name of the instance:


Select the key pair file and instance type:


Launch the instance using the private subnet and reviewing the settings from Summary:


Connect the VPC machine from the platform:


Use the following command to connect to the instance:

ssh -i "C:\Users\Lenovo\Documents\try.pem" openvpnas@54.255.18.13

 
Following is the syntax of the above command:

ssh -i "path of the key pair file" openvpnas@Public IP

 
The above command will connect the EC2 instance:


Type “yes” to continue with configuration and then hit enter multiple time to keep the default settings:


Use the following command to set the password:

sudo passwd openvpn

 
The above command will prompt the user to enter the password for it to be updated:


Copy the URL mentioned in the above screenshot and paste it on the web browser:


Provide the login credentials to access the platform:


Download the client OpenVPN for windows:


Connect to the OpenVPN by providing the Username and Password:


After that, connect to the instance with windows as Machine Image:


Download the remote desktop file and click on the “Get Password” button:


Upload the private key pair file to get the password:


Connect to the Remote Desktop file by providing the password provided by the platform:


The user is connected to the Windows Virtual machine using the VPN connection:


You have successfully set up the AWS VPN for the EC2 windows virtual machine.

Conclusion

To set up the AWS VPN for Windows virtual machines, create VPC, Subnets, Route tables, and Internet gateway. Once these services are running, head into the EC2 console to create a couple of instances using “OpenVPN” and “Windows” as the AMIs. After that, connect to a virtual machine with an OpenVPN image to download the VPN client and set the login credentials. Connect to the Windows instance and the AWS VPN is set up in the EC2 instance.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.