-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
190 lines (149 loc) · 4.19 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
required_version = ">= 1.3.0"
}
provider "aws" {
region = var.region.vpn_vpc
default_tags {
tags = { Name : "AWS VPN" }
}
}
provider "aws" {
region = var.region.libreswan_vpc
alias = "libreswan"
default_tags {
tags = { Name : "Libreswan" }
}
}
resource "aws_eip" "vpn" {
domain = "vpc"
}
resource "aws_eip" "libreswan" {
domain = "vpc"
provider = aws.libreswan
}
resource "random_integer" "this" {
min = 0
max = 254
}
locals {
cidr_block = {
vpn_vpc : coalesce(var.cidr_block.vpn_vpc, "192.168.${random_integer.this.result}.0/24"),
libreswan_vpc : coalesce(var.cidr_block.libreswan_vpc, "192.168.${random_integer.this.result + 1}.0/24"),
}
}
module "vpn_vpc" {
source = "./modules/vpc"
cidr_block = local.cidr_block.vpn_vpc
peer_cidr_block = local.cidr_block.libreswan_vpc
}
module "libreswan_vpc" {
source = "./modules/vpc"
cidr_block = local.cidr_block.libreswan_vpc
peer_cidr_block = local.cidr_block.vpn_vpc
vpn_tunnel1_address = aws_vpn_connection.this.tunnel1_address
providers = {
aws = aws.libreswan
}
}
resource "aws_vpn_gateway" "this" {
vpc_id = module.vpn_vpc.vpc_id
}
resource "aws_vpn_gateway_attachment" "this" {
vpc_id = module.vpn_vpc.vpc_id
vpn_gateway_id = aws_vpn_gateway.this.id
}
resource "aws_customer_gateway" "this" {
bgp_asn = 65000
type = "ipsec.1"
ip_address = aws_eip.libreswan.public_ip
device_name = "Libreswan"
lifecycle {
create_before_destroy = true
}
}
resource "aws_vpn_connection" "this" {
customer_gateway_id = aws_customer_gateway.this.id
type = "ipsec.1"
vpn_gateway_id = aws_vpn_gateway.this.id
static_routes_only = true
}
resource "aws_vpn_gateway_route_propagation" "this" {
vpn_gateway_id = aws_vpn_gateway.this.id
route_table_id = module.vpn_vpc.default_route_table_id
}
resource "aws_vpn_connection_route" "this" {
destination_cidr_block = module.libreswan_vpc.cidr_block
vpn_connection_id = aws_vpn_connection.this.id
}
resource "tls_private_key" "this" {
algorithm = "ED25519"
}
resource "random_pet" "this" {}
resource "local_file" "this" {
content = tls_private_key.this.public_key_openssh
filename = "${path.module}/${random_pet.this.id}.pub"
}
resource "local_sensitive_file" "this" {
content = tls_private_key.this.private_key_openssh
filename = "${path.module}/${random_pet.this.id}"
}
resource "aws_key_pair" "vpn" {
key_name = random_pet.this.id
public_key = tls_private_key.this.public_key_openssh
}
resource "aws_key_pair" "libreswan" {
key_name = random_pet.this.id
public_key = tls_private_key.this.public_key_openssh
provider = aws.libreswan
}
data "cloudinit_config" "vpn" {
gzip = false
base64_encode = false
part {
content = file("user-data-vpn.yaml")
content_type = "text/cloud-config"
filename = "cloud-init.conf"
}
}
module "vpn_instance" {
source = "./modules/instance"
user_data = data.cloudinit_config.vpn.rendered
eip_allocation_id = aws_eip.vpn.id
subnet_id = module.vpn_vpc.subnet_id
security_group_ids = [module.vpn_vpc.default_security_group_id]
key_name = aws_key_pair.vpn.key_name
}
data "cloudinit_config" "libreswan" {
gzip = false
base64_encode = false
part {
content = templatefile(
"user-data-libreswan.yaml",
{
libreswan_ip : aws_eip.libreswan.public_ip,
vpn_ip : aws_vpn_connection.this.tunnel1_address,
libreswan_cidr_block : module.libreswan_vpc.cidr_block,
vpn_cidr_block : module.vpn_vpc.cidr_block,
psk : aws_vpn_connection.this.tunnel1_preshared_key,
}
)
content_type = "text/jinja2"
filename = "cloud-init.conf"
}
}
module "libreswan_instance" {
source = "./modules/instance"
user_data = data.cloudinit_config.libreswan.rendered
eip_allocation_id = aws_eip.libreswan.id
subnet_id = module.libreswan_vpc.subnet_id
security_group_ids = [module.libreswan_vpc.default_security_group_id]
key_name = aws_key_pair.libreswan.key_name
providers = {
aws = aws.libreswan
}
}