简体   繁体   English

Terraform 用户数据 output 在 terraform output

[英]Terraform user_data output in terraform output

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.我使用 Terraform 创建 EC2 实例,其中还包括user_data ,我的问题是我可以从 terraform output 中的user_data看到 output 命令的 linux。
Ex.前任。

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

And see the this command output when executing terraform apply .并在执行terraform apply时看到这个命令 output 。 Please advise, maybe this need to be done not with user_data ?请告知,也许这不需要用user_data来完成?

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

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.我预计您可能使用的 AMI 包括运行cloud-init作为其启动过程的一部分,因此您已经习惯了在 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.在那种情况下,我可以理解为什么您可能期望 Terraform 能够观察到 output,但遗憾的是这是不可能的。

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

Furthermore, cloud-init is just normal software running on the system, just like any other application that runs on system boot.此外,cloud-init 只是在系统上运行的普通软件,就像在系统启动时运行的任何其他应用程序一样。 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.它没有任何特殊的方式将其 output 发送到外部程序,如 Terraform:它只能将其结果写入 VM 文件系统中的日志文件,对于您配置为在内部启动时运行的任何其他服务也是如此你的 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 .综上所述,那么:通常无法从 EC2 API 的角度观察 cloud-init 的结果。如果您确实希望看到这些结果,则需要采用不同的策略,例如将配置添加到您的 AMI,以便该 VM 的系统日志将被发送到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.然后,您可以使用该服务(在 Terraform 之外)在整个 VM 运行期间(而不仅仅是在启动过程中)从系统上的软件中查看正在进行的日志 output。

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

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