Skip to content

Commit ed260d5

Browse files
committed
Clean ecr images by schedule
1 parent 86efecd commit ed260d5

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-2
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ sls invoke -f notifyToSlack -l
3636
* [Angular2 app](https://github.com/springboot-angular2-tutorial/angular2-app)
3737
* [Spring Boot app](https://github.com/springboot-angular2-tutorial/boot-app)
3838
* [Android app](https://github.com/springboot-angular2-tutorial/android-app)
39-
* [Server provisioning by Ansible and Packer](https://github.com/springboot-angular2-tutorial/micropost-provisionings)
4039
* [Infrastructure by Terraform](https://github.com/springboot-angular2-tutorial/micropost-formation)
4140

4241
## License

functions/clean_ecr_images.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const cleanEcrImages = require('./clean_ecr_images/index');
2+
3+
module.exports.handle = (event, context, callback) => {
4+
5+
cleanEcrImages(event).then(() => {
6+
callback(null, {});
7+
}).catch(e => {
8+
console.error(e);
9+
});
10+
11+
};

functions/clean_ecr_images/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const AWS = require('aws-sdk');
2+
const _ = require('lodash');
3+
4+
const ecr = new AWS.ECR();
5+
6+
module.exports = function (params) {
7+
const repositoryName = params.repositoryName;
8+
const age = params.age || 2;
9+
10+
return ecr.describeImages({repositoryName: repositoryName})
11+
.promise()
12+
.then(data => findImagesToClear(data.imageDetails, age))
13+
.then(imagesToClear => deleteImages(imagesToClear, repositoryName))
14+
.then(data => console.log(data))
15+
;
16+
};
17+
18+
function findImagesToClear(images, age) {
19+
return _.chain(images)
20+
.sortBy('imagePushedAt')
21+
.take(images.length - age)
22+
.value();
23+
}
24+
25+
function deleteImages(images, repositoryName) {
26+
const imageIds = images.map(i => _.pick(i, 'imageDigest'));
27+
28+
return ecr.batchDeleteImage({
29+
imageIds: imageIds,
30+
repositoryName: repositoryName,
31+
}).promise();
32+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"dependencies": {
33
"@slack/client": "^3.6.0",
44
"dotenv": "^2.0.0",
5+
"lodash": "^4.17.2",
56
"serverless-plugin-write-env-vars": "^1.0.1"
67
},
78
"devDependencies": {

serverless.yml

+21
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,33 @@ provider:
55
runtime: nodejs4.3
66
region: ap-northeast-1
77
stage: ${opt:stage, self:custom.defaultStage}
8+
iamRoleStatements:
9+
- Effect: "Allow"
10+
Action:
11+
- "ecr:DescribeImages"
12+
- "ecr:BatchDeleteImage"
13+
Resource: "*"
814

915
functions:
1016
notifyToSlack:
1117
handler: functions/notify_to_slack.handle
1218
events:
1319
- sns: web_autoscaled
20+
cleanEcrImages:
21+
handler: functions/clean_ecr_images.handle
22+
events:
23+
- schedule:
24+
rate: rate(24 hours)
25+
enabled: true
26+
input:
27+
age: 2
28+
repositoryName: 'micropost/frontend'
29+
- schedule:
30+
rate: rate(1 minute)
31+
enabled: true
32+
input:
33+
age: 2
34+
repositoryName: 'micropost/backend'
1435

1536
custom:
1637
defaultStage: stg

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ jsprim@^1.2.2:
344344
json-schema "0.2.3"
345345
verror "1.3.6"
346346

347-
lodash@^4.13.1:
347+
lodash@^4.13.1, lodash@^4.17.2:
348348
version "4.17.2"
349349
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"
350350

0 commit comments

Comments
 (0)