-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunifi.yml
441 lines (397 loc) · 11.7 KB
/
unifi.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
Parameters:
ManagementIpAddress:
Type: String
Description: The IP address to whitelist for access to the controllers
VpcId:
Type: AWS::EC2::VPC::Id
Description: The VPC to use
ExternalSubnetIds:
Type: List<AWS::EC2::Subnet::Id>
Description: List of subnets where the Network Load Balancer will reside
InternalSubnetIds:
Type: List<AWS::EC2::Subnet::Id>
Description: List of subnets where Unifi controllers will reside
UbuntuImageId:
Type: AWS::EC2::Image::Id
Default: ami-000c2343cf03d7fd7
Description: AMI ID for latest Ubuntu 16.04
InstanceType:
Type: String
Description: Instance size for the controllers
Default: t3a.micro
AllowedValues:
- t3a.micro
- t3.micro
LoadBalancerCertArn:
Type: String
Description: >-
ARN of ACM Certificate for TLS on the Network Load Balancer
where applicable
HostedZoneId:
Type: String
Description: Route53 Hosted Zone ID for the Domain name
DomainName:
Type: String
Description: Domain name to use for the load balancer
KeyName:
Type: String
Description: EC2 Key Pair name for SSH access to controllers
Resources:
DNSRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZoneId
Comment: !Ref AWS::StackName
Name: !Ref DomainName
TTL: 300
Type: CNAME
ResourceRecords:
- !GetAtt LoadBalancer.DNSName
LoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
IpAddressType: ipv4
LoadBalancerAttributes:
- Key: load_balancing.cross_zone.enabled
Value: true
Scheme: internet-facing
Subnets: !Ref ExternalSubnetIds
Type: network
Listener22:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ControllerTarget22
LoadBalancerArn: !Ref LoadBalancer
Port: 22
Protocol: TCP
Listener8080:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ControllerTarget8080
LoadBalancerArn: !Ref LoadBalancer
Port: 8080
Protocol: TCP
Listener8443:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
Certificates:
- CertificateArn: !Ref LoadBalancerCertArn
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ControllerTarget8443
LoadBalancerArn: !Ref LoadBalancer
Port: 8443
Protocol: TLS
Listener8843:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
Certificates:
- CertificateArn: !Ref LoadBalancerCertArn
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ControllerTarget8843
LoadBalancerArn: !Ref LoadBalancer
Port: 8843
Protocol: TLS
Listener8880:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ControllerTarget8880
LoadBalancerArn: !Ref LoadBalancer
Port: 8880
Protocol: TCP
Listener6789:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ControllerTarget6789
LoadBalancerArn: !Ref LoadBalancer
Port: 6789
Protocol: TCP
Listener3478:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ControllerTarget3478
LoadBalancerArn: !Ref LoadBalancer
Port: 3478
Protocol: UDP
Listener5656:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ControllerTarget5656
LoadBalancerArn: !Ref LoadBalancer
Port: 5656
Protocol: UDP
EFS:
Type: AWS::EFS::FileSystem
EFSMountTarget:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref EFS
SecurityGroups:
- !GetAtt EFSSecurityGroup.GroupId
SubnetId: !Select [ 0, !Ref InternalSubnetIds ]
BackupRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- backup.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonElasticFileSystemFullAccess
BackupVault:
Type: AWS::Backup::BackupVault
Properties:
BackupVaultName: !Ref AWS::StackName
BackupPlan:
DependsOn: BackupVault
Type: AWS::Backup::BackupPlan
Properties:
BackupPlan:
BackupPlanName: !Ref AWS::StackName
BackupPlanRule:
- RuleName: Daily
TargetBackupVault: !Ref BackupVault
ScheduleExpression: cron(0 0 * * ? *)
BackupSelection:
DependsOn: BackupPlan
Type: AWS::Backup::BackupSelection
Properties:
BackupPlanId: !Ref BackupPlan
BackupSelection:
SelectionName: !Ref AWS::StackName
IamRoleArn: !GetAtt BackupRole.Arn
Resources:
- !Sub arn:aws:elasticfilesystem:${AWS::Region}:${AWS::AccountId}:file-system/${EFS}
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
ImageId: !Ref UbuntuImageId
InstanceInitiatedShutdownBehavior: terminate
InstanceType: !Ref InstanceType
KeyName: !Ref KeyName
SecurityGroupIds:
- !GetAtt SecurityGroup.GroupId
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: unifi-controller
UserData:
Fn::Base64:
!Sub |
#cloud-config
repo_update: true
repo_upgrade: all
runcmd:
# General
- apt update -y;
- apt upgrade -y;
- apt install awscli ca-certificates apt-transport-https -y;
# EFS tools
- git clone https://github.com/aws/efs-utils;
- apt install binutils -y;
- cd efs-utils;
- ./build-deb.sh;
- apt install ./build/amazon-efs-utils*deb -y;
- cd;
# Mount EFS
- file_system_id_01=${EFS};
- efs_directory=/var/lib/unifi;
- mkdir -p $efs_directory;
- echo "$file_system_id_01:/ $efs_directory efs tls,_netdev" >> /etc/fstab;
- mount -a -t efs defaults;
# Unifi
- echo 'deb https://www.ui.com/downloads/unifi/debian stable ubiquiti' | sudo tee /etc/apt/sources.list.d/100-ubnt-unifi.list;
- apt-key adv --keyserver keyserver.ubuntu.com --recv 06E85760C0A52C50;
- apt update -y;
- apt purge java* -y;
- apt install default-jre -y;
- echo "unifi unifi/has_backup boolean true" | debconf-set-selections;
- DEBIAN_FRONTEND=noninteractive apt-get install unifi -y;
AutoUpdateScaleUp:
Type: AWS::AutoScaling::ScheduledAction
Properties:
AutoScalingGroupName: !Ref ControllerGroup
DesiredCapacity: 2
MinSize: 1
MaxSize: 2
Recurrence: '0 14 * * *'
AutoUpdateScaleDown:
Type: AWS::AutoScaling::ScheduledAction
Properties:
AutoScalingGroupName: !Ref ControllerGroup
DesiredCapacity: 1
MinSize: 1
MaxSize: 2
Recurrence: '10 14 * * *'
ControllerGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier: !Ref InternalSubnetIds
DesiredCapacity: 1
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !GetAtt LaunchTemplate.LatestVersionNumber
MaxSize: 2
MinSize: 1
TargetGroupARNs:
- !Ref ControllerTarget22
- !Ref ControllerTarget8080
- !Ref ControllerTarget8443
- !Ref ControllerTarget8843
- !Ref ControllerTarget8880
- !Ref ControllerTarget6789
- !Ref ControllerTarget3478
- !Ref ControllerTarget5656
ControllerTarget22:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPort: 22
HealthCheckProtocol: TCP
Protocol: TCP
Port: 22
TargetType: instance
VpcId: !Ref VpcId
ControllerTarget8080:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPort: 22
HealthCheckProtocol: TCP
Protocol: TCP
Port: 8080
TargetType: instance
VpcId: !Ref VpcId
ControllerTarget8443:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPort: 22
HealthCheckProtocol: TCP
Protocol: TLS
Port: 8443
TargetType: instance
VpcId: !Ref VpcId
ControllerTarget8843:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPort: 22
HealthCheckProtocol: TCP
Protocol: TLS
Port: 8843
TargetType: instance
VpcId: !Ref VpcId
ControllerTarget8880:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPort: 22
HealthCheckProtocol: TCP
Protocol: TCP
Port: 8880
TargetType: instance
VpcId: !Ref VpcId
ControllerTarget6789:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPort: 22
HealthCheckProtocol: TCP
Protocol: TCP
Port: 6789
TargetType: instance
VpcId: !Ref VpcId
ControllerTarget3478:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPort: 22
HealthCheckProtocol: TCP
Protocol: UDP
Port: 3478
TargetType: instance
VpcId: !Ref VpcId
ControllerTarget5656:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPort: 22
HealthCheckProtocol: TCP
Protocol: UDP
Port: 5656
TargetType: instance
VpcId: !Ref VpcId
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VpcId
GroupDescription: Unifi Security Group
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 10.254.1.0/24
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 10.254.2.0/24
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 10.254.3.0/24
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Sub ${ManagementIpAddress}/32
- IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp: !Sub ${ManagementIpAddress}/32
- IpProtocol: tcp
FromPort: 8443
ToPort: 8443
CidrIp: !Sub ${ManagementIpAddress}/32
- IpProtocol: tcp
FromPort: 8843
ToPort: 8843
CidrIp: !Sub ${ManagementIpAddress}/32
- IpProtocol: tcp
FromPort: 8880
ToPort: 8880
CidrIp: !Sub ${ManagementIpAddress}/32
- IpProtocol: tcp
FromPort: 6789
ToPort: 6789
CidrIp: !Sub ${ManagementIpAddress}/32
- IpProtocol: udp
FromPort: 3478
ToPort: 3478
CidrIp: !Sub ${ManagementIpAddress}/32
- IpProtocol: udp
FromPort: 5656
ToPort: 5699
CidrIp: !Sub ${ManagementIpAddress}/32
EFSSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VpcId
GroupDescription: EFS Security Group
SecurityGroupIngress:
- IpProtocol: -1
SourceSecurityGroupId: !GetAtt SecurityGroup.GroupId