Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vdt/e828a977951383f23f0680e67d1f900d to your computer and use it in GitHub Desktop.
Save vdt/e828a977951383f23f0680e67d1f900d to your computer and use it in GitHub Desktop.

Revisions

  1. @quiver quiver revised this gist Mar 19, 2016. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions retrieve-EC2-region-information-from-metadata.md
    Original file line number Diff line number Diff line change
    @@ -68,10 +68,12 @@ $ cat ~/.aws/config
    region = ap-northeast-1
    ```

    ### cloud-init
    ### UserData
    To configure API region, just add following code to you UserData.

    ```
    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`']
    #!/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
    ```
  2. @quiver quiver revised this gist Nov 3, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions retrieve-EC2-region-information-from-metadata.md
    Original 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`']
    ```
  3. @quiver quiver renamed this gist Nov 3, 2015. 1 changed file with 0 additions and 0 deletions.
  4. @quiver quiver created this gist Nov 3, 2015.
    69 changes: 69 additions & 0 deletions README.md
    Original 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
    ```