-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cf.yml
284 lines (256 loc) · 7.89 KB
/
stack.cf.yml
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
AWSTemplateFormatVersion: 2010-09-09
Description: WireGuard VPN server, inside its own VPC, with sensible networking defaults
Parameters:
#TODO: Allow for use of a specific (Ubuntu) AMI instead of the preconfigured ones (use a condition)
#AMIID:
# Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
# Type: String
InstanceTypeParameter:
Description: WireGuard server EC2 instance type
Type: String
Default: t3a.nano
AllowedValues:
- t2.nano
- t2.micro
- t3.nano
- t3.micro
- t3a.nano
- t3a.micro
ConstraintDescription: must be a valid EC2 instance type.
KeyName: #TODO: remove the use of SSH?
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: 'AWS::EC2::KeyPair::KeyName'
ConstraintDescription: must be the name of an existing EC2 KeyPair.
VPNDomainHostedZoneId:
Description: Route53 zone
Type: 'AWS::Route53::HostedZone::Id'
ConstraintDescription: must be an existing Route53 hosted zone in the region of the server
VPNServerDomainName:
Description: Full domain name (FQDN) used by the server
Type: String
AllowedPattern: '((?=[a-z0-9-]{1,63}\.)([a-z0-9-]{1,63}\.)[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}'
ConstraintDescription: must be a valid domain name including the host part (e.g vpn.example.net)
TunnelServerIP:
Description: WireGuard server VPN tunnel IP address
Type: String
Default: 10.1.2.1
AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})'
ConstraintDescription: must be a valid IP address in the form of x.x.x.x
TunnelPeerIP:
Description: WireGuard client VPN tunnel IP address
Type: String
Default: 10.1.2.2
AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})'
ConstraintDescription: must be a valid IP address in the form of x.x.x.x
WireGuardPort:
Description: WireGuard port
Type: Number
Default: 51820
MinValue: 1024
MaxValue: 65535
ConstraintDescription: must be a valid port (1024-65535)
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: "Instance Configuration"
Parameters:
- InstanceTypeParameter
- KeyName #TODO: remove the use of SSH?
- VPNDomainHostedZoneId
- VPNServerDomainName
-
Label:
default: "WireGuard Configuration"
Parameters:
- WireGuardPort
- TunnelServerIP
- TunnelPeerIP
ParameterLabels:
InstanceTypeParameter:
default: "Instance Type"
VPNDomainHostedZoneId:
default: "Domain Hosted Zone"
VPNServerDomainName:
default: "Server domain name"
Mappings:
AWSRegionArch2AMI: #Id of the official Ubuntu Focal Fossa 20.04 AMI
us-east-1:
HVM64: ami-068663a3c619dd892
us-west-2:
HVM64: ami-09dd2e08d601bff67
us-west-1:
HVM64: ami-075fd582acf0c0128
eu-west-1:
HVM64: ami-0dad359ff462124ca
eu-west-2:
HVM64: ami-0917237b4e71c5759
eu-west-3:
HVM64: ami-0e60c6afa19d896ee
eu-central-1:
HVM64: ami-05c26ae4789875080
eu-north-1:
HVM64: ami-0d4e2b57f569e9daa
Resources:
wgVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.1.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: false
InstanceTenancy: default
Tags:
- Key: Name
Value: !Ref AWS::StackName
wgInternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Ref AWS::StackName
wgVPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
DependsOn:
- wgVPC
- wgInternetGateway
Properties:
VpcId: !Ref wgVPC
InternetGatewayId: !Ref wgInternetGateway
wgRouteTable:
Type: AWS::EC2::RouteTable
DependsOn:
- wgVPC
Properties:
VpcId: !Ref wgVPC
Tags:
- Key: Name
Value: !Ref AWS::StackName
wgRoute:
Type: AWS::EC2::Route
DependsOn:
- wgInternetGateway
- wgRouteTable
- wgVPCGatewayAttachment
Properties:
RouteTableId: !Ref wgRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref wgInternetGateway
wgSubnet:
Type: AWS::EC2::Subnet
DependsOn:
- wgVPC
Properties:
CidrBlock: 10.1.1.0/28
MapPublicIpOnLaunch: true
VpcId: !Ref wgVPC
Tags:
- Key: Name
Value: !Ref AWS::StackName
wgSubnetRouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
DependsOn:
- wgRouteTable
- wgSubnet
- wgRoute
Properties:
RouteTableId: !Ref wgRouteTable
SubnetId: !Ref wgSubnet
wgSecurityGroup:
Type: AWS::EC2::SecurityGroup
DependsOn:
- wgVPC
Properties:
VpcId: !Ref wgVPC
GroupDescription: Wireguard Security Group
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
SecurityGroupIngress:
- IpProtocol: udp
FromPort: !Ref WireGuardPort
ToPort: !Ref WireGuardPort
CidrIp: 0.0.0.0/0
- IpProtocol: tcp #TODO: remove the use of SSH?
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Ref AWS::StackName
wgEC2Instance:
Type: AWS::EC2::Instance
DependsOn:
- wgSubnetRouteTableAssociation
- wgSecurityGroup
Metadata:
'AWS::CloudFormation::Init':
configSets:
Install:
- Install
Install:
packages:
apt:
wireguard: []
Properties:
InstanceType:
Ref: InstanceTypeParameter
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
DeleteOnTermination: true
VolumeSize: 8
Encrypted: true
InstanceInitiatedShutdownBehavior: terminate
SecurityGroupIds:
- Ref: wgSecurityGroup
ImageId: !FindInMap
- AWSRegionArch2AMI
- !Ref 'AWS::Region'
- HVM64
SubnetId: !Ref wgSubnet
KeyName: !Ref KeyName #TODO: remove the use of SSH?
Tags:
- Key: Name
Value: !Ref AWS::StackName
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
set -euxo pipefail
apt-get update && apt-get upgrade -y
mkdir -p /tmp/aws-cfn-bootstrap-latest
curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xzv -C /tmp/aws-cfn-bootstrap-latest --strip-components 1
cd /tmp/aws-cfn-bootstrap-latest
# cfn-bootstrap not compatible with python3
# hack based on https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/151#issuecomment-619835104
apt-get install -y python2 python-setuptools
python2 setup.py install
# replace shebang to specify python2
sed -i "s~/usr/bin/env python~/usr/bin/env python2~" /usr/local/bin/cfn-*
/usr/local/bin/cfn-init -v --stack ${AWS::StackName} --resource wgEC2Instance --configsets Install --region ${AWS::Region}
/usr/local/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource wgEC2Instance --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Timeout: PT5M
wgDNSRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref VPNDomainHostedZoneId
Comment: !Ref AWS::StackName
Name: !Ref VPNServerDomainName
TTL: 300
Type: A
ResourceRecords:
- !GetAtt wgEC2Instance.PublicIp
Outputs:
wgServerIP:
Description: Public IP address of the WireGuard VPN server
Value: !GetAtt wgEC2Instance.PublicIp
Export:
Name: !Sub "${AWS::StackName}-PublicIP"
wgServerDomainName:
Description: FQDN of the WireGuard VPN server
Value: !Ref wgDNSRecord
Export:
Name: !Sub "${AWS::StackName}-DomainName"