- Now let's take a look at a demo
of querying the instance metadata service.
Okay, so I'm at my terminal
and I'm going to shell back
into the instance
that we launched in,
shelled into earlier.
There we go.
So now we have a shell into our Amazon Linux instance.
Now the instance metadata service is available
at a very particular URL,
so we can visit that by doing a curl
to http
one six nine
dot two five four dot one six nine dot two five four
slash latest slash
meta dash data
and by curling that we will get a list of other resources,
different pieces of information that we can retrieve.
And so again, this particular URL is only available
from within an instance.
And so I can only use this from within an instance
to gain information about this one instance.
From this instance I cannot gain information about
other instances using instance metadata.
And so for example, let's try,
let's replay that and then we can go with the first one,
all of these would then just be additions to that URL.
So we could say, meta dash data slash ami id
and it would give us back the machine image id
that is currently in use on this instance.
We could do the same thing with something like,
host name, and you'll see here is the host name.
We could go with instance id as well.
Let's try instance id,
and there you go, there is the instance id.
And notice it's returned with as plain text,
and so we can use the instance metadata service
to right automated scripts, CLI scripts,
that can be used to boot trap this instance on boot.
So we could do something like this,
we could say, instance id equals
and we could copy
that URL
and paste it here
and by playing that
and now we can echo the instance id
and there you go.
So now we can do things like that,
we can capture things in environment variables
and use those in our scripts.
So there's a lot of information here.
There's more information about the network for example.
Let's take a look here at the network
and you'll see there are,
there's another level to that,
so we have to go network interfaces
and we keep on going, right,
it sometimes it keeps going
until finally you reach some information.
So there we go, now we have information
about the mac address.
And we could take a look at placement,
I believe placement would show us,
let's see what that shows us.
So then placement availability zone, right,
so let's copy that and then try that one.
And there you go, now it's showing us the availability zone.
So we could capture that, say, in a script
and then use that information somewhere else
in that script, right, so we may want to,
for example, capture the instance id
and the particular availability zone
and then use the availability zone to determine which
maybe if there's an EBS volume available,
and then use the instance id to as a call
to attach an EBS volume, or to update routing tables.
We can use information about the network to, say, update,
automatically update route tables, or firewall rules,
or things like that, right.
So there, again, lot of information available in this
particular list of possibilities to run
with the instance metadata service and the primary use case
for the instance metadata service is to automate
the bootstrapping of this machine.
So this machine can, you know, boot and then say
all right who am I, and where am I,
and what am I supposed to be doing, right.
So we can use those scripts to pull the instance id
and any other information that we need about this instance
in order to then go on and configure other things.