Skip to content

Instantly share code, notes, and snippets.

@daorte
Created September 11, 2019 21:15
Show Gist options
  • Save daorte/27ad8d39a04b112ed72624a3c1a504db to your computer and use it in GitHub Desktop.
Save daorte/27ad8d39a04b112ed72624a3c1a504db to your computer and use it in GitHub Desktop.
********************************
StartStopEC2
********************************
var AWS = require('aws-sdk');
exports.handler = function(event, context, callback) {
var isStartOperation = event.operation == 'start';
var region = 'us-west-2';
var EC2 = new AWS.EC2({
region: region
});
var params = {
Filters: [{
Name: 'tag:ENV',
Values: ['PRE']
}]
};
var errorMessage = '';
if (isStartOperation) {
params.Filters.push({
Name: 'instance-state-name',
Values: ['stopped', 'stopping']
});
} else {
params.Filters.push({
Name: 'instance-state-name',
Values: ['running', 'pending']
});
}
EC2.describeInstances(params, function(err, data) {
if (err) {
errorMessage = "Error connecting, No such instance(s) found!";
console.log(errorMessage);
callback(err, errorMessage);
}
var ids = [];
data.Reservations.forEach(function(reservation) {
reservation.Instances.forEach(function(instance) {
ids.push(instance.InstanceId);
});
});
if (ids.length > 0) {
var params = {
InstanceIds: ids
};
if (isStartOperation) {
EC2.startInstances(params, function(err, data) {
if (err) {
errorMessage = "OOps! Instance(s) could not be started";
console.log(errorMessage);
callback(err, errorMessage);
} else {
console.log("EC2 Instances started successfully");
console.log(JSON.stringify(data, null, 4));
}
});
} else {
EC2.stopInstances(params, function(err, data) {
if (err) {
errorMessage = "OOps! Instance(s) could not be stopped";
console.log(errorMessage);
callback(err, errorMessage);
} else {
console.log("EC2 Instances stopped successfully");
console.log(JSON.stringify(data, null, 4));
}
});
}
} else {
console.log("OOps! There is no instance(s) matching the search filter");
}
});
};
********************************
StartStopRDS
********************************
var AWS = require('aws-sdk');
exports.handler = function(event, context, callback) {
var isStartOperation = event.operation == 'start';
var region = 'eu-west-1';
var RDS = new AWS.RDS({
region: region
});
var errorMessage = '';
RDS.describeDBInstances({}, function(rdsInstancesErr, rdsInstancesData) {
if (rdsInstancesErr) {
errorMessage = "Error connecting, No such instance(s) found!";
console.log(errorMessage);
callback(rdsInstancesErr, errorMessage);
}
rdsInstancesData.DBInstances.forEach(function(dbInstance) {
var rdsInstanceParam = {
ResourceName: dbInstance.DBInstanceArn
}
RDS.listTagsForResource(rdsInstanceParam, function(tagErr, tagData) {
if (tagErr) {
errorMessage = "OOps! Instance(s) tags could not be found";
console.log(errorMessage);
callback(tagErr, errorMessage);
}
var tags = tagData.TagList || [];
tags.forEach(function(tag) {
console.log("INSTANCE [" + dbInstance.DBInstanceIdentifier + "] TAG KEY [" + tag.Key + "] TAG VAL [" + tag.Value + "]");
if (tag.Key == 'ENV' && tag.Value == 'PRE') {
var dbInstanceParams = {
DBInstanceIdentifier: dbInstance.DBInstanceIdentifier
};
var dbInstanceStatus = dbInstance.DBInstanceStatus;
if (isStartOperation && dbInstanceStatus == 'stopped') {
RDS.startDBInstance(dbInstanceParams, function(startErr, startData) {
if (startErr) {
errorMessage = "OOps! Instance(s) could not be started";
console.log(errorMessage);
callback(startErr, errorMessage);
} else {
console.log("RDS Instances started successfully");
console.log(JSON.stringify(startData));
callback(null, startData);
}
});
} else if (!isStartOperation && dbInstanceStatus == 'available') {
RDS.stopDBInstance(dbInstanceParams, function(stopErr, stopData) {
if (stopErr) {
errorMessage = "OOps! Instance(s) could not be stopped";
console.log(errorMessage);
callback(stopErr, errorMessage);
} else {
console.log("RDS Instances stopped successfully");
console.log(JSON.stringify(stopData));
callback(null, stopData);
}
});
}
}
});
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment