Skip to content

Instantly share code, notes, and snippets.

@swithrow
Last active August 18, 2023 15:16
Show Gist options
  • Select an option

  • Save swithrow/05add0d405f7cce18265 to your computer and use it in GitHub Desktop.

Select an option

Save swithrow/05add0d405f7cce18265 to your computer and use it in GitHub Desktop.

Revisions

  1. Sam Withrow renamed this gist May 29, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.txt → instance-name-tag.sh
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,10 @@
    #
    # copy this into /etc/profile.d/instance-name-tag.sh
    #
    # you will need:
    # - curl, jq, and aws cli installed
    # - an IAM role that gives the EC2 instance access to describe tags
    #

    ## get instance info
    INSTANCE=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|jq .instanceId|tr -d '"')
  2. Sam Withrow created this gist May 29, 2014.
    18 changes: 18 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/bin/bash

    #
    # copy this into /etc/profile.d/instance-name-tag.sh
    #

    ## get instance info
    INSTANCE=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|jq .instanceId|tr -d '"')

    ## use region the instance is in as default region
    export AWS_DEFAULT_REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|jq .region|tr -d '"')

    ## make sure aws stuff is in the path
    PATH="/opt/aws/bin:$PATH"

    ## grab the Name tag for this instance
    NAMETAG=$(aws ec2 describe-tags --filters Name=resource-id,Values=$INSTANCE Name=key,Values=Name|jq ".Tags[0].Value"|tr -d '"')
    export PS1="\\t [\\u@${NAMETAG} \\W]\\\$ "