-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheb–cleanup-cloudformation.json
134 lines (133 loc) · 6.89 KB
/
eb–cleanup-cloudformation.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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Cleans up old EB versions",
"Parameters": {
"VersionLimit": {
"Type": "String",
"Default": "10"
},
"MemorySize": {
"Type": "String",
"Default": "128"
},
"Timeout": {
"Type": "String",
"Default": "60"
},
"Schedule": {
"Type": "String",
"Default": "rate(1 day)"
}
},
"Resources": {
"CleanupScheduleRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": {"Ref": "Schedule"},
"Targets": [
{
"Id": "CleanupVersions",
"Arn": {"Fn::GetAtt": ["CleanupVersions", "Arn"]}
}
]
}
},
"InvokeLambdaPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"FunctionName": {"Fn::GetAtt": ["CleanupVersions", "Arn"]},
"Action": "lambda:InvokeFunction",
"Principal": "events.amazonaws.com",
"SourceArn": { "Fn::GetAtt": ["CleanupScheduleRule", "Arn"]}
}
},
"CleanupVersions": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": {
"Fn::Join": ["", [
"import boto3\n",
"versionLimit = ", {"Ref": "VersionLimit"}, "\n",
"region = \"", {"Ref": "AWS::Region"}, "\"\n",
"def lambda_handler(event, context):\n",
" print '--- Cleaning up Beanstalk App Versions in region: %s - limit: %s' % (region, versionLimit)\n",
" ebClient = boto3.client('elasticbeanstalk', region_name=region)\n",
" for app in ebClient.describe_applications()['Applications']:\n",
" applicationName = app['ApplicationName']\n",
" print 'Cleaning application: %s' % applicationName\n",
" appVersions = ebClient.describe_application_versions(ApplicationName=applicationName)['ApplicationVersions']\n",
" print ' * Application Versions: %s' % len(appVersions)\n",
" sortedVersions = sorted(appVersions, key=lambda v: v['DateCreated'], reverse=True)\n",
" sortedVersionsCount = len(sortedVersions)\n",
" listDiff = sortedVersionsCount - versionLimit;\n",
" if (listDiff > 0):\n",
" versionsToDelete = [version['VersionLabel'] for version in sortedVersions[(sortedVersionsCount-listDiff):sortedVersionsCount]]\n",
" print ' *** Suggested Versions to Delete (%s): %s ' % (len(versionsToDelete), versionsToDelete)\n",
" environments = ebClient.describe_environments(ApplicationName=applicationName)['Environments']\n",
" deployedVersions = [environment['VersionLabel'] for environment in environments]\n",
" print ' *** Deployed Versions: %s' % deployedVersions\n",
" finalVersionsToDelete = set(versionsToDelete).difference(set(deployedVersions))\n",
" print ' *** Final Versions to delete (%s, excluding the ones that are deployed) : %s' % (len(finalVersionsToDelete), finalVersionsToDelete)\n",
" for version in finalVersionsToDelete:\n",
" print ' **** Deleting Version : %s ' % version\n",
" ebClient.delete_application_version(ApplicationName=applicationName,VersionLabel=version,DeleteSourceBundle=True)\n",
" print ' *** Application Versions deleted: %s, bye' % len(finalVersionsToDelete)\n",
" else:\n",
" print ' *** Application Versions number %s is lower than the limit %s, no need to clean up, bye.' % (sortedVersionsCount, versionLimit)\n",
" print '------------------------------------------------------------------------------'\n"
]]
}
},
"FunctionName": "CleanupBeanstalkAppVersions",
"Handler": "index.lambda_handler",
"MemorySize": {"Ref": "MemorySize"},
"Role": { "Fn::GetAtt": ["IamRole", "Arn"]},
"Runtime": "python2.7",
"Timeout": {"Ref": "Timeout"}
}
},
"IamRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [{ "Effect": "Allow", "Principal": {"Service": ["lambda.amazonaws.com"]}, "Action": ["sts:AssumeRole"] }]
},
"Path": "/",
"Policies": [
{
"PolicyName": "root",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"elasticbeanstalk:DescribeApplications",
"elasticbeanstalk:DescribeApplicationVersions",
"elasticbeanstalk:DescribeEnvironments",
"elasticbeanstalk:DeleteApplicationVersion",
"s3:GetObject",
"s3:ListBucket",
"s3:DeleteObject"
],
"Resource": "*"
}
]
}
}
]
}
}
}
}