我有多个aws帐户,我不记得此EC2实例是在哪个aws帐户中创建的,有没有什么最佳的方法可以在很短的时间内弄清楚呢?
注意:我需要知道帐户DNS名称或别名。(不是帐户号码)
答案 0 :(得分:0)
如果您有权访问该实例,则可以使用Instance metadata API:
[ec2-user ~]$ curl http://169.254.169.254/latest/dynamic/instance-identity/document
它返回带有accountId
字段的json。
答案 1 :(得分:0)
如果您为所有账户配置AWS CLI,则可以获得账户ID,ARN和用户ID。 该脚本执行以下操作。
./script.sh 52.x.x.x
script.sh
#!/bin/bash
INSTANCE_IP="${1}"
if [ -z "${INSTANCE_IP}" ]; then
echo "pls provide instance IP"
echo "./scipt.sh 54.x.x.x"
exit 1
fi
PROFILE_LIST=$(grep -o "\\[[^]]*]" < ~/.aws/credentials | tr -d "[]")
for PROFILE in $PROFILE_LIST; do
ALL_IPS=$(aws ec2 describe-instances --profile "${PROFILE}" --query "Reservations[].Instances[][PublicIpAddress]" --output text | tr '\r\n' ' ')
echo "looking against profile ${PROFILE}"
for IP in $ALL_IPS; do
if [ "${INSTANCE_IP}" == "${IP}" ]; then
echo "Instance IP matched in below account"
aws sts get-caller-identity
exit 0
fi
done
done
echo "seems like instance not belong to these profile"
echo "${PROFILE_LIST}"
exit 1