'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.
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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Atkins