|
| 1 | +# openvpn_server |
| 2 | + |
| 3 | +provider "aws" { |
| 4 | + region = "${var.region}" |
| 5 | +} |
| 6 | + |
| 7 | +/* ---------------------------- */ |
| 8 | +/* IAM Role & Instance Profile */ |
| 9 | +/* ---------------------------- */ |
| 10 | + |
| 11 | +resource "aws_iam_role" "vpn_role" { |
| 12 | + name = "${var.region}-${var.stack_item_label}-vpn" |
| 13 | + path = "/" |
| 14 | + |
| 15 | + assume_role_policy = <<EOF |
| 16 | +{ |
| 17 | + "Version": "2012-10-17", |
| 18 | + "Statement": [ |
| 19 | + { |
| 20 | + "Effect": "Allow", |
| 21 | + "Action": "sts:AssumeRole", |
| 22 | + "Principal": { |
| 23 | + "Service": "ec2.amazonaws.com" |
| 24 | + } |
| 25 | + } |
| 26 | + ] |
| 27 | +} |
| 28 | +EOF |
| 29 | +} |
| 30 | + |
| 31 | +resource "aws_iam_role_policy" "s3_vpn" { |
| 32 | + name = "s3_vpn" |
| 33 | + role = "${aws_iam_role.vpn_role.id}" |
| 34 | + |
| 35 | + policy = <<EOF |
| 36 | +{ |
| 37 | + "Version": "2012-10-17", |
| 38 | + "Statement": [ |
| 39 | + { |
| 40 | + "Effect": "Allow", |
| 41 | + "Action": [ |
| 42 | + "s3:Get*" |
| 43 | + ], |
| 44 | + "Resource": [ |
| 45 | + "arn:aws:s3:::${var.s3_path}", |
| 46 | + "arn:aws:s3:::${var.s3_path}/*" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "Effect": "Allow", |
| 51 | + "Action": [ |
| 52 | + "s3:List*" |
| 53 | + ], |
| 54 | + "Resource": [ |
| 55 | + "arn:aws:s3:::${var.s3_bucket}" |
| 56 | + ] |
| 57 | + } |
| 58 | + ] |
| 59 | +} |
| 60 | +EOF |
| 61 | +} |
| 62 | + |
| 63 | +resource "aws_iam_role_policy" "tags" { |
| 64 | + name = "tags" |
| 65 | + role = "${aws_iam_role.vpn_role.id}" |
| 66 | + |
| 67 | + policy = <<EOF |
| 68 | +{ |
| 69 | + "Version": "2012-10-17", |
| 70 | + "Statement": [ |
| 71 | + { |
| 72 | + "Effect": "Allow", |
| 73 | + "Action": [ |
| 74 | + "ec2:CreateTags", |
| 75 | + "ec2:AssociateAddress", |
| 76 | + "ec2:DescribeAddresses", |
| 77 | + "ec2:DescribeInstances" |
| 78 | + ], |
| 79 | + "Resource": "*" |
| 80 | + } |
| 81 | + ] |
| 82 | +} |
| 83 | +EOF |
| 84 | +} |
| 85 | + |
| 86 | +resource "aws_iam_instance_profile" "vpn_profile" { |
| 87 | + name = "${var.region}-${var.stack_item_label}-vpn" |
| 88 | + roles = ["${aws_iam_role.vpn_role.name}"] |
| 89 | +} |
| 90 | + |
| 91 | +/* ---------------------------- */ |
| 92 | +/* Security Group */ |
| 93 | +/* ---------------------------- */ |
| 94 | +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}" |
| 102 | +} |
| 103 | + |
| 104 | +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}" |
| 122 | +} |
| 123 | + |
| 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}" |
| 132 | +} |
| 133 | + |
| 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}" |
| 142 | +} |
| 143 | + |
| 144 | +/* ---------------------------- */ |
| 145 | +/* User Data */ |
| 146 | +/* ---------------------------- */ |
| 147 | +resource "template_file" "user_data" { |
| 148 | + template = "${file("${path.module}/templates/user_data.tpl")}" |
| 149 | + |
| 150 | + 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}" |
| 157 | + route_cidrs = "${var.route_cidrs}" |
| 158 | + } |
| 159 | + |
| 160 | + lifecycle { |
| 161 | + create_before_destroy = true |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +module "asg" { |
| 166 | + source = "github.com/unifio/terraform-aws-asg?ref=v0.2.0//group" |
| 167 | + |
| 168 | + # Resource tags |
| 169 | + stack_item_label = "${var.stack_item_label}-vpn-asg" |
| 170 | + stack_item_fullname = "${var.stack_item_fullname}-vpn" |
| 171 | + |
| 172 | + # VPC parameters |
| 173 | + vpc_id = "${var.vpc_id}" |
| 174 | + subnets = "${var.subnets}" |
| 175 | + region = "${var.region}" |
| 176 | + |
| 177 | + # LC parameters |
| 178 | + ami = "${var.ami}" |
| 179 | + instance_type = "${var.instance_type}" |
| 180 | + instance_profile = "${aws_iam_instance_profile.vpn_profile.id}" |
| 181 | + user_data = "${template_file.user_data.rendered}" |
| 182 | + key_name = "${var.key_name}" |
| 183 | + ebs_optimized = false |
| 184 | + |
| 185 | + # ASG parameters |
| 186 | + max_size = 2 |
| 187 | + min_size = 1 |
| 188 | + hc_grace_period = 300 |
| 189 | + hc_check_type = "EC2" |
| 190 | + min_elb_capacity = 1 |
| 191 | + load_balancers = "${aws_elb.elb.id}" |
| 192 | +} |
| 193 | + |
| 194 | +# Create a new load balancer |
| 195 | +resource "aws_elb" "elb" { |
| 196 | + name = "${var.stack_item_label}-vpn-elb" |
| 197 | + subnets = ["${split(",",var.subnets)}"] |
| 198 | + internal = false |
| 199 | + security_groups = ["${module.asg.sg_id}"] |
| 200 | + |
| 201 | + listener { |
| 202 | + instance_port = 1194 |
| 203 | + instance_protocol = "tcp" |
| 204 | + lb_port = 1194 |
| 205 | + lb_protocol = "tcp" |
| 206 | + } |
| 207 | + |
| 208 | + health_check { |
| 209 | + healthy_threshold = 2 |
| 210 | + unhealthy_threshold = 2 |
| 211 | + timeout = 3 |
| 212 | + target = "TCP:1194" |
| 213 | + interval = 30 |
| 214 | + } |
| 215 | + |
| 216 | + tags { |
| 217 | + Name = "${var.stack_item_label}-vpn-elb" |
| 218 | + application = "${var.stack_item_fullname}-vpn" |
| 219 | + managed_by = "terraform" |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +# Create a Route53 record |
| 224 | + |
0 commit comments