简体   繁体   English

是否可以执行一次现有EC2的修改用户数据?

[英]Is it possible to execute the modified user data of an existing EC2 once?

In the AWS website https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html , it shows how to modify the instance user data of an existing EC2.在 AWS 网站https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html中,它展示了如何修改现有 EC2 的实例用户数据。

However, at the end it says the modified user data will not be executed (7).然而,最后它说修改后的用户数据将不会被执行(7)。 What's the point to modify the user data and not executed?修改用户数据不执行有什么意义呢? Is it possible to execute the modified user data once?是否可以将修改后的用户数据执行一次?

To modify instance user data修改实例用户数据

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ .https://console.aws.amazon.com/ec2/打开 Amazon EC2 控制台。

  2. In the navigation pane, choose Instances.在导航窗格中,选择实例。

  3. Select the instance and choose Instance state, Stop instance. Select 实例并选择实例 state,停止实例。 If this option is disabled, either the instance is already stopped or its root device is an instance store volume.如果禁用此选项,则实例已停止或其根设备是实例存储卷。

  4. When prompted for confirmation, choose Stop.当提示确认时,选择停止。 It can take a few minutes for the instance to stop.实例可能需要几分钟才能停止。

  5. With the instance still selected, choose Actions, Instance settings, Edit user data.在实例仍处于选中状态的情况下,选择操作、实例设置、编辑用户数据。

  6. Modify the user data as needed, and then choose Save.根据需要修改用户数据,然后选择保存。

  7. Restart the instance.重启实例。 The new user data is visible on your instance after you restart it;重新启动实例后,新的用户数据在您的实例上可见; however, user data scripts are not executed.但是,不会执行用户数据脚本。

The User Data field was created as a way to pass information to an instance.创建用户数据字段是为了将信息传递给实例。 For example, providing a password to a database, or configuration information.例如,为数据库提供密码或配置信息。

Then, the Ubuntu community came up with the clever idea of passing a script via the User Data field, and having some code on the instance execute the script when the system boots.然后,Ubuntu 社区提出了一个聪明的想法,即通过用户数据字段传递脚本,并在系统启动时让实例上的一些代码执行脚本。 This has enabled "self-configuring" systems, and is called cloud-init .这启用了“自配置”系统,称为cloud-init

By default, the User Data script only executes once per instance , with the intention of installing software.默认情况下,用户数据脚本每个实例只执行一次,目的是安装软件。

From Boot Stages — cloud-init documentation :启动阶段 — cloud-init 文档

cloud-init ships a command for manually cleaning the cache: cloud-init clean cloud-init 提供了一个手动清理缓存的命令: cloud-init clean

Running this command will 'forget' the previous runs, and will execute the User Data script on the next boot.运行此命令将“忘记”之前的运行,并将在下次启动时执行用户数据脚本。

It is also possible to run a script on every boot by placing the script in /var/lib/cloud/scripts/per-boot/ .通过将脚本放在/var/lib/cloud/scripts/per-boot/也可以在每次启动时运行脚本。

Simple step to step working Solution:一步到位的简单工作解决方案:

  1. Stop the Instance停止实例

  2. Go to Action > Instance Setting > Edit User Data Go 到 Action > Instance Setting > Edit User Data

And make sure to choose As Text in Edit User Data Screen并确保在编辑用户数据屏幕中选择作为文本

  1. Add commands on Textarea在 Textarea 上添加命令

  2. Now start the Instance And check that it is on Running State现在启动实例并检查它是否正在运行 State

  3. check your public IP (This has been changed after restart)检查您的公共 IP(重启后已更改)

  4. Now finally connect to the instance using ssh: ssh -i @<IP_ADDRESS>现在终于使用 ssh 连接到实例:ssh -i @<IP_ADDRESS>

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

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