I am trying to set OpenVPN using Terraform but did not find any way to do that. I would like to use terraform to deploy OpenVPN ec2 server. How do I get the OpenVPN AMI id to place on terraform? Or Do I have to deploy a traditional ec2 ubuntu server and install OpenVPN manually?
Asked
Active
Viewed 2,698 times
1
-
you can review the code here https://github.com/DNXLabs/terraform-aws-openvpn for some help. This is not my work. – NFR Jul 21 '20 at 01:28
1 Answers
1
You can use traditional Ec2 instance and install OpenVPN from user data.
You can look into this Github repository
It's also possible to run the script headless, e.g. without waiting for user input, in an automated manner.
Example usage:
AUTO_INSTALL=y ./openvpn-install.sh
# or
export AUTO_INSTALL=y
./openvpn-install.sh
When OpenVPN is installed, you can run the script again, and you will get the choice to:
Add a client
Remove a client
Uninstall OpenVPN
Here is Terraform code that you can try
resource "aws_instance" "openvpn" {
ami = "ami-0d2f82a622136a696" //us-west-2
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.vpn.id}"]
associate_public_ip_address = true
subnet_id = "${aws_subnet.PubSubnet2a.id}"
iam_instance_profile = "${aws_iam_instance_profile.aws-vpn-profile.name}"
user_data = "${data.template_file.vpn.rendered}"
tags = {
Name = "${var.env_prefix_name}-vpn"
}
}
data "template_file" "vpn" {
template = "${file("bash_scripts/vpnuserdata.sh")}"
vars = {
region_name = "${var.region}"
}
}
vpnuserdata.sh
#!/usr/bin/env bash
set -x
/usr/bin/yum update -y
curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh
chmod +x openvpn-install.sh
export AUTO_INSTALL=y
./openvpn-install.sh

Adiii
- 54,482
- 7
- 145
- 148
-
Hi. Just quick question. `ami-0d2f82a622136a696` would be for which region? – Marcin Jul 21 '20 at 00:55
-
1Hi, @Marcin this is for us-west-2, which should be base on some variable to make it working with other regions. – Adiii Jul 21 '20 at 00:58