简体   繁体   English

在 AWS 中,在非办公时间关闭/启动所有开发人员 EC2 实例以节省成本(使用 Dynamic IP)

[英]in AWS shutting down / starting all developers EC2 instance at non office working Hours for cost saving ( using Dynamic IP )

Have a small requirement, I have 50 developers, using EC2 instances for their development tasks, they use it to develop word press sites on their local ec2 instance.有一个小要求,我有 50 个开发人员,他们使用 EC2 实例来完成他们的开发任务,他们用它来在本地 ec2 实例上开发 word press 网站。 Currently they EC2 instances are up and running 24/7 and this increasing the cost involved.目前,他们的 EC2 实例全天候 24/7 运行,这增加了所涉及的成本。 If I use the EC2CLI to shutdown and start their EC2 instance they will lose their IP (since we are using dynamic IP and don't want to use static IP as it it cost intensive).如果我使用EC2CLI关闭并启动他们的 EC2 实例,他们将丢失他们的 IP(因为我们使用动态 IP 并且不想使用 static IP,因为它成本很高)。 They map their IP to a domain name (eg: 1.1.1.1 wwwname.examplesite.com ) in the local windows 7 hosts file.他们 map 他们 IP 到一个域名(例如: 1.1.1.1 wwwname.examplesite.com )在本地 windows 7 主机文件。

We plan to shut down their EC2 instance in non-working office hours using EC2 CLI and again start it automatically once the Day begins, but once we start it again, the IP will change and how the developer get to know their IP.我们计划在非工作办公时间使用 EC2 CLI 关闭他们的 EC2 实例,并在一天开始后再次自动启动它,但是一旦我们再次启动它,IP 就会改变,开发人员如何了解他们的 IP。

Any inputs on this will be highly Appreciated.对此的任何投入将不胜感激。

There is no "one click way" to do this afaik, since amazon doesnt really want you to save money this way :). 没有“一键式”方式来执行此afaik,因为亚马逊并不真的希望您通过这种方式省钱:)。

I have programmed a solution for this by using the AWS SDK: Basically it periodically polls AWS for all instances public hostnames. 我已经使用AWS开发工具包为此编程了一个解决方案:基本上,它会定期轮询AWS以获取所有实例的公共主机名。 When it finds them, it registers the public hostname to amazon Route 53 (DNS) with a CNAME link to the public domain name (the one you are now using in windows hosts file) of your choice. 找到它们后,它会将公共主机名注册到带有选择名称的公共域名(您现在在Windows主机文件中正在使用的域名)的CNAME链接到Amazon Route 53(DNS)上。

It s the principle of dynamic DNS. 这是动态DNS的原理。 It s been working fine for several years. 它已经工作好几年了。 Good luck 祝好运

A lot of the time, it's actually bad practice to rely upon IP Addresses, as they are subject to change like you touched upon in your answer. 很多时候,依赖IP地址实际上是一种不好的做法,因为它们会像您在回答中所触及的那样不断变化。 You should instead rely upon DNS names. 您应该改为依靠DNS名称。

The best solution would be to write a script gets the current public IP on the instance and updates a DNS A record. 最好的解决方案是编写一个脚本,以获取实例上的当前公共IP并更新DNS A记录。 It would make most sense to do this with Route 53 as it has a nice API. 使用Route 53进行此操作最有意义,因为它具有不错的API。

You'd need to allocate each employee their own DNS name, and each instance would need to know the DNS name relevant to it (eg. hard coded in script, environment variable etc). 您需要为每个员工分配自己的DNS名称,每个实例都需要知道与其相关的DNS名称(例如,在脚本中硬编码,环境变量等)。 Then, you could write a script, make it run each time the instance boots up, and have it do the following: 然后,您可以编写一个脚本,使该脚本在每次实例启动时运行,并执行以下操作:

  1. Submit a HTTP request to http://169.254.169.254/latest/meta-data/public-ipv4 (EC2 meta data service, read more here ) to get the current public IP Address of the instance http://169.254.169.254/latest/meta-data/public-ipv4(EC2元数据服务, 在此处了解更多信息 )提交HTTP请求,以获取实例的当前公共IP地址
  2. Update an A record on Route 53 using the ChangeResourceRecordSets API call. 使用ChangeResourceRecordSets API调用在Route 53上更新A记录。

Using AWS Lambda, you could write a script to stop all instances at a particular time thanks to this recent announcement . 借助此最新公告 ,您可以使用AWS Lambda编写脚本以在特定时间停止所有实例。 But this could pose some small problems if your employees were working late, and were interrupted by their instance shutting down. 但是,如果您的员工上班迟到,并因实例关闭而中断,则可能会带来一些小问题。 Also, I wouldn't recommend that you then start all these instances back up again at a particular time. 另外,我不建议您在特定时间再次启动所有这些实例。 It would be a more cost efficient solution to just allow employees to start their instance each day. 仅允许员工每天启动实例将是一种更具成本效益的解决方案。 What if 5 employees are out sick? 如果有5名员工生病怎么办? If they don't come in to work, their instances won't get started :) 如果他们不上班,他们的实例将无法开始:)

I am probably comming in late but if someone else is looking to do it you could script it using python and boto3 .我可能会迟到,但如果其他人想要这样做,您可以使用 python 和boto3编写脚本。 This will allow you to:这将使您能够:

  • Start and stop a given (or all) instances启动和停止给定(或所有)实例
  • Get the IP adress of a given instance获取给定实例的 IP 地址

How about writing a VB script on the developer machine . 如何在开发人员机器上编写VB脚本。 On windows7 desktop boot, their specific machine would boot up and on windows7 desktop shutdown, could call a VB script to shut down the instance. 在Windows7桌面启动时,其特定计算机将启动,在Windows7桌面关闭时,可以调用VB脚本关闭实例。 Or is there any other language I could use instead of VB ( would have to learn VB ) . 还是我可以使用其他任何语言代替VB(必须学习VB)。 Cannot use python either cant install python. 无法使用python或无法安装python。

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

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