Last active
January 23, 2024 17:11
-
-
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
This file contains 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 characters
#!/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}] \$ " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For info, now you need a token to access the metadata:
(also removed private ip from the prompt)