-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemplate.json
147 lines (147 loc) · 4.22 KB
/
template.json
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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS Snapshot Cleanup",
"Parameters": {
"LambdaS3Bucket": {
"Type": "String",
"Description": "Bucket where the Lambda code is located"
},
"LambdaS3Key": {
"Type": "String",
"Description": "Path to Lambda code",
"Default": "lambda.zip"
},
"LambdaS3ObjectVersion": {
"Type": "String",
"Description": "Leave blank if you don't have versioning enabled on LambdaS3Bucket"
},
"AlarmTopicParameter": {
"Description": "ARN of SNS topic to send CloudWatch alarms",
"Type": "String"
}
},
"Conditions": {
"VersioningEnabled": {"Fn::Not": [{"Fn::Equals" : [{"Ref" : "LambdaS3ObjectVersion"}, ""]}]}
},
"Resources": {
"LambdaRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": ["sts:AssumeRole"]
}]
},
"Path": "/",
"Policies": [{
"PolicyName": "logs",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"logs:*"
],
"Resource": "arn:aws:logs:*:*:*"
}]
}
}, {
"PolicyName": "ec2",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Sid": "Stmt1424083772000",
"Effect": "Allow",
"Action": [
"ec2:DescribeSnapshots",
"ec2:DeleteSnapshot"
],
"Resource": [
"*"
]
}]
}
}]
}
},
"Cron": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(1 hour)",
"State": "ENABLED",
"Targets": [{
"Arn": {"Fn::GetAtt": ["Lambda", "Arn"]},
"Id": "Lambda"
}]
}
},
"CronLambdaPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"FunctionName": {"Fn::GetAtt": ["Lambda", "Arn"]},
"SourceArn": {"Fn::GetAtt": ["Cron", "Arn"]},
"Principal": "events.amazonaws.com"
}
},
"Lambda": {
"Type" : "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": {"Ref": "LambdaS3Bucket"},
"S3Key": {"Ref": "LambdaS3Key"},
"S3ObjectVersion": {"Fn::If": ["VersioningEnabled", {"Ref": "LambdaS3ObjectVersion"}, {"Ref": "AWS::NoValue"}]}
},
"Description": "AWS Snapshot Cleanup",
"Handler": "index.handler",
"MemorySize": 128,
"Role": {"Fn::GetAtt": ["LambdaRole", "Arn"]},
"Runtime": "nodejs6.10",
"Timeout": 60
}
},
"LambdaErrorsAlarm": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Alarm if executions fail",
"Namespace": "AWS/Lambda",
"MetricName": "Errors",
"Dimensions": [{
"Name": "FunctionName",
"Value": {"Ref": "Lambda"}
}],
"Statistic": "Sum",
"Period": "60",
"EvaluationPeriods": "1",
"Threshold": "1",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"AlarmActions": [{"Ref": "AlarmTopicParameter"}],
"OKActions": [{"Ref": "AlarmTopicParameter"}]
}
},
"LambdaThrottlesAlarm": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Alarm if executions are throttled",
"Namespace": "AWS/Lambda",
"MetricName": "Throttles",
"Dimensions": [{
"Name": "FunctionName",
"Value": {"Ref": "Lambda"}
}],
"Statistic": "Sum",
"Period": "60",
"EvaluationPeriods": "1",
"Threshold": "1",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"AlarmActions": [{"Ref": "AlarmTopicParameter"}],
"OKActions": [{"Ref": "AlarmTopicParameter"}]
}
}
}
}