Forked from quiver/retrieve-EC2-region-information-from-metadata.md
Created
December 10, 2020 21:53
-
-
Save vdt/e828a977951383f23f0680e67d1f900d to your computer and use it in GitHub Desktop.
Revisions
-
quiver revised this gist
Mar 19, 2016 . 1 changed file with 7 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -68,10 +68,12 @@ $ cat ~/.aws/config region = ap-northeast-1 ``` ### UserData To configure API region, just add following code to you UserData. ``` #!/bin/bash yum install -y jq REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq .region -r` sudo -u ec2-user aws configure set region $REGION ``` -
quiver revised this gist
Nov 3, 2015 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -67,3 +67,11 @@ $ cat ~/.aws/config [default] region = ap-northeast-1 ``` ### cloud-init ``` packages: - jq runcmd: - [sudo, -u, ec2-user, sh, -c, 'aws configure set region `curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region`'] ``` -
quiver renamed this gist
Nov 3, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
quiver created this gist
Nov 3, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ Sometimes you want to retrieve EC2 insntances' region information. You can query that information through instance metadata(169.254.169.254). ``` $ curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document { "privateIp" : "172.31.2.15", "instanceId" : "i-12341ee8", "billingProducts" : null, "instanceType" : "t2.small", "accountId" : "1234567890", "pendingTime" : "2015-11-03T03:09:54Z", "imageId" : "ami-383c1956", "kernelId" : null, "ramdiskId" : null, "architecture" : "x86_64", "region" : "ap-northeast-1", # <- region "version" : "2010-08-31", "availabilityZone" : "ap-northeast-1c", "devpayProductCodes" : null } ``` Response's JSON has a `region` key, so if you just want to get region, filter the key with `jq`. ``` $ sudo yum install -y jq $ curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region ap-northeast-1 ``` ## Usecase : set aws-cli's region Even if you launch an EC2 instance with IAM role, you can't run commands without supplying region info. Otherwise, you'll encounter errors as follows. ``` $ aws ec2 describe-vpcs You must specify a region. You can also configure your region by running "aws configure". ``` Here's how to set aws-cli's region. First, check the curren settings. ``` $ aws configure list Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************GVKA iam-role secret_key ****************bll+ iam-role region <not set> None None ``` `region` is not set. Now pass metadata's region to `aws configure set region` command. ``` $ aws configure set region `curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region` $ aws configure get region ap-northeast-1 $ aws configure list Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************GVKA iam-role secret_key ****************bll+ iam-role region ap-northeast-1 config-file ~/.aws/config $ cat ~/.aws/config [default] region = ap-northeast-1 ```