Automating Alerts for Unassociated Elastic IPs w/ AWS https://www.aaronmedacco.com/blog/post/2016/12/30/automating-alerts-for-unassociated-elastic-ips-w-aws
var AWS = require("aws-sdk");
exports.handler = function(event, context) {
var sns = new AWS.SNS();
var ec2 = new AWS.EC2();
var message = "The following Elastic IPs are not associated:\n\n";
var params = {};
ec2.describeAddresses(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
var unassociatedAddresses = 0;
for (var i = 0; i < data.Addresses.length; i++){
if (!data.Addresses[i].hasOwnProperty("InstanceId")){
console.log(data.Addresses[i].PublicIp);
unassociatedAddresses++;
message += " " + data.Addresses[i].PublicIp + "\n";
}
}
if (unassociatedAddresses > 0){
var publishParams = {
Message: message,
Subject: "Elastic IP Addresses Unassociated",
TopicArn: "Your Topic ARN"
};
sns.publish(publishParams, context.done);
}
}
});
};
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sns:Publish",
"sns:Subscribe"
],
"Resource": [
"Your Topic ARN"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:DescribeAddresses"
],
"Resource": [
"*"
]
}
]
}