Last active
December 21, 2015 17:48
-
-
Save mikej165/6342543 to your computer and use it in GitHub Desktop.
Remapping an EC2 instance to an elastic IP in node.js. Useful for restarts and reboots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author: Michael R. Johnston | |
* Date: 8/26/13 | |
* Time: 9:34 AM | |
*/ | |
var AWS = require('aws-sdk'); | |
var request = require('request'); | |
// Fill in your info here | |
AWS.config = { | |
"accessKeyId": "XXXXXXXXXXXXXXXXXXXXX", | |
"secretAccessKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX", | |
"region": "us-east-1", | |
"maxRedirects": 10 | |
}; | |
var ec2 = new AWS.EC2(); | |
// Change to the elastic IP you're assigning | |
var PublicIp = '54.221.243.199'; | |
// Grab the instanceId for this instance | |
request.get('http://169.254.169.254/latest/meta-data/instance-id',function(err,response, instanceId){ | |
if(!err){ | |
// Remove 'DryRun' when putting into production | |
var params = { | |
"DryRun": true, | |
"InstanceId": instanceId, | |
"PublicIp": PublicIp | |
}; | |
ec2.associateAddress(params,function(err,data){ | |
if(!err){ | |
console.log('Address assigned: %s', JSON.stringify(data)); | |
} else { | |
console.log('Error associating address to instance: %s', err); | |
} | |
}); | |
} else { | |
console.log('Unable to obtain instance id'); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment