亚马逊ec2用户数据,它是如何工作的?

时间:2021-11-04 06:40:13

We are starting instances, and accessing the user-data we place. But does anybody understand the internals of this operation (from Amazon's side)? When we pass in the user-data, at what point is that data transmitted to the VM (is this a Xen feature) and where is it stored?

我们正在启动实例,并访问我们放置的用户数据。但有人理解这项行动的内部(来自亚马逊方面)吗?当我们传入用户数据时,在什么时候传输到VM的数​​据(这是一个Xen功能)和存储在哪里?

I first thought, it was set as the USER_DATA env var, but we can also pass it as a file. Where is that file stored? Is it generic to all instances or varies depending on AMI?

我首先想到的是,它被设置为USER_DATA env var,但我们也可以将其作为文件传递。该文件存储在哪里?它是通用的所有实例还是因AMI而异?

This is not a problem per se, just wanted to know how Amazon does this.

这本身不是问题,只是想知道亚马逊如何做到这一点。

User data screenshot: http://d.pr/GZlY

用户数据截图:http://d.pr/GZlY

4 个解决方案

#1


49  

The user-data is available to the instance with a simple HTTP request at this URL:

在此URL处,通过简单的HTTP请求,实例可以使用用户数据:

http://169.254.169.254/latest/user-data

Amazon EC2 does not put this user-data on the instance directly, though many AMIs have code that instructs the instance to download and process the user-data automatically.

虽然许多AMI都有指示实例自动下载和处理用户数据的代码,但Amazon EC2并未将此用户数据直接放在实例上。

See also:

也可以看看:

#2


4  

An easy example for everyone's understanding: If you want to create the file /tmp/testfile.txt when the machine gets started, you can simply add these two lines on the User data field.

每个人都能理解的简单示例:如果要在机器启动时创建文件/tmp/testfile.txt,只需在用户数据字段中添加这两行即可。

#!/bin/bash
touch /tmp/testfile.txt

Remember to put the #!/bin/bash at the top before your commands.

记得在命令之前将#!/ bin / bash放在顶部。

When you run the instance (Linux AMI), you can see the User data field content at /var/lib/cloud/instance/user-data.txt

运行实例(Linux AMI)时,可以在/var/lib/cloud/instance/user-data.txt中看到用户数据字段内容。

#3


1  

AWS userdata is the set of commands/data you can provide to a instance at launch time. For example if you are launching an ec2 instance and want to have docker installed on the newly launched ec2, than you can provide set of bash commands in the userdata field of aws ec2 config page.

AWS userdata是您可以在启动时为实例提供的命令/数据集。例如,如果要启动ec2实例并希望在新启动的ec2上安装docker,则可以在aws ec2 config页面的userdata字段中提供一组bash命令。

Usecase

用例

Here is a well explained example of AWS userdata with video tutorial

以下是带有视频教程的AWS userdata的一个很好解释的示例

#4


1  

Sorry to post to such an old question, but this seems like the best place to put this additional piece of information.

很抱歉发布这么老的问题,但这似乎是放置这些额外信息的最佳位置。

Most all the AWS documents describe User Data as a property in which to put instance lifecycle startup scripting, that is, the stuff you want to run only when the instance first launches.

大多数AWS文档都将User Data描述为一个属性,用于放置实例生命周期启动脚本,即只有在实例首次启动时才能运行的内容。

This is usually the case, but there has been at least one other person besides myself wanting to perform different scripting on restart, say to fix a broken key or something. And guess what... you can do that using User Data.

通常就是这种情况,但除了我自己想要在重启时执行不同的脚本时,至少还有一个人,比如修复损坏的密钥或其他东西。猜猜是什么......你可以使用用户数据做到这一点。

Here is the code and the link to the AWS document...

以下是AWS文档的代码和链接......

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World." >> /tmp/sdksdfjsdlf
--//

I can find no documentation on this formatting of User Data that allows this to happen. I've tried it out and it works. I have tried to see if it runs on every startup, and it does.

我找不到允许这种情况发生的用户数据格式的文档。我已经尝试过它并且有效。我试图看看它是否在每次启动时都运行,而且确实如此。

So, if you think you need to do this, I recommend that you backup. Make sure you have a copy of the original User Data, and use the code provided modified to suite, and remove the code upon the next time you stop the instance (to avoid multiple runs of the script).

所以,如果你认为你需要这样做,我建议你备份。确保您拥有原始用户数据的副本,并使用修改后提供的代码套件,并在下次停止实例时删除代码(以避免多次运行脚本)。

#1


49  

The user-data is available to the instance with a simple HTTP request at this URL:

在此URL处,通过简单的HTTP请求,实例可以使用用户数据:

http://169.254.169.254/latest/user-data

Amazon EC2 does not put this user-data on the instance directly, though many AMIs have code that instructs the instance to download and process the user-data automatically.

虽然许多AMI都有指示实例自动下载和处理用户数据的代码,但Amazon EC2并未将此用户数据直接放在实例上。

See also:

也可以看看:

#2


4  

An easy example for everyone's understanding: If you want to create the file /tmp/testfile.txt when the machine gets started, you can simply add these two lines on the User data field.

每个人都能理解的简单示例:如果要在机器启动时创建文件/tmp/testfile.txt,只需在用户数据字段中添加这两行即可。

#!/bin/bash
touch /tmp/testfile.txt

Remember to put the #!/bin/bash at the top before your commands.

记得在命令之前将#!/ bin / bash放在顶部。

When you run the instance (Linux AMI), you can see the User data field content at /var/lib/cloud/instance/user-data.txt

运行实例(Linux AMI)时,可以在/var/lib/cloud/instance/user-data.txt中看到用户数据字段内容。

#3


1  

AWS userdata is the set of commands/data you can provide to a instance at launch time. For example if you are launching an ec2 instance and want to have docker installed on the newly launched ec2, than you can provide set of bash commands in the userdata field of aws ec2 config page.

AWS userdata是您可以在启动时为实例提供的命令/数据集。例如,如果要启动ec2实例并希望在新启动的ec2上安装docker,则可以在aws ec2 config页面的userdata字段中提供一组bash命令。

Usecase

用例

Here is a well explained example of AWS userdata with video tutorial

以下是带有视频教程的AWS userdata的一个很好解释的示例

#4


1  

Sorry to post to such an old question, but this seems like the best place to put this additional piece of information.

很抱歉发布这么老的问题,但这似乎是放置这些额外信息的最佳位置。

Most all the AWS documents describe User Data as a property in which to put instance lifecycle startup scripting, that is, the stuff you want to run only when the instance first launches.

大多数AWS文档都将User Data描述为一个属性,用于放置实例生命周期启动脚本,即只有在实例首次启动时才能运行的内容。

This is usually the case, but there has been at least one other person besides myself wanting to perform different scripting on restart, say to fix a broken key or something. And guess what... you can do that using User Data.

通常就是这种情况,但除了我自己想要在重启时执行不同的脚本时,至少还有一个人,比如修复损坏的密钥或其他东西。猜猜是什么......你可以使用用户数据做到这一点。

Here is the code and the link to the AWS document...

以下是AWS文档的代码和链接......

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World." >> /tmp/sdksdfjsdlf
--//

I can find no documentation on this formatting of User Data that allows this to happen. I've tried it out and it works. I have tried to see if it runs on every startup, and it does.

我找不到允许这种情况发生的用户数据格式的文档。我已经尝试过它并且有效。我试图看看它是否在每次启动时都运行,而且确实如此。

So, if you think you need to do this, I recommend that you backup. Make sure you have a copy of the original User Data, and use the code provided modified to suite, and remove the code upon the next time you stop the instance (to avoid multiple runs of the script).

所以,如果你认为你需要这样做,我建议你备份。确保您拥有原始用户数据的副本,并使用修改后提供的代码套件,并在下次停止实例时删除代码(以避免多次运行脚本)。