forked from anupam-ncsu/JenkinsECS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LambdaSnapshooter.yaml
108 lines (94 loc) · 3.96 KB
/
LambdaSnapshooter.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
AWSTemplateFormatVersion: '2010-09-09'
Description: 'CFN template to create a lambda and a event to trigger lambda at a certain frequency'
Parameters:
EnvName:
Type: String
Description: 'Name of an environment. ''dev'', ''staging'', ''prod'' and any name.'
Default: dev
LambdaServiceRole:
Type: String
Description: 'role of lambda for snapshotting'
Default: 'arn:aws:iam::952408268520:role/service-role/abs-snapshot-role-xznsj5je'
VolumeID:
Type: String
Description: 'Volume to be snapshotted'
Default: vol-06ce286891e0e83a4
rateOfMakingSnapshot:
Type: String
Description: 'what is the frequency of making snapshots'
Default: 'rate(1 minute)'
SnapshotRegion:
Type: String
Description: ' Region in which your EBS snapshot is stored'
Default: 'ca-central-1'
applicationName:
Type: String
Description: 'Name of the application to tag the volume snapshot with'
Default: 'JenkinsMasterVolumeSnapshotBackup'
Resources:
ScheduledRuleForSnapshotCreation:
DependsOn: LambdaFunctiontoCreateSnapshot
Type: AWS::Events::Rule
Properties:
Description: "ScheduledRule for LambdaFunctiontoCreateSnapshot trigger"
ScheduleExpression: !Ref rateOfMakingSnapshot
State: "ENABLED"
Targets:
- Arn: !GetAtt
- LambdaFunctiontoCreateSnapshot
- Arn
Id: LambdaFunctiontoCreateSnapshot
PermissionForEventsToInvokeSnapshotCreationLambda:
DependsOn: ScheduledRuleForSnapshotCreation
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref LambdaFunctiontoCreateSnapshot
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "ScheduledRuleForSnapshotCreation"
- "Arn"
LambdaFunctiontoCreateSnapshot:
Type: AWS::Lambda::Function
Properties:
FunctionName:
Fn::Sub: EBS-snapshooter-lambda-function-${EnvName}
Runtime: python2.7
Code:
ZipFile: |
import boto3;
import datetime;
import os;
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
ts = datetime.datetime.now()
volumeID = os.environ['volumeID'] #"vol-06ce286891e0e83a4"
applicationName = os.environ['applicationName']
reg = "ca-central-1"
print "Backing up Jenkins Cluster state from %s at %s" % ( volumeID , datetime.datetime.now())
# do snapshot and tag it !
result = ec2.create_snapshot(
VolumeId=volumeID,
Description='Snapshot Jenkins Master State for '+ts.strftime("%m/%d/%Y, %H:%M:%S"),
TagSpecifications=[{
'ResourceType': 'snapshot',
'Tags': [{
'Key': 'Application',
'Value': applicationName },
{
'Key': 'Creation Time',
'Value': ts.strftime("%m/%d/%Y, %H:%M:%S") }]
}]
)
# Get snapshot resource
snapshotId = result['SnapshotId']
print "Snapshot id %s" % (snapshotId)
Handler: index.lambda_handler
MemorySize: 128
Timeout: 10
Role: !Ref LambdaServiceRole
Environment:
Variables:
volumeID: !Ref VolumeID
applicationName: !Ref applicationName