Skip to content

Commit b5f7f74

Browse files
authored
Merge pull request #4 from unifio/wc-review
Misc. Updates
2 parents 19e094b + f401d2e commit b5f7f74

File tree

12 files changed

+318
-216
lines changed

12 files changed

+318
-216
lines changed

CHANGELOG.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
# CHANGELOG
1+
## Unreleased
22

3-
### ???
3+
## ???
44

55
- Feature: Automatically push instance's subnet route into `server.conf`
66
- export `zone_id`, `dns_name` from aws_elb
77
- Fix the 4 subnet fixed mapping
88
- Fill in some examples
99

10-
### 0.0.5
10+
## 0.0.6
11+
12+
#### BREAKING CHANGES:
13+
- Updates in resource naming will cause churn for existing resources.
14+
- Updated certificate generator to require VPC deployment
15+
16+
#### IMPROVEMENTS:
17+
- Standardization with other Unif.io OSS terraform modules
18+
- Documentation improvements
19+
- Updated security group scheme for OpenVPN server
20+
- Added pre-built AMI lookup to the server module
21+
22+
## 0.0.5
23+
24+
#### FEATURES:
1125
- Initial release of `generate-certs` module
1226

13-
### 0.0.4
27+
## 0.0.4
28+
29+
#### IMPROVEMENTS:
1430
- Standardization with other Unif.io OSS terraform modules
1531
- CI Builder
1632
- Small tweaks:
@@ -20,15 +36,17 @@
2036
- lifecycle `create_before_destroy` fixes to deal with dependency issues on build.
2137
- somewhat breaking change: in_vpc now is `1`(true) instead of `0`(false)
2238

23-
### 0.0.3
39+
## 0.0.3
2440

41+
#### IMPROVEMENTS:
2542
- Fix: tag.application for elb reverted to using short name instead of full application name due to naming restrictions
2643

27-
### 0.0.2
44+
## 0.0.2
2845

46+
#### IMPROVEMENTS:
2947
- Fix: use updated `awscli` client from pip instead of apt
3048

31-
### 0.0.1
49+
## 0.0.1
3250

51+
#### FEATURES:
3352
- Basic functioning openvpn server working off us-east-1
34-

Rakefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ inputs = {
44
'stack_item_label' => 'expl-tst',
55
'stack_item_fullname' => 'Example Stack',
66
'vpc_id' => 'vpc-xxxxxx',
7-
'region' => 'us-west-2',
7+
'region' => 'us-east-1',
88
'subnets' => 'subnet-111111,subnet-222222',
9-
'ami' => 'ami-xxxxxx',
109
'instance_type' => 't2.small',
1110
'key_name' => 'example',
1211
'route_cidrs' => '10.10.0.0/25,10.10.0.128/25,10.10.4.0/25,10.10.4.128/25',
1312
's3_bucket' => 'openvpn-certs',
1413
's3_bucket_prefix' => '20160603',
15-
'cidr_whitelist' => '0.0.0.0/0'
1614
}
1715

1816
task :default => :verify

certs/main.tf

Lines changed: 91 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# OpenVPN Server
22

