-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMICreate.js
74 lines (72 loc) · 2.61 KB
/
AMICreate.js
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
/**
* AWS Lambda function to delete AMI backup images and associated snapshots
* This script just creates AMIs and tags them
* Official documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#describeInstances-property
*/
var settings = {
'region': 'eu-west-2', // if you get an error like "region not found" or not supported remove the last letter the original region was eu-west-2a
'noreboot': true,
'ec2backuptagname': 'Backup', // Add this tag to EC2 instances you wish to backup.
'ec2backuptagvalue': 'yes',
'amideletetagname': 'DeleteOn', // This tag name must match the one in the delete_amis.js script.
'amideletetagvalue': 'yes'
}
var aws = require('aws-sdk')
aws.config.region = settings.region
var ec2 = new aws.EC2()
var now = new Date()
date = now.toISOString().substring(0, 10)
hours = now.getHours()
minutes = now.getMinutes()
exports.handler = function (event, context) {
var instanceparams = {
Filters: [{
Name: 'tag:' + settings.ec2backuptagname,
Values: [
settings.ec2backuptagvalue
]
}]
}
ec2.describeInstances(instanceparams, function (err, data) {
if (err) console.log(err, err.stack)
else {
for (var i in data.Reservations) {
for (var j in data.Reservations[i].Instances) {
instanceid = data.Reservations[i].Instances[j].InstanceId
nametag = data.Reservations[i].Instances[j].Tags
for (var k in data.Reservations[i].Instances[j].Tags) {
if (data.Reservations[i].Instances[j].Tags[k].Key == 'Name') {
name = data.Reservations[i].Instances[j].Tags[k].Value
}
}
console.log('Creating AMIs of the Instance: ', name)
//preparing the name of the AMI
var imageparams = {
InstanceId: instanceid,
Name: name + '_' + date + '_' + hours + '-' + minutes,
NoReboot: settings.noreboot
}
//AMI creation
ec2.createImage(imageparams, function (err, data) {
if (err) console.log(err, err.stack)
else {
image = data.ImageId
console.log(image)
var tagparams = {
Resources: [image],
Tags: [{
Key: settings.amideletetagname,
Value: settings.amideletetagvalue
}]
}
ec2.createTags(tagparams, function (err, data) {
if (err) console.log(err, err.stack)
else console.log('Tags added to the created AMIs')
})
}
})
}
}
}
})
}