10

I have an EC2 instance and I am running a Powershell script there where I would like to get the region that the EC2 is running in.

Currently I have workaround like this which grabs the availability zone first. The availability zone is in the format like 'us-east-1a'.

$region = invoke-restmethod -uri http://169.254.169.254/latest/meta-data/placement/availability-zone 
if ($region -like "*east*") {$region = "us-east-1"} ELSE {$region = "us-west-2"} 

I would like to just grab the region, rather than get the availability zone and then do some modifications. I know there is a possibility to use:

http://169.254.169.254/latest/dynamic/instance-identity/document

This returns a JSON object which has the region, but I would also need to parse the JSON to achieve this.

How do I get just the region?

5 Answers 5

9

You can use ConvertFrom-Json :

PS C:\> $region = (Invoke-WebRequest -UseBasicParsing -Uri http://169.254.169.254/latest/dynamic/instance-identity/document | ConvertFrom-Json | Select region).region

edit: added -UseBasicParsing

1
  • 1
    Simplified to (Invoke-RestMethod http://169.254.169.254/latest/dynamic/instance-identity/document).region
    – Stoinov
    Commented Mar 13, 2020 at 14:30
6

Will this work?

PS C:\> $region = invoke-restmethod -uri http://169.254.169.254/latest/meta-data/placement/availability-zone

PS C:\> $region.Substring(0,$region.Length-1)
1
  • Yea, that solution will work since it seems that all the availability zones have a single letter tacked onto the end. Discovered there's no native way to grab the region with a single command. Thank you. Commented Aug 22, 2017 at 18:13
4

Parsing the availability-zone is not the safest way. Region name is available as an attribute of the Instance Identity Document, which is generated when the instance is launched. There are two options to read this information with Powershell:

You could use Invoke-WebRequest:

IWR (also aliased as curl and wget) works fine, but it can only deal with HTML. So you need an extra step to parse JSON. It uses the IE COM interface to parse the DOM by default, but you can avoid that with the -UseBasicParsing option.

PS C:\> curl http://169.254.169.254/latest/dynamic/instance-identity/document | ConvertFrom-Json | Select region

region
------
us-east-1

PS C:\> (curl http://169.254.169.254/latest/dynamic/instance-identity/document | ConvertFrom-Json).region
us-east-1

But Invoke-RestMethod is the best option:

Since this is a REST interface, IRM is the best choice because it natively supports JSON and XML.

PS C:\> irm http://169.254.169.254/latest/dynamic/instance-identity/document | Select region

region
------
us-east-1

PS C:\> (irm http://169.254.169.254/latest/dynamic/instance-identity/document).region
us-east-1

PS C:\> irm http://169.254.169.254/latest/dynamic/instance-identity/document | % region
us-east-1
1

Try using :

EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"

OR

EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"test -n "$EC2_INSTANCE_ID" || die 'cannot obtain instance-id'

EC2_AVAIL_ZONE="`wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone || die \"wget availability-zone has failed: $?\"`"test -n "$EC2_AVAIL_ZONE" || die 'cannot obtain availability-zone'

EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"

JQ:

curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq .region -r

Reference : Instance Metadata

Note: This has to be run from inside the EC2 instance because that IP is an APIPA. There is no way to get this information directly from inside the instance without connecting to a metadata source

Hope it helps

3
  • Looks like those are Linux commands... was wondering how I can do the same in Powershell? Commented Aug 22, 2017 at 17:52
  • 1
    The OP clearly says the solution needed is for powershell
    – helloV
    Commented Aug 22, 2017 at 18:11
  • @helloV: My bad. Invoke-webrequest will do that instead of curl :) Commented Aug 23, 2017 at 2:47
0

$region = Get-EC2InstanceMetadata -Category region

write-output "Get region:"

write-output $region.SystemName

2
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Commented Mar 3, 2022 at 14:59
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 4, 2022 at 2:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.