简体   繁体   English

仅使用实例 ID SSH 到 Amazon EC2

[英]SSH to Amazon EC2 using instance ID only

I would like to turn on/off some of my Amazon EC2 instance, but this causes the IP and all DNS names to change.我想打开/关闭我的一些 Amazon EC2 实例,但这会导致 IP 和所有 DNS 名称发生更改。 Therefore when I boot my machine again, all my SSH configurations are lost since I was connecting using the previous DNS name.因此,当我再次启动我的机器时,我所有的 SSH 配置都丢失了,因为我使用以前的 DNS 名称进行连接。

Is there a simple way to resolve the dns of the target machine (with no or as low as possible cost) using only its instance-id (or any other parameter that do not change over shutdowns-restarts) ?是否有一种简单的方法来解析目标机器的 dns(没有或尽可能低的成本)仅使用其实例 ID(或任何其他不会因关机重启而改变的参数)? Do I have to use the AWS-CLI ?我必须使用 AWS-CLI 吗? What if I want to provide an access to an EC2 machine to someone who doesn't have AWS credentials ?如果我想向没有 AWS 凭证的人提供对 EC2 机器的访问权限,该怎么办?

Not sure if tags like "service-discovery", "broker", or "proxy" would really make sense here, but for the sake of references I'm adding them in my post.不确定像“服务发现”、“经纪人”或“代理”这样的标签在这里是否真的有意义,但为了参考起见,我将它们添加到我的帖子中。

I do not want to pay for elastic IPs.我不想为弹性 IP 付费。

There are already Utils that do this, no need to roll your own: ec2ssh is a python script to do it but a google for ec2ssh will turn up numerous similar tools in multiple languages that will do the job.已经有 Utils 可以做到这一点,无需自己动手: ec2ssh是一个 Python 脚本来执行此操作,但是ec2ssh的 google 将提供ec2ssh许多类似工具来完成这项工作。

Personally, I setup a bastion with an EIP, and jump from there to all the other hosts.就个人而言,我设置了一个带有 EIP 的堡垒,然后从那里跳转到所有其他主机。 This way you don't need to give your Instances public IPs just for admin access.这样,您就无需仅为管理员访问权限而提供您的实例公共 IP。 If your not transferring large files you can get away with a t2.nano as the bastion instance, which with a reservation costs you peanuts a month.如果您不传输大文件,您可以使用 t2.nano 作为堡垒实例,如果预订,您需要花费一个月的费用。

Ec2ssh has bastion support so the config overhead is minimal. Ec2ssh 具有堡垒支持,因此配置开销最小。

Building off of @jarmod 's comment, if you know the instance ID then you can use the awscli to get the IP like this:根据@jarmod 的评论构建,如果您知道实例 ID,那么您可以使用 awscli 获取 IP,如下所示:

aws ec2 describe-addresses --filters Name=instance-id,Values=i-xxxxxxxxxxxxxxxxx | grep PublicIp | awk -F\\" '{print $4}'

That being said, I'm not sure why you think that Elastic IPs are expensive.话虽如此,我不确定您为什么认为弹性 IP 很昂贵。 Per the Pricing page , Elastic IPs are free when associated with running instances, and $0.005/hour when not associated with an instance.根据定价页面,弹性 IP 在与正在运行的实例相关联时免费,如果与实例无关,则为 0.005 美元/小时。

EC2 elastic IP is exactly what you want. EC2 弹性 IP 正是您想要的。 Instance will retain it's elastic IP (and associated DNS-name) during reboots and start/stop cycles.实例将在重新启动和启动/停止周期期间保留其弹性 IP(和关联的 DNS 名称)。 https://aws.amazon.com/ec2/pricing/on-demand/#Elastic_IP_Addresses https://aws.amazon.com/ec2/pricing/on-demand/#Elastic_IP_Addresses

Keep in mind: it only works with VPC EC2 instances.请记住:它仅适用于 VPC EC2 实例。 Old "classic" instances will lose their elastic IP and you will have to associate it manually again.旧的“经典”实例将失去其弹性 IP,您将不得不再次手动关联它。

This tool should do what you're asking for: https://github.com/wagoodman/bridgy这个工具应该做你所要求的: https : //github.com/wagoodman/bridgy

bridgy ssh <some-instance-id>

If you have the ~/.aws creds/config defined as you would with the aws-cli as well as the following in ~/.bridgy/config.yml :如果您像使用 aws-cli 一样定义了~/.aws creds/config 以及~/.bridgy/config.yml的以下~/.bridgy/config.yml

inventory:
    source: aws
    update_at_start: true
ssh:
    user: <your ssh user>
...

then it should work!那么它应该工作!

Disclosure: I'm the author.披露:我是作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM