vMX explores his surroundings in Amazon AWS

vMX explores his surroundings in Amazon AWS

Recently I started exploring ways in Junos to program IP routes in Amazon AWS EC2 using their REST API. Junos 16.1 not only comes with Python 2.7.x, but also with all the default modules and some extras, like PyEz: Understanding Python Automation Scripts for Devices Running Junos OS.

Metadata

Every virtual instance running in Amazon EC2 has access to metadata avilable via HTTP. The instance identity document provides some basic information about the instance itself.

To retrieve the instance identity document on Linux, one can use curl:

ubuntu@ip-10-5-0-47:~$ curl http://169.254.169.254/latest/dynamic/instance-identity/document
{
  "privateIp" : "10.5.0.47",
  "devpayProductCodes" : null,
  "availabilityZone" : "eu-west-1c",
  "version" : "2010-08-31",
  "instanceId" : "i-0d7be893281b9ab02",
  "billingProducts" : null,
  "instanceType" : "t2.micro",
  "accountId" : "123456789",
  "architecture" : "x86_64",
  "kernelId" : null,
  "ramdiskId" : null,
  "imageId" : "ami-a8d2d7ce",
  "pendingTime" : "2017-04-28T07:39:53Z",
  "region" : "eu-west-1"

Wouldn’t it be nice to get the same information also in Junos? Abosuletly doable with a 3 line Python op script:

  jnpr@vmx1> file show /var/db/scripts/op/aws-document.py
import urllib2

document = urllib2.urlopen("http://169.254.169.254/latest/dynamic/instance-identity/document").read()
print(document)

Combined with this Junos configuration:

jnpr@vmx1> show configuration system scripts
op {
    file aws-document.py;
}
language python;

And here is the result:

jnpr@vmx1> op aws-document.py
{
  "devpayProductCodes" : null,
  "availabilityZone" : "eu-west-1c",
  "privateIp" : "10.5.0.37",
  "version" : "2010-08-31",
  "instanceId" : "i-041b5a5f965f0a9aa",
  "billingProducts" : null,
  "instanceType" : "m4.2xlarge",
  "accountId" : "123456789",
  "architecture" : "x86_64",
  "kernelId" : null,
  "ramdiskId" : null,
  "imageId" : "ami-07172f61",
  "pendingTime" : "2017-04-28T07:37:29Z",
  "region" : "eu-west-1"
}

There is much more useful information available via metadata, including information about the virtual interfaces attached to the instance, their private and public IP addresses and attached VPC networks.
More about that in a subsequent blog post, promise!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: