Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmkale/b2bc585140f60cb06dcd020c0c818d87 to your computer and use it in GitHub Desktop.
Save kmkale/b2bc585140f60cb06dcd020c0c818d87 to your computer and use it in GitHub Desktop.
handler Function for aws-process-default-sg-security-rules
const aws = require('aws-sdk');
const ec2 = new aws.EC2();
const lambda = new aws.Lambda();
exports.handler = (event, context, callback) => {
if (!(event.regions)) {
//lets get the regions
ec2.describeRegions({}, function(regionerr, region_data) {
if (regionerr) {
console.log("Error from ec2.describeRegions: ", regionerr);
callback(regionerr);
} else {
console.log("Data from ec2.describeRegions: " + JSON.stringify(region_data));
event.regions = region_data.Regions;
//lets process one region per iteration
var thisregion = event.regions.pop();
deleteRulesFromDefaultSG(thisregion.RegionName, function(err, data) {
if (err) {
console.log("Error from deleteRulesFromDefaultSG: ", err);
callback(err);
} else {
console.log("Data from deleteRulesFromDefaultSG: ", data);
//lets invoke this function with the event object
//since we have poped a region from it, each iteration will process
//one region. After all are processed the function will exit in the
//first if (event.regions.length === 0) block
//lets call this function recursively till we process all regions
invokeLambda(event, "process-security-rules", callback);
}
});
}
});
} else if (event.regions.length === 0) {
//looks like we have processed all regions
//lets callback success
console.log("regions.length === 0 calling back success");
callback(null, "SUCCESS");
} else {
//lets process one region per iteration
var thisregion = event.regions.pop();
deleteRulesFromDefaultSG(thisregion.RegionName, function(err, data) {
if (err) {
console.log("Error from deleteRulesFromDefaultSG: ", err);
callback(err);
} else {
console.log("Data from deleteRulesFromDefaultSG: ", data);
//lets invoke this function with the event object
//since we have poped a region from it, each iteration will process
//one region. After all are processed the function will exit in the
//first if (event.regions.length === 0) block
//lets call this function recursively till we process all regions
invokeLambda(event, "process-security-rules", callback);
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment