Skip to content

Instantly share code, notes, and snippets.

@miglen
Last active January 23, 2024 17:11
Show Gist options
  • Save miglen/e2e577b95acf1171a1853871737323ce to your computer and use it in GitHub Desktop.
Save miglen/e2e577b95acf1171a1853871737323ce to your computer and use it in GitHub Desktop.
AWS EC2 Instance Prompt with EC2 ARN Instance Id Public IP Private IP Account Id Region and Instance Name Tag
#!/bin/bash
#
# description: EC2 Instance Prompt
# author: Miglen Evlogiev <[email protected]>
#
# deployment: copy this file into /etc/profile.d/ec2-instance-prompt.sh
# sudo wget https://gist.githubusercontent.com/miglen/e2e577b95acf1171a1853871737323ce/raw/ec2-instance-prompt.sh -P /etc/profile.d/
# sudo bash /etc/profile.d/ec2-instance-prompt.sh
#
# Obtain information from EC2 Metadata (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
aws_account_id=$(wget -qO- http://instance-data/latest/dynamic/instance-identity/document | sed -n 's/.*"accountId"\s*:\s*"\(.*\)".*/\1/p')
aws_instance_id=$(wget -qO- http://instance-data/latest/meta-data/instance-id)
aws_public_ip=$(wget -qO- http://instance-data/latest/meta-data/public-ipv4)
aws_private_ip=$(wget -qO- http://instance-data/latest/meta-data/local-ipv4)
aws_region=$(wget -qO- http://instance-data/latest/meta-data/placement/availability-zone | sed 's/.$//')
aws_instance_arn="arn::ec2:${aws_region}:${aws_account_id}:instance/${aws_instance_id}"
# Export the prompt
export PS1="[${aws_instance_arn}]-[${aws_public_ip}]-[${aws_private_ip}] \n[${USER}:${PWD}] \$ "
# Uncomment the following block to add EC2 Name Tag to the prompt
# Prerequisites:
# 1. Ensure you have AWS cli installed https://aws.amazon.com/cli/
# 2. Ensure your instance has instance profile with at least ec2:DescribeTags action allowed.
#
# aws_tag_name="Name"
# aws_instance_name=$(aws ec2 describe-tags --region ${aws_region} --filter "Name=resource-id,Values=${aws_instance_id}" "Name=key,Values=Name" --output text | cut -f5)
#
# export PS1="[${aws_instance_arn}]-[${aws_instance_name}]-[${aws_public_ip}]-[${aws_private_ip}] \n[${USER}:${PWD}] \$ "
@castortech
Copy link

I got into a small issue getting the instance name at line 28 since we had defined extra tags.

So I added "Name=key,Values=Name" to the filter to make sure that it only returns the instance name.

Thanks for the great script.

@miglen
Copy link
Author

miglen commented Feb 10, 2021

Thanks for using and the feedback @castortech I've added the filter!

@nicobao
Copy link

nicobao commented Jan 23, 2024

For info, now you need a token to access the metadata:

(also removed private ip from the prompt)

#!/bin/bash
# description: EC2 Instance Prompt
# author: Miglen Evlogiev <[email protected]>
#
# deployment: copy this file into /etc/profile.d/ec2-instance-prompt.sh
# sudo wget https://gist.githubusercontent.com/miglen/e2e577b95acf1171a1853871737323ce/raw/ec2-instance-prompt.sh -P /etc/profile.d/
# sudo bash /etc/profile.d/ec2-instance-prompt.sh
#

instance_data=169.254.169.254
TOKEN=`curl -X PUT "http://${instance_data}/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`

# Obtain information from EC2 Metadata (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
aws_account_id=$(wget -qO- --header="X-aws-ec2-metadata-token: $TOKEN" http://${instance_data}/latest/dynamic/instance-identity/document | sed -n 's/.*"accountId"\s*:\s*"\(.*\)".*/\1/p')
aws_instance_id=$(wget -qO- --header="X-aws-ec2-metadata-token: $TOKEN" http://${instance_data}/latest/meta-data/instance-id)
aws_public_ip=$(wget -qO- --header="X-aws-ec2-metadata-token: $TOKEN" http://${instance_data}/latest/meta-data/public-ipv4)
# aws_private_ip=$(wget -qO- --header="X-aws-ec2-metadata-token: $TOKEN" http://${instance_data}/latest/meta-data/local-ipv4)
aws_region=$(wget -qO-  --header="X-aws-ec2-metadata-token: $TOKEN" http://${instance_data}/latest/meta-data/placement/availability-zone | sed 's/.$//')
aws_instance_arn="arn::ec2:${aws_region}:${aws_account_id}:instance/${aws_instance_id}"

# Uncomment the following block to add EC2 Name Tag to the prompt
# Prerequisites:
# 1. Ensure you have AWS cli installed https://aws.amazon.com/cli/
# 2. Ensure your instance has instance profile with at least ec2:DescribeTags action allowed.
#
aws_tag_name="Name"
aws_instance_name=$(aws ec2 describe-tags --region ${aws_region} --filter "Name=resource-id,Values=${aws_instance_id}" "Name=key,Values=Name" --output text | cut -f5)

# Export the prompt
PS1="[${aws_instance_arn}]-[${aws_public_ip}-${aws_instance_name}]\n[${USER}:${PWD}]\$ "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment