Terraform 输出中的 Terraform user_data 输出

发布于 2025-01-12 22:57:10 字数 300 浏览 0 评论 0原文

我使用 Terraform 创建 EC2 实例,其中还包括 user_data,我的问题是我能否在 terraform 输出中看到 user_data 的 linux 命令的输出。
前任。

....
user_data = <<EOF
 echo "Output from server"
EOF
...

并在执行 terraform apply 时查看此命令的输出。请告知,也许这不需要使用 user_data 来完成?

I creating EC2 instance with Terraform, which includes also user_data, my question is can I see the output of linux command from user_data in terraform output.
Ex.

....
user_data = <<EOF
 echo "Output from server"
EOF
...

And see the this command output when executing terraform apply. Please advise, maybe this need to be done not with user_data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2025-01-19 22:57:10

从 EC2 和 Terraform 的角度来看,user_data 只是保存在 EC2 API 中的一些任意字节,EC2 实例中的软件可以在虚拟机开始运行后随时检索这些字节。由于这种设计,没有具体要求 user_data 是一组要运行的命令,或者这些命令应在引导过程的早期运行。

我预计您使用的 AMI 可能包含运行 cloud-init 作为其一部分启动过程,因此您习惯于在虚拟机首次启动时运行此参数的内容。在这种情况下,我可以理解为什么您可能期望 Terraform 能够观察输出,但不幸的是这是不可能的。

不可能的原因是,从 EC2 API 的角度来看,实例在开始在 AMI 中运行代码时立即“运行”,因此,尽管 cloud-init 通常会在启动过程的早期运行您指定的脚本,但Terraform 的反应仍然太晚:在 cloud-init 有机会检索该数据并将其作为脚本执行之前,实例创建已经完成。

此外,cloud-init 只是系统上运行的普通软件,就像系统启动时运行的任何其他应用程序一样。它没有任何特殊的方式将其输出发送到 Terraform 等外部程序:它只能将其结果写入虚拟机文件系统中的日志文件,就像您配置为在启动时运行的任何其他服务一样您的 AMI。

综上所述,那么:通常无法从 EC2 API 的角度观察 cloud-init 的结果。如果您确实希望看到这些结果,那么您将需要采用不同的策略,例如向 AMI 添加配置,以便该虚拟机的系统日志将发送到日志聚合器服务,例如 CloudWatch 日志。然后,您可以使用该服务(Terraform 外部)在虚拟机运行时(而不仅仅是在启动过程中)查看系统上软件的持续日志输出。

From the perspective of EC2 and of Terraform, user_data is just some arbitrary bytes saved in the EC2 API which software in the EC2 instance can retrieve at any time after the virtual machine begins running. Because of that design, there's no specific requirement that the user_data be a set of commands to run, or that those commands should run early in the boot process.

I expect you're probably using an AMI which includes running cloud-init as part of its boot process, and so you're accustomed to the content of this argument being run on first boot of the VM. In that case, I can understand why you might expect Terraform to be able to observe the output, but that is unfortunately not possible.

The reason it's not possible is that from the perspective of the EC2 API an instance is "running" immediately when it begins running code in the AMI, and so even though cloud-init does typically run your specified script early in the boot process, that is still too late for Terraform to react to: the instance creation is already complete before cloud-init gets an opportunity to retrieve that data and execute it as a script.

Furthermore, cloud-init is just normal software running on the system, just like any other application that runs on system boot. It doesn't have any special way to send its output to an external program like Terraform: it can only write its results into a log file in the VM's filesystem, as would be true for any other service you have configured to run on boot inside your AMI.

With all of that said, then: there is generally no way to observe cloud-init's results from the perspective of the EC2 API. If you do wish to see those results then you will need to adopt a different strategy, such as adding configuration to your AMI so that the system logs for that VM will be sent to a log aggregator service like CloudWatch Logs. You can then use that service (outside of Terraform) to see the ongoing log output from software on your system throughout the runtime of the VM, not just during the boot process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文