|
| 1 | +data "aws_region" "current" {} |
| 2 | + |
| 3 | +data "aws_caller_identity" "current" {} |
| 4 | + |
| 5 | +resource "aws_security_group" "pritunl" { |
| 6 | + name = "${var.resource_name_prefix}-vpn" |
| 7 | + description = "${var.resource_name_prefix}-vpn" |
| 8 | + vpc_id = "${var.vpc_id}" |
| 9 | + |
| 10 | + # SSH access |
| 11 | + ingress { |
| 12 | + from_port = 22 |
| 13 | + to_port = 22 |
| 14 | + protocol = "tcp" |
| 15 | + cidr_blocks = var.internal_cidrs |
| 16 | + } |
| 17 | + |
| 18 | + # HTTP access for Let's Encrypt validation |
| 19 | + ingress { |
| 20 | + from_port = 80 |
| 21 | + to_port = 80 |
| 22 | + protocol = "tcp" |
| 23 | + |
| 24 | + cidr_blocks = var.whitelist_http |
| 25 | + } |
| 26 | + |
| 27 | + # HTTPS access |
| 28 | + ingress { |
| 29 | + from_port = 443 |
| 30 | + to_port = 443 |
| 31 | + protocol = "tcp" |
| 32 | + cidr_blocks = var.internal_cidrs |
| 33 | + } |
| 34 | + |
| 35 | + # VPN WAN access |
| 36 | + ingress { |
| 37 | + from_port = 10000 |
| 38 | + to_port = 19999 |
| 39 | + protocol = "udp" |
| 40 | + cidr_blocks = ["0.0.0.0/0"] |
| 41 | + } |
| 42 | + |
| 43 | + # ICMP |
| 44 | + ingress { |
| 45 | + from_port = -1 |
| 46 | + to_port = -1 |
| 47 | + protocol = "icmp" |
| 48 | + cidr_blocks = var.internal_cidrs |
| 49 | + } |
| 50 | + |
| 51 | + # outbound internet access |
| 52 | + egress { |
| 53 | + from_port = 0 |
| 54 | + to_port = 0 |
| 55 | + protocol = "-1" |
| 56 | + cidr_blocks = ["0.0.0.0/0"] |
| 57 | + } |
| 58 | + |
| 59 | + tags = "${ |
| 60 | + merge( |
| 61 | + map("Name", format("%s-%s", var.resource_name_prefix, "vpn")), |
| 62 | + var.tags, |
| 63 | + ) |
| 64 | + }" |
| 65 | +} |
| 66 | + |
| 67 | +resource "aws_security_group" "allow_from_office" { |
| 68 | + name = "${var.resource_name_prefix}-whitelist" |
| 69 | + description = "Allows SSH connections and HTTP(s) connections from office" |
| 70 | + vpc_id = "${var.vpc_id}" |
| 71 | + |
| 72 | + # SSH access |
| 73 | + ingress { |
| 74 | + description = "Allow SSH access from select CIDRs" |
| 75 | + from_port = 22 |
| 76 | + to_port = 22 |
| 77 | + protocol = "tcp" |
| 78 | + cidr_blocks = var.whitelist |
| 79 | + } |
| 80 | + |
| 81 | + # HTTPS access |
| 82 | + ingress { |
| 83 | + description = "Allow HTTPS access from select CIDRs" |
| 84 | + from_port = 443 |
| 85 | + to_port = 443 |
| 86 | + protocol = "tcp" |
| 87 | + cidr_blocks = var.whitelist |
| 88 | + } |
| 89 | + |
| 90 | + # ICMP |
| 91 | + ingress { |
| 92 | + description = "Allow ICMPv4 from select CIDRs" |
| 93 | + from_port = -1 |
| 94 | + to_port = -1 |
| 95 | + protocol = "icmp" |
| 96 | + cidr_blocks = var.whitelist |
| 97 | + } |
| 98 | + |
| 99 | + # outbound internet access |
| 100 | + egress { |
| 101 | + from_port = 0 |
| 102 | + to_port = 0 |
| 103 | + protocol = "-1" |
| 104 | + cidr_blocks = ["0.0.0.0/0"] |
| 105 | + } |
| 106 | + |
| 107 | + tags = "${ |
| 108 | + merge( |
| 109 | + map("Name", format("%s-%s", var.resource_name_prefix, "whitelist")), |
| 110 | + var.tags, |
| 111 | + ) |
| 112 | + }" |
| 113 | +} |
| 114 | + |
| 115 | +data "aws_ami" "oracle" { |
| 116 | + most_recent = true |
| 117 | + |
| 118 | + filter { |
| 119 | + name = "name" |
| 120 | + values = ["OL7.6-x86_64-HVM-2019-01-29"] |
| 121 | + } |
| 122 | + |
| 123 | + filter { |
| 124 | + name = "virtualization-type" |
| 125 | + values = ["hvm"] |
| 126 | + } |
| 127 | + |
| 128 | + owners = ["131827586825"] # Canonical |
| 129 | +} |
| 130 | + |
| 131 | +resource "aws_instance" "pritunl" { |
| 132 | + ami = "${data.aws_ami.oracle.id}" |
| 133 | + instance_type = "${var.instance_type}" |
| 134 | + key_name = "${var.aws_key_name}" |
| 135 | + user_data = "${file("${path.module}/provision.sh")}" |
| 136 | + |
| 137 | + vpc_security_group_ids = [ |
| 138 | + "${aws_security_group.pritunl.id}", |
| 139 | + "${aws_security_group.allow_from_office.id}", |
| 140 | + ] |
| 141 | + |
| 142 | + subnet_id = "${var.public_subnet_id}" |
| 143 | + |
| 144 | + tags = "${ |
| 145 | + merge( |
| 146 | + map("Name", format("%s-%s", var.resource_name_prefix, "vpn")), |
| 147 | + var.tags, |
| 148 | + ) |
| 149 | + }" |
| 150 | + |
| 151 | +} |
| 152 | + |
| 153 | +# resource "aws_route53_record" "pritunl-www" { |
| 154 | +# zone_id = "${var.route53_zoneid}" |
| 155 | +# name = "${var.domain_name}" |
| 156 | +# type = "A" |
| 157 | +# ttl = "300" |
| 158 | +# records = ["${aws_instance.pritunl.public_ip}"] |
| 159 | +# } |
| 160 | + |
| 161 | + |
0 commit comments