-
Notifications
You must be signed in to change notification settings - Fork 41
/
efs-backup.yaml
442 lines (432 loc) · 14.3 KB
/
efs-backup.yaml
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
442
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS in Action: chapter 9'
Mappings:
RegionMap:
'eu-north-1':
AMI: 'ami-05bc2576a72f22c39'
'ap-south-1':
AMI: 'ami-0002bdad91f793433'
'eu-west-3':
AMI: 'ami-0c73cd1c5347436f3'
'eu-west-2':
AMI: 'ami-029ed17b4ea379178'
'eu-west-1':
AMI: 'ami-04632f3cef5083854'
'ap-northeast-3':
AMI: 'ami-0ae88850834d2c589'
'ap-northeast-2':
AMI: 'ami-0263588f2531a56bd'
'ap-northeast-1':
AMI: 'ami-0abaa5b0faf689830'
'sa-east-1':
AMI: 'ami-053a035b046dbb704'
'ca-central-1':
AMI: 'ami-0173297cea9ba27b0'
'ap-southeast-1':
AMI: 'ami-0d1d4b8d5a0cd293f'
'ap-southeast-2':
AMI: 'ami-0f4484f62c4fd8767'
'eu-central-1':
AMI: 'ami-099ccc441b2ef41ec'
'us-east-1':
AMI: 'ami-061ac2e015473fbe2'
'us-east-2':
AMI: 'ami-056b1936002ca8ede'
'us-west-1':
AMI: 'ami-028f2b5ee08012131'
'us-west-2':
AMI: 'ami-0e21d4d9303512b8e'
Resources:
##########################################################################
# #
# VPC with two public subnets #
# #
##########################################################################
VPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: '172.31.0.0/16'
EnableDnsHostnames: true
InternetGateway:
Type: 'AWS::EC2::InternetGateway'
Properties: {}
VPCGatewayAttachment:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
SubnetA:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Select [0, !GetAZs '']
CidrBlock: '172.31.38.0/24'
VpcId: !Ref VPC
SubnetB:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Select [1, !GetAZs '']
CidrBlock: '172.31.37.0/24'
VpcId: !Ref VPC
RouteTable:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref VPC
SubnetRouteTableAssociationA:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref SubnetA
RouteTableId: !Ref RouteTable
SubnetRouteTableAssociationB:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref SubnetB
RouteTableId: !Ref RouteTable
RouteToInternet:
Type: 'AWS::EC2::Route'
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: '0.0.0.0/0'
GatewayId: !Ref InternetGateway
DependsOn: VPCGatewayAttachment
NetworkAcl:
Type: AWS::EC2::NetworkAcl
Properties:
VpcId: !Ref VPC
SubnetNetworkAclAssociationA:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetA
NetworkAclId: !Ref NetworkAcl
SubnetNetworkAclAssociationB:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetB
NetworkAclId: !Ref NetworkAcl
NetworkAclEntryIngress:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: 100
Protocol: -1
RuleAction: allow
Egress: false
CidrBlock: '0.0.0.0/0'
NetworkAclEntryEgress:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: 100
Protocol: -1
RuleAction: allow
Egress: true
CidrBlock: 0.0.0.0/0
##########################################################################
# #
# EFS related resources #
# #
##########################################################################
FileSystem:
Type: 'AWS::EFS::FileSystem'
Properties:
Encrypted: true
ThroughputMode: bursting
PerformanceMode: generalPurpose
FileSystemPolicy:
Version: '2012-10-17'
Statement:
- Effect: 'Deny'
Action: '*'
Principal:
AWS: '*'
Condition:
Bool:
'aws:SecureTransport': 'false'
EFSClientSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: 'EFS Mount target client'
VpcId: !Ref VPC
MountTargetSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: 'EFS Mount target'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 2049
ToPort: 2049
SourceSecurityGroupId: !Ref EFSClientSecurityGroup
VpcId: !Ref VPC
MountTargetA:
Type: 'AWS::EFS::MountTarget'
Properties:
FileSystemId: !Ref FileSystem
SecurityGroups:
- !Ref MountTargetSecurityGroup
SubnetId: !Ref SubnetA
MountTargetB:
Type: 'AWS::EFS::MountTarget'
Properties:
FileSystemId: !Ref FileSystem
SecurityGroups:
- !Ref MountTargetSecurityGroup
SubnetId: !Ref SubnetB
##########################################################################
# #
# EC2 instances for testing #
# #
##########################################################################
IamRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'ec2.amazonaws.com'
Action: 'sts:AssumeRole'
Policies:
- PolicyName: efs
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'elasticfilesystem:ClientRootAccess'
- 'elasticfilesystem:ClientWrite'
- 'elasticfilesystem:ClientMount'
- 'elasticfilesystem:DescribeMountTargets'
Resource: !GetAtt 'FileSystem.Arn'
- Effect: Allow
Action:
- 'ec2:DescribeAvailabilityZones'
Resource: '*'
- PolicyName: ssm
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'ssmmessages:*' # SSM Agent by https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-setting-up-messageAPIs.html
- 'ssm:UpdateInstanceInformation' # SSM agent by https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-setting-up-messageAPIs.html
- 'ec2messages:*' # SSM Session Manager by https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-setting-up-messageAPIs.html
Resource: '*'
IamInstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Roles:
- !Ref IamRole
EC2InstanceA:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
InstanceType: 't3.micro'
IamInstanceProfile: !Ref IamInstanceProfile
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeleteOnTermination: true
DeviceIndex: 0
GroupSet:
- !Ref EFSClientSecurityGroup
SubnetId: !Ref SubnetA
UserData:
'Fn::Base64': !Sub |
#!/bin/bash -ex
trap '/opt/aws/bin/cfn-signal -e 1 --stack ${AWS::StackName} --resource EC2InstanceA --region ${AWS::Region}' ERR
# install dependencies
yum install -y amazon-efs-utils
pip3 install botocore
# copy existing /home to /oldhome
mkdir /oldhome
cp -a /home/. /oldhome
# wait for EFS mount target
while ! (echo > /dev/tcp/${FileSystem}.efs.${AWS::Region}.amazonaws.com/2049) >/dev/null 2>&1; do sleep 5; done
# mount EFS file system
echo "${FileSystem}:/ /home efs _netdev,noresvport,tls,iam 0 0" >> /etc/fstab
mount -a
# copy /oldhome to new /home
cp -a /oldhome/. /home
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource EC2InstanceA --region ${AWS::Region}
Tags:
- Key: Name
Value: 'efs-a'
CreationPolicy:
ResourceSignal:
Timeout: PT10M
DependsOn:
- VPCGatewayAttachment
- MountTargetA
EC2InstanceB:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
InstanceType: 't3.micro'
IamInstanceProfile: !Ref IamInstanceProfile
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeleteOnTermination: true
DeviceIndex: 0
GroupSet:
- !Ref EFSClientSecurityGroup
SubnetId: !Ref SubnetB
UserData:
'Fn::Base64': !Sub |
#!/bin/bash -ex
trap '/opt/aws/bin/cfn-signal -e 1 --stack ${AWS::StackName} --resource EC2InstanceB --region ${AWS::Region}' ERR
# install dependencies
yum install -y amazon-efs-utils
pip3 install botocore
# wait for EFS mount target
while ! (echo > /dev/tcp/${FileSystem}.efs.${AWS::Region}.amazonaws.com/2049) >/dev/null 2>&1; do sleep 5; done
# mount EFS file system
echo "${FileSystem}:/ /home efs _netdev,noresvport,tls,iam 0 0" >> /etc/fstab
mount -a
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource EC2InstanceB --region ${AWS::Region}
Tags:
- Key: Name
Value: 'efs-b'
CreationPolicy:
ResourceSignal:
Timeout: PT10M
DependsOn:
- VPCGatewayAttachment
- MountTargetB
##########################################################################
# #
# Backup related resources #
# #
##########################################################################
BackupRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: 'backup.amazonaws.com'
Action: 'sts:AssumeRole'
Policies:
- PolicyName: backup
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'elasticfilesystem:Backup'
- 'elasticfilesystem:DescribeTags'
Resource: !Sub 'arn:${AWS::Partition}:elasticfilesystem:${AWS::Region}:${AWS::AccountId}:file-system/${FileSystem}'
BackupVault:
Type: 'AWS::Backup::BackupVault'
Properties:
BackupVaultName: !Ref 'AWS::StackName'
BackupPlan:
Type: 'AWS::Backup::BackupPlan'
Properties:
BackupPlan:
BackupPlanName: 'efs-daily'
BackupPlanRule:
- RuleName: 'efs-daily'
TargetBackupVault: !Ref BackupVault
Lifecycle:
DeleteAfterDays: 30
ScheduleExpression: 'cron(0 5 ? * * *)'
CompletionWindowMinutes: 1440
BackupSelection:
Type: 'AWS::Backup::BackupSelection'
Properties:
BackupPlanId: !Ref BackupPlan
BackupSelection:
SelectionName: 'efs'
IamRoleArn: !GetAtt 'BackupRole.Arn'
Resources:
- !GetAtt 'FileSystem.Arn'
##########################################################################
# #
# Monitoring EFS #
# #
##########################################################################
BurstCreditBalanceTooLowAlarm:
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmDescription: 'Average burst credit balance over last 10 minutes too low, expect a significant performance drop soon.'
Namespace: 'AWS/EFS'
MetricName: BurstCreditBalance
Statistic: Average
Period: 600
EvaluationPeriods: 1
ComparisonOperator: LessThanThreshold
Threshold: 192000000000 # 192 GB in Bytes (last ~30 minutes where you can burst at 100 MB/sec)
Dimensions:
- Name: FileSystemId
Value: !Ref FileSystem
PercentIOLimitTooHighAlarm:
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmDescription: 'I/O limit has been reached, consider using Max I/O performance mode.'
Namespace: 'AWS/EFS'
MetricName: PercentIOLimit
Statistic: Maximum
Period: 600
EvaluationPeriods: 3
ComparisonOperator: GreaterThanThreshold
Threshold: 95
Dimensions:
- Name: FileSystemId
Value: !Ref FileSystem
PermittedThroughputAlarm:
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmDescription: 'Reached 80% of the permitted throughput 6 times over last 10 minutes.'
Metrics:
- Id: m1
Label: MeteredIOBytes
MetricStat:
Metric:
Namespace: 'AWS/EFS'
MetricName: MeteredIOBytes
Dimensions:
- Name: FileSystemId
Value: !Ref FileSystem
Period: 60
Stat: Sum
Unit: Bytes
ReturnData: false
- Id: m2
Label: PermittedThroughput
MetricStat:
Metric:
Namespace: 'AWS/EFS'
MetricName: PermittedThroughput
Dimensions:
- Name: FileSystemId
Value: !Ref FileSystem
Period: 60
Stat: Sum
Unit: 'Bytes/Second'
ReturnData: false
- Expression: '(m1/1048576)/PERIOD(m1)'
Id: e1
Label: e1
ReturnData: false
- Expression: 'm2/1048576'
Id: e2
Label: e2
ReturnData: false
- Expression: '((e1)*100)/(e2)'
Id: e3
Label: 'Throughput utilization (%)'
ReturnData: true
EvaluationPeriods: 10
DatapointsToAlarm: 6
ComparisonOperator: GreaterThanThreshold
Threshold: 80
Outputs:
EC2InstanceA:
Value: !Ref EC2InstanceA
Description: 'Id of EC2 Instance in AZ A (connect via Session Manager)'
EC2InstanceB:
Value: !Ref EC2InstanceB
Description: 'Id of EC2 Instance in AZ B (connect via Session Manager)'