Skip to content

Instantly share code, notes, and snippets.

@mikej165
Last active December 21, 2015 17:48
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 mikej165/6342543 to your computer and use it in GitHub Desktop.
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.
/**
* 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