This project automates the setup and teardown of an OpenVPN server on an AWS instance using Terraform and Bash scripts. The setup includes the creation of the necessary AWS infrastructure, SSH keys, and VPN access. After deployment, a client .ovpn file is generated for connecting to the VPN server.
- Terraform: Install Terraform from terraform.io, or use the .exe in the dir.
- AWS Account: Make sure you have valid AWS credentials configured, either in
~/.aws/credentialsor through environment variables. - SSH Key: A public SSH key file for accessing the instance. This should be specified in your Terraform configuration.
- OpenVPN Client: Any OpenVPN-compatible client to connect to the VPN using the generated
.ovpnfile.
main.tf: The main Terraform configuration file defining the resources (AWS instance, security groups, etc.) needed to set up the VPN server.variables.tf: Contains the configurable variables like AWS region, instance type, SSH key path, etc.do_thing.sh: A script to initialize and apply the Terraform configuration, retrieve the IP of the VPN server, and download the.ovpnclient configuration file.undo_thing.sh: A script to tear down the Terraform setup and clean up generated files.
The configuration can be customized by modifying the variables.tf file:
aws_region: The AWS region where the instance will be deployed.instance_type: The type of AWS instance to be used (e.g.,t2.micro).public_key_path: Path to your SSH public key file.- Other settings such as instance name tags can also be adjusted here.
Run do_thing.sh to initialize Terraform, deploy the instance, and retrieve the .ovpn file.
./do_thing.shThis script performs the following steps:
-
Initialize and Deploy with Terraform
- Initializes Terraform and applies the configuration.
- Waits for the instance to fully initialize (
sleep 65ensures the server is ready). - Reads the public IP of the VPN server from
vpn_ip.txt. - Downloads the generated
.ovpnfile to the local directory, which can be used with an OpenVPN client.
-
Connect to the VPN
- Once the
client1.ovpnfile is downloaded, you can use it with an OpenVPN client to connect to your VPN server.
Linux CLI:
- Once the
sudo openvpn --config client1.ovpnRun undo_thing.sh to destroy the Terraform-managed resources and clean up local files.
./undo_thing.sh- Run
terraform destroyto remove the deployed AWS instance and related resources. - Delete generated Terraform files:
.terraform.terraform.lock.hclclient.ovpnterraform.tfstateterraform.tfstate.backupvpn_ip.txt
- Ensure your AWS credentials are properly configured and have sufficient permissions.
- Verify that your SSH key path in
variables.tfis correct and accessible. - If the
.ovpnfile isn't generated or accessible, check the instance logs to ensure the OpenVPN installation completed successfully.
This project is licensed under the MIT License.