Amazon web services 如何自动化EC2实例启动和ssh连接
此时,我手动连接以下步骤:Amazon web services 如何自动化EC2实例启动和ssh连接,amazon-web-services,amazon-ec2,Amazon Web Services,Amazon Ec2,此时,我手动连接以下步骤: 打开EC2实例web 在操作->实例状态下单击开始 查看连接选项卡 手动复制ssh命令,例如: ssh-i“mykey.pem”ubuntu@ec2-13-112-241-333.ap-northeast-1.compute.amazonaws.com 我可以通过本地计算机中的命令行简化这些茎的最佳实践是什么?因此,我可以只使用一个命令。使用 您可以使用ec2命令行。创建批处理或shell脚本。可以通过命令执行以下操作:启动、检查状态是否正在运行或挂起、获取自动分配的
ssh-i“mykey.pem”ubuntu@ec2-13-112-241-333.ap-northeast-1.compute.amazonaws.com
您可以使用ec2命令行。创建批处理或shell脚本。可以通过命令执行以下操作:启动、检查状态是否正在运行或挂起、获取自动分配的dns名称。然后可以在dns上启动ssh。请查看手册中的确切命令。我使用java,但你有很多选择。或者,分配一个弹性IP地址,并在实例停止/启动后保持相同的IP地址(附加费用适用)。@JohnRotenstein,不确定OP是否需要它。将它添加到答案中,可能会有人发现它很有用。@JohnRotenstein如何将弹性IP地址添加到franklinsijo的代码中?@neversaint它是添加的代码只是:启动实例,等待它启动,然后ssh到它。顺便说一句,等待状态检查“通过”可能等待的时间太长,因为在状态检查完成之前,实例通常运行良好。我会等待State=Running,然后尝试连接。如果不起作用,请重试。
# Start the instance
aws ec2 start-instances --instance-ids i-xxxxxxxxxxx
status=0
# Wait for the instance until the 2/2 checks are passed
while [ $status -lt 2]
do
status=`aws ec2 describe-instance-status --instance-ids i-xxxxxxxxxxx --filters Name="instance-status.reachability,Values=passed" | grep '"Status": "passed"' | wc -l`
# add sleep time
done
# Associate an Elastic IP if already have one allocated (skip if not reqd)
aws ec2 associate-address --instance-id i-xxxxxxxxxxx --public-ip elastic_ip
# Get the Public DNS, (If the instance has only PrivateIp, grep "PrivateIpAddress")
public_dns=`aws ec2 describe-instances --instance-ids i-xxxxxxxxxxx | grep "PublicDnsName" | head -1 | awk -F: '{print $2}' | sed 's/\ "//g;s/",//g'`
ssh -i key.pem username@public_dns