Skip to content

Commit f254353

Browse files
committed
Merge pull request #1 from unifio/wlc-update
Review & Update
2 parents e3e8fe0 + 72f155a commit f254353

File tree

12 files changed

+263
-128
lines changed

12 files changed

+263
-128
lines changed

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.3.0

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "http://rubygems.org"
2+
3+
gem "rake"

Gemfile.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
rake (11.1.2)
5+
6+
PLATFORMS
7+
ruby
8+
9+
DEPENDENCIES
10+
rake
11+
12+
BUNDLED WITH
13+
1.11.2

Rakefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'rake'
2+
3+
inputs = {
4+
'stack_item_label' => 'expl-tst',
5+
'stack_item_fullname' => 'Example Stack',
6+
'vpc_id' => 'vpc-xxxxxx',
7+
'region' => 'us-west-2',
8+
'subnets' => 'subnet-111111,subnet-222222',
9+
'ami' => 'ami-xxxxxx',
10+
'instance_type' => 't2.small',
11+
'key_name' => 'example',
12+
'route_cidrs' => '10.10.0.0/25,10.10.0.128/25,10.10.4.0/25,10.10.4.128/25',
13+
's3_bucket' => 'openvpn-certs',
14+
's3_bucket_prefix' => '20160603',
15+
'cidr_whitelist' => '0.0.0.0/0'
16+
}
17+
18+
task :default => :verify
19+
20+
desc "Verify the stack"
21+
task :verify do
22+
23+
vars = []
24+
inputs.each() do |var, value|
25+
vars.push("-var #{var}=\"#{value}\"")
26+
end
27+
28+
['openvpn'].each do |stack|
29+
task_args = {:stack => stack, :args => vars.join(' ')}
30+
Rake::Task['clean'].execute(Rake::TaskArguments.new(task_args.keys, task_args.values))
31+
Rake::Task['plan'].execute(Rake::TaskArguments.new(task_args.keys, task_args.values))
32+
end
33+
end
34+
35+
desc "Remove existing local state if present"
36+
task :clean, [:stack] do |t, args|
37+
sh "cd examples/#{args['stack']} && rm -fr .terraform *.tfstate*"
38+
end
39+
40+
desc "Create execution plan"
41+
task :plan, [:stack, :args] do |t, args|
42+
sh "cd examples/#{args['stack']} && terraform get && terraform plan -module-depth=-1 -input=false #{args['args']}"
43+
end

certs/main.tf

Lines changed: 50 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
# openvpn_server
2-
3-
provider "aws" {
4-
region = "${var.region}"
5-
}
6-
7-
/* ---------------------------- */
8-
/* IAM Role & Instance Profile */
9-
/* ---------------------------- */
1+
# OpenVPN Server
102

3+
## Creates IAM role & policies
114
resource "aws_iam_role" "vpn_role" {
12-
name = "${var.region}-${var.stack_item_label}-vpn"
5+
name = "${var.stack_item_label}-${var.region}"
136
path = "/"
147

158
assume_role_policy = <<EOF
@@ -42,8 +35,8 @@ resource "aws_iam_role_policy" "s3_vpn_ro" {
4235
"s3:Get*"
4336
],
4437
"Resource": [
45-
"arn:aws:s3:::${var.s3_path}",
46-
"arn:aws:s3:::${var.s3_path}/*"
38+
"arn:aws:s3:::${var.s3_bucket}/${var.s3_bucket_prefix}",
39+
"arn:aws:s3:::${var.s3_bucket}/${var.s3_bucket_prefix}/*"
4740
]
4841
},
4942
{
@@ -83,77 +76,56 @@ resource "aws_iam_role_policy" "tags" {
8376
EOF
8477
}
8578

79+
## Creates IAM instance profile
8680
resource "aws_iam_instance_profile" "vpn_profile" {
87-
name = "${var.region}-${var.stack_item_label}-vpn"
81+
name = "${var.stack_item_label}-${var.region}"
8882
roles = ["${aws_iam_role.vpn_role.name}"]
8983
}
9084

91-
/* ---------------------------- */
92-
/* Security Group */
93-
/* ---------------------------- */
85+
## Creates security group rules
9486
resource "aws_security_group_rule" "allow_all_out" {
95-
type = "egress"
96-
from_port = 0
97-
to_port = 0
98-
protocol = "-1"
99-
cidr_blocks = ["0.0.0.0/0"]
100-
101-
security_group_id = "${module.asg.sg_id}"
87+
type = "egress"
88+
from_port = 0
89+
to_port = 0
90+
protocol = "-1"
91+
cidr_blocks = ["0.0.0.0/0"]
92+
security_group_id = "${module.cluster.sg_id}"
10293
}
10394

10495
resource "aws_security_group_rule" "allow_ssh_in_tcp" {
105-
type = "ingress"
106-
from_port = 22
107-
to_port = 22
108-
protocol = "tcp"
109-
cidr_blocks = ["${split(",",var.cidr_whitelist)}"]
110-
111-
security_group_id = "${module.asg.sg_id}"
112-
}
113-
114-
resource "aws_security_group_rule" "allow_openvpn_in_tdp" {
115-
type = "ingress"
116-
from_port = 1194
117-
to_port = 1194
118-
protocol = "tcp"
119-
cidr_blocks = ["${split(",",var.cidr_whitelist)}"]
120-
121-
security_group_id = "${module.asg.sg_id}"
96+
type = "ingress"
97+
from_port = 22
98+
to_port = 22
99+
protocol = "tcp"
100+
cidr_blocks = ["${split(",",var.cidr_whitelist)}"]
101+
security_group_id = "${module.cluster.sg_id}"
122102
}
123103

124-
resource "aws_security_group_rule" "allow_ping_request_icmp" {
125-
type = "ingress"
126-
from_port = 8
127-
to_port = 0
128-
protocol = "icmp"
129-
cidr_blocks = ["0.0.0.0/0"]
130-
131-
security_group_id = "${module.asg.sg_id}"
104+
resource "aws_security_group_rule" "allow_openvpn_in_tcp" {
105+
type = "ingress"
106+
from_port = 1194
107+
to_port = 1194
108+
protocol = "tcp"
109+
cidr_blocks = ["${split(",",var.cidr_whitelist)}"]
110+
security_group_id = "${module.cluster.sg_id}"
132111
}
133112

134-
resource "aws_security_group_rule" "allow_ping_reply_icmp" {
135-
type = "ingress"
136-
from_port = 0
137-
to_port = 0
138-
protocol = "icmp"
139-
cidr_blocks = ["0.0.0.0/0"]
140-
141-
security_group_id = "${module.asg.sg_id}"
113+
resource "aws_security_group_rule" "allow_ping_in_icmp" {
114+
type = "ingress"
115+
from_port = 0
116+
to_port = 0
117+
protocol = "icmp"
118+
cidr_blocks = ["0.0.0.0/0"]
119+
security_group_id = "${module.cluster.sg_id}"
142120
}
143121

144-
/* ---------------------------- */
145-
/* User Data */
146-
/* ---------------------------- */
122+
## Creates instance user data
147123
resource "template_file" "user_data" {
148124
template = "${file("${path.module}/templates/user_data.tpl")}"
149125

150126
vars {
151-
instance_number = "${count.index}"
152-
hostname = "${var.role}-${count.index}"
153-
region = "${var.region}"
154-
stack_item_label = "${var.stack_item_label}"
155-
role = "${var.role}"
156-
s3_path = "${var.s3_path}"
127+
s3_bucket = "${var.s3_bucket}"
128+
s3_bucket_prefix = "${var.s3_bucket_prefix}"
157129
route_cidrs = "${var.route_cidrs}"
158130
}
159131

@@ -162,12 +134,13 @@ resource "template_file" "user_data" {
162134
}
163135
}
164136

165-
module "asg" {
137+
## Creates auto scaling cluster
138+
module "cluster" {
166139
source = "github.com/unifio/terraform-aws-asg?ref=v0.2.0//group"
167140

168141
# Resource tags
169-
stack_item_label = "${var.stack_item_label}-vpn-asg"
170-
stack_item_fullname = "${var.stack_item_fullname}-vpn"
142+
stack_item_label = "${var.stack_item_label}"
143+
stack_item_fullname = "${var.stack_item_fullname}"
171144

172145
# VPC parameters
173146
vpc_id = "${var.vpc_id}"
@@ -186,17 +159,16 @@ module "asg" {
186159
max_size = 2
187160
min_size = 1
188161
hc_grace_period = 300
189-
hc_check_type = "EC2"
190162
min_elb_capacity = 1
191163
load_balancers = "${aws_elb.elb.id}"
192164
}
193165

194-
# Create a new load balancer
166+
## Creates a load balancer
195167
resource "aws_elb" "elb" {
196-
name = "${var.stack_item_label}-vpn-elb"
168+
name = "${var.stack_item_label}"
197169
subnets = ["${split(",",var.subnets)}"]
198170
internal = false
199-
security_groups = ["${module.asg.sg_id}"]
171+
security_groups = ["${module.cluster.sg_id}"]
200172

201173
listener {
202174
instance_port = 1194
@@ -214,11 +186,12 @@ resource "aws_elb" "elb" {
214186
}
215187

216188
tags {
217-
Name = "${var.stack_item_label}-vpn-elb"
218-
application = "${var.stack_item_label}-vpn"
189+
Name = "${var.stack_item_label}"
190+
application = "${var.stack_item_fullname}"
219191
managed_by = "terraform"
220192
}
221-
}
222-
223-
# Create a Route53 record
224193

194+
lifecycle {
195+
create_before_destroy = true
196+
}
197+
}

certs/outputs.tf

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
# openvpn_server - Output Variables
1+
# Outputs
22

3-
/* ---------------------------- */
4-
## VPN server security group ID */
5-
/* ---------------------------- */
63
output "vpn_server_sg_id" {
7-
value = "${module.asg.sg_id}"
4+
value = "${module.cluster.sg_id}"
85
}
96

107
output "cidr_whitelist" {

certs/templates/user_data.tpl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
#cloud-config
2-
environment:
3-
stack_item_label: ${stack_item_label}
4-
hostname: ${hostname}
5-
aws_region: ${region}
6-
instance_role: ${role}
7-
instance_number: ${instance_number}
82
runcmd:
9-
- echo "OPENVPN_CERT_SOURCE=s3://${s3_path}" > /etc/openvpn/get-openvpn-certs.env
3+
- echo "OPENVPN_CERT_SOURCE=s3://${s3_bucket}/${s3_bucket_prefix}" > /etc/openvpn/get-openvpn-certs.env
104
- 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
115
- echo "push \"route ${cidrhost(element(split(",",route_cidrs),1), 0)} ${cidrnetmask(element(split(",",route_cidrs),1))}\"" >> /etc/openvpn/server.conf
126
- echo "push \"route ${cidrhost(element(split(",",route_cidrs),2), 0)} ${cidrnetmask(element(split(",",route_cidrs),2))}\"" >> /etc/openvpn/server.conf

0 commit comments

Comments
 (0)