Skip to content

Commit e2b9c83

Browse files
committed
Updated server security groups and instance user data
1 parent 19e094b commit e2b9c83

File tree

3 files changed

+97
-70
lines changed

3 files changed

+97
-70
lines changed

certs/main.tf

Lines changed: 87 additions & 55 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
{
@@ -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}"
@@ -150,7 +215,7 @@ module "cluster" {
150215
# LC parameters
151216
ami = "${var.ami}"
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ 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}"
99
}

certs/variables.tf

Lines changed: 8 additions & 13 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."
@@ -74,8 +63,14 @@ variable "s3_bucket_prefix" {
7463
default = ""
7564
}
7665

77-
variable "cidr_whitelist" {
66+
variable "vpn_whitelist" {
67+
type = "string"
68+
description = "Limit VPN access to the designated list of CIDRs"
69+
default = "0.0.0.0/0"
70+
}
71+
72+
variable "ssh_whitelist" {
7873
type = "string"
79-
description = "Limit access to the designated list of CIDRs"
74+
description = "Limit SSH access to the designated list of CIDRs"
8075
default = "0.0.0.0/0"
8176
}

0 commit comments

Comments
 (0)