33
## Creates IAM role & policies
4-
resource "aws_iam_role" "vpn_role" {
4+
resource "aws_iam_role" "role" {
55
name = "${var.stack_item_label}-${var.region}"
66
path = "/"
77

@@ -21,9 +21,9 @@ resource "aws_iam_role" "vpn_role" {
2121
EOF
2222
}
2323

24-
resource "aws_iam_role_policy" "s3_vpn_ro" {
25-
name = "s3_vpn_ro"
26-
role = "${aws_iam_role.vpn_role.id}"
24+
resource "aws_iam_role_policy" "s3_certs_ro" {
25+
name = "s3_certs_ro"
26+
role = "${aws_iam_role.role.id}"
2727

2828
policy = <<EOF
2929
{
@@ -35,8 +35,8 @@ resource "aws_iam_role_policy" "s3_vpn_ro" {
3535
"s3:Get*"
3636
],
3737
"Resource": [
38-
"arn:aws:s3:::${var.s3_bucket}/${var.s3_bucket_prefix}",
39-
"arn:aws:s3:::${var.s3_bucket}/${var.s3_bucket_prefix}/*"
38+
"arn:aws:s3:::${replace(var.s3_bucket,"/(\/)+$/","")}/${replace(var.s3_bucket_prefix,"/^(\/)+|(\/)+$/","")}",
39+
"arn:aws:s3:::${replace(var.s3_bucket,"/(\/)+$/","")}/${replace(var.s3_bucket_prefix,"/^(\/)+|(\/)+$/","")}/*"
4040
]
4141
},
4242
{
@@ -45,7 +45,7 @@ resource "aws_iam_role_policy" "s3_vpn_ro" {
4545
"s3:List*"
4646
],
4747
"Resource": [
48-
"arn:aws:s3:::${var.s3_bucket}"
48+
"arn:aws:s3:::${replace(var.s3_bucket,"/(\/)+$/","")}"
4949
]
5050
}
5151
]
@@ -54,8 +54,8 @@ EOF
5454
}
5555

5656
resource "aws_iam_role_policy" "tags" {
57-
name = "vpn-tags"
58-
role = "${aws_iam_role.vpn_role.id}"
57+
name = "tags"
58+
role = "${aws_iam_role.role.id}"
5959

6060
policy = <<EOF
6161
{
@@ -77,45 +77,109 @@ EOF
7777
}
7878

7979
## Creates IAM instance profile
80-
resource "aws_iam_instance_profile" "vpn_profile" {
80+
resource "aws_iam_instance_profile" "profile" {
8181
name = "${var.stack_item_label}-${var.region}"
82-
roles = ["${aws_iam_role.vpn_role.name}"]
82+
roles = ["${aws_iam_role.role.name}"]
8383
}
8484

85-
## Creates security group rules
86-
resource "aws_security_group_rule" "allow_all_out" {
85+
## Create elastic load balancer security group and rules
86+
resource "aws_security_group" "sg_elb" {
87+
name_prefix = "${var.stack_item_label}-elb-"
88+
description = "${var.stack_item_fullname} load balancer security group"
89+
vpc_id = "${var.vpc_id}"
90+
91+
tags {
92+
Name = "${var.stack_item_label}-elb"
93+
application = "${var.stack_item_fullname}"
94+
managed_by = "terraform"
95+
}
96+
}
97+
98+
resource "aws_security_group_rule" "elb_allow_all_out" {
8799
type = "egress"
88100
from_port = 0
89101
to_port = 0
90102
protocol = "-1"
91103
cidr_blocks = ["0.0.0.0/0"]
92-
security_group_id = "${module.cluster.sg_id}"
104+
security_group_id = "${aws_security_group.sg_elb.id}"
93105
}
94106

95-
resource "aws_security_group_rule" "allow_ssh_in_tcp" {
107+
resource "aws_security_group_rule" "elb_allow_openvpn_tcp_in" {
96108
type = "ingress"
97-
from_port = 22
98-
to_port = 22
109+
from_port = 1194
110+
to_port = 1194
99111
protocol = "tcp"
100-
cidr_blocks = ["${split(",",var.cidr_whitelist)}"]
112+
cidr_blocks = ["${split(",",var.vpn_whitelist)}"]
113+
security_group_id = "${aws_security_group.sg_elb.id}"
114+
}
115+
116+
## Creates an elastic load balancer
117+
resource "aws_elb" "elb" {
118+
name = "${var.stack_item_label}"
119+
subnets = ["${split(",",var.subnets)}"]
120+
internal = false
121+
security_groups = ["${aws_security_group.sg_elb.id}"]
122+
123+
listener {
124+
instance_port = 1194
125+
instance_protocol = "tcp"
126+
lb_port = 1194
127+
lb_protocol = "tcp"
128+
}
129+
130+
health_check {
131+
healthy_threshold = 4
132+
unhealthy_threshold = 2
133+
timeout = 3
134+
target = "TCP:1194"
135+
interval = 30
136+
}
137+
138+
tags {
139+
Name = "${var.stack_item_label}"
140+
application = "${var.stack_item_fullname}"
141+
managed_by = "terraform"
142+
}
143+
144+
lifecycle {
145+
create_before_destroy = true
146+
}
147+
}
148+
149+
## Creates security group rules
150+
resource "aws_security_group_rule" "cluster_allow_all_out" {
151+
type = "egress"
152+
from_port = 0
153+
to_port = 0
154+
protocol = "-1"
155+
cidr_blocks = ["0.0.0.0/0"]
101156
security_group_id = "${module.cluster.sg_id}"
102157
}
103158

104-
resource "aws_security_group_rule" "allow_openvpn_in_tcp" {
159+
resource "aws_security_group_rule" "cluster_allow_openvpn_tcp_in" {
160+
type = "ingress"
161+
from_port = 1194
162+
to_port = 1194
163+
protocol = "tcp"
164+
source_security_group_id = "${aws_security_group.sg_elb.id}"
165+
security_group_id = "${module.cluster.sg_id}"
166+
}
167+
168+
resource "aws_security_group_rule" "cluster_allow_ssh_in" {
105169
type = "ingress"
106-
from_port = 1194
107-
to_port = 1194
170+
from_port = 22
171+
to_port = 22
108172
protocol = "tcp"
109-
cidr_blocks = ["${split(",",var.cidr_whitelist)}"]
173+
cidr_blocks = ["${split(",",var.ssh_whitelist)}"]
110174
security_group_id = "${module.cluster.sg_id}"
111175
}
112176

113-
resource "aws_security_group_rule" "allow_ping_in_icmp" {
177+
resource "aws_security_group_rule" "cluster_allow_icmp_in" {
114178
type = "ingress"
115179
from_port = 0
116180
to_port = 0
117181
protocol = "icmp"
118-
cidr_blocks = ["0.0.0.0/0"]
182+
cidr_blocks = ["${split(",",var.ssh_whitelist)}"]
119183
security_group_id = "${module.cluster.sg_id}"
120184
}
121185

@@ -124,6 +188,7 @@ resource "template_file" "user_data" {
124188
template = "${file("${path.module}/templates/user_data.tpl")}"
125189

126190
vars {
191+
hostname = "${var.stack_item_label}"
127192
s3_bucket = "${var.s3_bucket}"
128193
s3_bucket_prefix = "${var.s3_bucket_prefix}"
129194
route_cidrs = "${var.route_cidrs}"
@@ -148,9 +213,9 @@ module "cluster" {
148213
region = "${var.region}"
149214

150215
# LC parameters
151-
ami = "${var.ami}"
216+
ami = "${coalesce(lookup(var.ami_region_lookup, var.region), var.ami_custom)}"
152217
instance_type = "${var.instance_type}"
153-
instance_profile = "${aws_iam_instance_profile.vpn_profile.id}"
218+
instance_profile = "${aws_iam_instance_profile.profile.id}"
154219
user_data = "${template_file.user_data.rendered}"
155220
key_name = "${var.key_name}"
156221
ebs_optimized = false
@@ -162,36 +227,3 @@ module "cluster" {
162227
min_elb_capacity = 1
163228
load_balancers = "${aws_elb.elb.id}"
164229
}
165-
166-
## Creates a load balancer
167-
resource "aws_elb" "elb" {
168-
name = "${var.stack_item_label}"
169-
subnets = ["${split(",",var.subnets)}"]
170-
internal = false
171-
security_groups = ["${module.cluster.sg_id}"]
172-
173-
listener {
174-
instance_port = 1194
175-
instance_protocol = "tcp"
176-
lb_port = 1194
177-
lb_protocol = "tcp"
178-
}
179-
180-
health_check {
181-
healthy_threshold = 2
182-
unhealthy_threshold = 2
183-
timeout = 3
184-
target = "TCP:1194"
185-
interval = 30
186-
}
187-
188-
tags {
189-
Name = "${var.stack_item_label}"
190-
application = "${var.stack_item_fullname}"
191-
managed_by = "terraform"
192-
}
193-
194-
lifecycle {
195-
create_before_destroy = true
196-
}
197-
}

certs/outputs.tf

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ output "vpn_server_sg_id" {
44
value = "${module.cluster.sg_id}"
55
}
66

7-
output "cidr_whitelist" {
8-
value = "${var.cidr_whitelist}"
7+
output "vpn_whitelist" {
8+
value = "${var.vpn_whitelist}"
9+
}
10+
11+
output "vpn_elb_dns_name" {
12+
value = "${aws_elb.elb.dns_name}"
13+
}
14+
15+
output "vpn_elb_zone_id" {
16+
value = "${aws_elb.elb.zone_id}"
917
}

certs/templates/user_data.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#cloud-config
22
runcmd:
3-
- echo "OPENVPN_CERT_SOURCE=s3://${s3_bucket}/${s3_bucket_prefix}" > /etc/openvpn/get-openvpn-certs.env
3+
- echo "OPENVPN_CERT_SOURCE=s3://${replace(s3_bucket,"/(\/)+$/","")}/${replace(s3_bucket_prefix,"/^(\/)+|(\/)+$/","")}" > /etc/openvpn/get-openvpn-certs.env
44
- echo "push \"route $(ip route get 8.8.8.8| grep src| sed 's/.*src \(.*\)$/\1/g') 255.255.255.255 net_gateway\"" >> /etc/openvpn/server.conf
55
- echo "push \"route ${cidrhost(element(split(",",route_cidrs),1), 0)} ${cidrnetmask(element(split(",",route_cidrs),1))}\"" >> /etc/openvpn/server.conf
66
- echo "push \"route ${cidrhost(element(split(",",route_cidrs),2), 0)} ${cidrnetmask(element(split(",",route_cidrs),2))}\"" >> /etc/openvpn/server.conf

certs/variables.tf

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ variable "stack_item_fullname" {
1212
}
1313

1414
## VPC parameters
15-
16-
###
17-
### 0 - if instance is a standalone instance outside a VPC
18-
### 1 - if instance is in a vpc
19-
###
20-
variable "in_vpc" {
21-
type = "string"
22-
description = "Flag for associating the cluster with a VPC."
23-
default = 1
24-
}
25-
2615
variable "vpc_id" {
2716
type = "string"
2817
description = "ID of the target VPC."
@@ -39,9 +28,21 @@ variable "subnets" {
3928
}
4029

4130
## OpenVPN parameters
42-
variable "ami" {
31+
variable "ami_custom" {
4332
type = "string"
44-
description = "Amazon Machine Image (AMI) to associate with the launch configuration."
33+
description = "Custom AMI to utilize"
34+
default = ""
35+
}
36+
37+
variable "ami_region_lookup" {
38+
# Not meant to be overwritten
39+
type = "map"
40+
41+
default = {
42+
us-east-1 = "ami-d66995bb"
43+
ap-northeast-1 = "ami-4803ec29"
44+
custom = ""
45+
}
4546
}
4647

4748
variable "instance_type" {
@@ -74,8 +75,14 @@ variable "s3_bucket_prefix" {
7475
default = ""
7576
}
7677

77-
variable "cidr_whitelist" {
78+
variable "vpn_whitelist" {
79+
type = "string"
80+
description = "Limit VPN access to the designated list of CIDRs"
81+
default = "0.0.0.0/0"
82+
}
83+
84+
variable "ssh_whitelist" {
7885
type = "string"
79-
description = "Limit access to the designated list of CIDRs"
86+
description = "Limit SSH access to the designated list of CIDRs"
8087
default = "0.0.0.0/0"
8188
}

0 commit comments

Comments
 (0)