Library to fetch aws metadata for the application running on EC2 instances in AWS
Instance metadata is available from your running instance, you do not need to use the Amazon EC2 console or the AWS CLI. This can be helpful when you're writing scripts to run from your instance. For example, you can access the local IP address of your instance from instance metadata to manage a connection to an external application.
Instance metadata is divided into categories. For a description of each instance metadata category, see Instance metadata categories below.
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 12 or higher is required.
npm install --save node-aws-metadata
-
ami-id
-
ami-launch-index
-
ami-manifest-path
-
hostname
-
instance-action
-
instance-id
-
instance-type
-
local-hostname
-
local-ipv4
-
profile
-
public-hostname
-
public-ipv4
-
placement/availability-zone
-
placement/availability-zone-id
-
placement/region
const AWSMetaData = require('node-aws-metadata');
// To Get Instance Meta Data by a meta data category
const getInstanceData = async() =>{
try{
const _aws_metadata = new AWSMetaData();
const metadata = await _aws_metadata.getInstanceMetaData('ami-id');
console.log(metadata);
}
catch(err=>{
console.log(err);
})
}
// To Get Instance Avaliable Meta Data
const getInstanceAllMetaData = async() =>{
try{
const _aws_metadata = new AWSMetaData();
const metadata = await _aws_metadata.getInstanceAvailableMetaData();
console.log(metadata);
}
catch(err=>{
console.log(err);
})
}
Output displayed below will have same format, content might defer below content for response is not shown.
getInstanceMetaData('ami-id') :
{
'ami-id': 'amazon-linux-image-id'
}
getInstanceAvailableMetaData()
{
'ami-id': '',
'ami-launch-index': ,
'ami-manifest-path': '',
'hostname': '',
'instance-action': '',
'instance-id': '',
'instance-type': 't2.micro',
'local-hostname': '',
'local-ipv4': '',
'profile': '',
'public-hostname': '',
'public-ipv4': '',
'placement/availability-zone': 'us-west-1',
'placement/availability-zone-id': '',
'placement/region': 'us-west-1'
}
ISC © NiteshVishwakarma896