Skip to content

Commit 0ebaa6e

Browse files
authored
Add additional configuration options to Openvpn (#15)
* Add eip and extra route capability * Update formatting * Fix the tag filter
1 parent 266ef56 commit 0ebaa6e

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

certs/main.tf

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ resource "aws_iam_role_policy" "tags" {
6969
"Effect": "Allow",
7070
"Action": [
7171
"ec2:CreateTags",
72+
"ec2:DescribeTags",
7273
"ec2:AssociateAddress",
7374
"ec2:DescribeAddresses",
7475
"ec2:DescribeInstances"
@@ -187,16 +188,31 @@ resource "aws_security_group_rule" "cluster_allow_icmp_in" {
187188
security_group_id = "${module.cluster.sg_id}"
188189
}
189190

191+
resource "aws_eip" "openvpn_eip" {
192+
count = "${var.assign_eip == "true" ? 1 : 0}"
193+
vpc = true
194+
195+
tags {
196+
application = "${var.stack_item_fullname}"
197+
managed_by = "terraform"
198+
Name = "${var.stack_item_label}"
199+
}
200+
}
201+
190202
## Creates instance user data
191203
data "template_file" "user_data" {
192204
template = "${file("${path.module}/templates/user_data.tpl")}"
193205

194206
vars {
195-
hostname = "${var.stack_item_label}"
196-
s3_bucket = "${var.s3_bucket}"
197-
s3_bucket_prefix = "${var.s3_bucket_prefix}"
198-
route_cidrs = "${var.route_cidrs}"
199-
vpc_dns_ip = "${var.vpc_dns_ip}"
207+
additional_routes = "${var.additional_routes}"
208+
assign_eip = "${var.assign_eip}"
209+
hostname = "${var.stack_item_label}"
210+
s3_bucket = "${var.s3_bucket}"
211+
s3_bucket_prefix = "${var.s3_bucket_prefix}"
212+
stack_item_label = "${var.stack_item_label}"
213+
region = "${var.region}"
214+
route_cidrs = "${var.route_cidrs}"
215+
vpc_dns_ip = "${var.vpc_dns_ip}"
200216
}
201217
}
202218

certs/templates/user_data.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
## The sed and daemon-reload entries are temporary and will be removed once permission issue is handled on base AMI.
33
## https://github.com/WhistleLabs/terraform-aws-openvpn/pull/2
44
runcmd:
5+
- export INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id`
56
- echo "OPENVPN_CERT_SOURCE=s3://${replace(s3_bucket,"/(/)+$/","")}/${replace(s3_bucket_prefix,"/^(/)+|(/)+$/","")}" > /etc/openvpn/get-openvpn-certs.env
67
- echo "push \"dhcp-option DNS ${vpc_dns_ip}\"" >> /etc/openvpn/server.conf
78
- echo 'crl-verify /etc/openvpn/keys/crl.pem' >> /etc/openvpn/server.conf
@@ -10,10 +11,12 @@ runcmd:
1011
- echo "push \"route ${cidrhost(element(split(",",route_cidrs),2), 0)} ${cidrnetmask(element(split(",",route_cidrs),2))}\"" >> /etc/openvpn/server.conf
1112
- echo "push \"route ${cidrhost(element(split(",",route_cidrs),3), 0)} ${cidrnetmask(element(split(",",route_cidrs),3))}\"" >> /etc/openvpn/server.conf
1213
- echo "push \"route ${cidrhost(element(split(",",route_cidrs),4), 0)} ${cidrnetmask(element(split(",",route_cidrs),4))}\"" >> /etc/openvpn/server.conf
14+
- for route in `echo ${additional_routes} | tr ',' ' '`; do echo "push \"route $${route} 255.255.255.255\"" >> /etc/openvpn/server.conf;done
1315
- sed -i 's/\(ExecStartPost=.*chmod.*$\)/ExecStartPost=\/bin\/chown -R nobody:nogroup \/etc\/openvpn\n\1\n/g' /etc/systemd/system/get-openvpn-certs.service
1416
- systemctl daemon-reload
1517
- systemctl start get-openvpn-certs
1618
- systemctl restart openvpn@server
1719
- systemctl restart iptables
20+
- if [ ${assign_eip} = 'true' ]; then for eip in `aws ec2 describe-tags --region=${region} --filters "Name=resource-type,Values=elastic-ip" "Name=value,Values=${stack_item_label}" | jq -r '.Tags[].ResourceId'`; do if [ `aws ec2 describe-addresses --allocation-id $${eip} --region=${region} | jq -r '.Addresses[].InstanceId'` = 'null' ]; then echo "$${eip} is available, assigning it to current instance";aws ec2 associate-address --instance-id "$${INSTANCE_ID}" --allocation-id $${eip} --region=${region};else echo "$${eip} is taken";fi; done;fi
1821

1922
output : { all : '| tee -a /var/log/cloud-init-output.log' }

certs/variables.tf

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ variable "subnets" {
2828
}
2929

3030
## OpenVPN parameters
31+
variable "additional_routes" {
32+
type = "string"
33+
description = "Additional routes to add to Openvpn Config"
34+
default = ""
35+
}
36+
3137
variable "ami_custom" {
3238
type = "string"
3339
description = "Custom AMI to utilize"
@@ -39,11 +45,23 @@ variable "ami_region_lookup" {
3945
type = "map"
4046

4147
default = {
42-
us-east-1 = "ami-44e8913e"
43-
us-east-2 = "ami-d1c9e1b4"
48+
us-east-1 = "ami-44e8913e"
49+
us-east-2 = "ami-d1c9e1b4"
4450
}
4551
}
4652

53+
variable "assign_eip" {
54+
type = "string"
55+
description = "Boolean to determine if Elastic IP should be assigned to host"
56+
default = "false"
57+
}
58+
59+
variable "eip_tag" {
60+
type = "string"
61+
description = "Tag used to lookup Elastic IP to assign"
62+
default = ""
63+
}
64+
4765
variable "instance_type" {
4866
type = "string"
4967
description = "EC2 instance type to associate with the launch configuration."
@@ -90,4 +108,3 @@ variable "vpn_whitelist" {
90108
description = "Limit VPN access to the designated list of CIDRs"
91109
default = "0.0.0.0/0"
92110
}
93-

generate-certs/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ resource "aws_iam_role_policy" "tags" {
7070
"Effect": "Allow",
7171
"Action": [
7272
"ec2:CreateTags",
73+
"ec2:DescribeTags",
7374
"ec2:AssociateAddress",
7475
"ec2:DescribeAddresses",
7576
"ec2:DescribeInstances"

generate-certs/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ variable "ami_region_lookup" {
2323
type = "map"
2424

2525
default = {
26-
us-east-1 = "ami-6d65687b"
27-
us-east-2 = "ami-1dcbe878"
28-
custom = ""
26+
us-east-1 = "ami-6d65687b"
27+
us-east-2 = "ami-1dcbe878"
28+
custom = ""
2929
}
3030
}
3131

0 commit comments

Comments
 (0)