Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Created June 25, 2019 14:14
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 loretoparisi/dcda5d843adb01193afe6da28b244d46 to your computer and use it in GitHub Desktop.
Save loretoparisi/dcda5d843adb01193afe6da28b244d46 to your computer and use it in GitHub Desktop.
Get Docker Host IP Address for EC2 instances
const cp = require('child_process');
const ec2 = function (callback) {
const URL = 'http://169.254.169.254/latest/meta-data/local-ipv4';
// we make it silent and timeout to 1 sec
const args = [URL, '-s', '--max-time', '1'];
const opts = {};
cp.execFile('curl', args, opts, (error, stdout) => {
if (error) return callback(new Error('ec2 ip error'));
else return callback(null, stdout);
})
.on('error', (error) => callback(new Error('ec2 ip error')));
}//ec2
@loretoparisi
Copy link
Author

ec2(function(err, ip) {
        if(err) console.log(err)
        else console.log(ip);
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment