-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
191 lines (152 loc) · 4.83 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
191
# Change these to match your environment
locals {
# The region to deploy the Gateway instances in.
region = "us-east-1"
# The availability zone to deploy the Gateway instances in.
availability_zone = "us-east-1a"
# Generate a token from the admin portal in Sites -> <site> -> Deploy Gateway.
firezone_token = "<YOUR TOKEN HERE>"
# We recommend a minimum of 3 instances for high availability.
gateway_count = 4
# Whether to attach Elastic IPs to the Gateway instances. Set to false to restrict the Gateways
# to private subnets only. Note: using only private subnets for the Gateway will require a NAT in a public subnet.
attach_public_ips = true
}
module "gateway" {
source = "firezone/gateway/aws"
###################
# Required inputs #
###################
# Generate a token from the admin portal in Sites -> <site> -> Deploy Gateway.
# Only one token is needed for the cluster.
firezone_token = local.firezone_token
# Pick an AMI to use. We recommend Ubuntu LTS or Amazon Linux 2.
base_ami = data.aws_ami_ids.ubuntu.ids[0]
# Attach the Gateways to your VPC and subnets.
vpc = aws_vpc.main.id
private_subnet = aws_subnet.private.id
instance_security_groups = [
aws_security_group.instance.id
]
###################
# Optional inputs #
###################
# Attach existing Elastic IPs. Length must match the number of replicas if provided.
aws_eip_ids = aws_eip.gateway[*].id
# We recommend a minimum of 3 instances for high availability.
replicas = local.gateway_count
# Deploy a specific version of the Gateway. Generally, we recommend using the latest version.
# firezone_version = "latest"
# Override the default API URL. This should almost never be needed.
# firezone_api_url = "wss://api.firezone.dev"
# Gateways are very lightweight.
# See https://www.firezone.dev/kb/deploy/gateways#sizing-recommendations.
# instance_type = "t3.nano"
# Availability zone to deploy the instances in.
availability_zone = local.availability_zone
}
data "aws_ami_ids" "ubuntu" {
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-*-22.04-amd64-server-*"]
}
}
provider "aws" {
# Change this to your desired region
region = local.region
}
resource "aws_vpc" "main" {
cidr_block = "172.16.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
}
resource "aws_subnet" "public" {
availability_zone = local.availability_zone
vpc_id = aws_vpc.main.id
cidr_block = "172.16.0.0/24"
# We will attach a public IP to the instances ourselves.
map_public_ip_on_launch = false
}
resource "aws_subnet" "private" {
availability_zone = local.availability_zone
vpc_id = aws_vpc.main.id
cidr_block = "172.16.1.0/24"
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
}
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
}
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "private" {
subnet_id = aws_subnet.private.id
route_table_id = aws_route_table.private.id
}
resource "aws_security_group" "instance" {
vpc_id = aws_vpc.main.id
// allow SSH from other machines on the subnet
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
aws_subnet.private.cidr_block,
aws_subnet.public.cidr_block
]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "instance_connect" {
name = "allow egress to all vpc subnets"
description = "Security group to allow SSH to vpc subnets. Created for use with EC2 Instance Connect Endpoint."
vpc_id = aws_vpc.main.id
egress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
aws_subnet.private.cidr_block,
aws_subnet.public.cidr_block
]
}
}
resource "aws_ec2_instance_connect_endpoint" "instance_connect_endpoint" {
subnet_id = aws_subnet.public.id
preserve_client_ip = false
security_group_ids = [
aws_security_group.instance_connect.id
]
tags = {
Name = "firezone-gateway-instance-connect-endpoint"
}
}
resource "aws_eip" "gateway" {
count = local.attach_public_ips ? local.gateway_count : 0
domain = "vpc"
}
# Output the Elastic IPs for the Gateway instances.
output "public_ips" {
description = "The public IPs of the Gateway instances"
value = aws_eip.gateway[*].public_ip
}