Get file content as a Terraform output

amazon-web-servicesterraform

I have a Terraform configuration where I start a systemd service on an AWS EC2 instance. I need to grep that service's log and export it to be a Terraform output.

When hello.service is started, it logs a line like this (among many other):

Root Key: F4BF9F7FCBEDABA0392F108C59D8F4A38B38

I need that line to be a Terraform output. Something like this:

resource "aws_instance" "instance" {

    provisioner "remote-exec" {
        //start hello.service
    }

}

output "rootKey" {
    value = "${}" //??
}

I want rootKey output to be the result of:

journalctl -u hello.service | grep "Root Key"

being executed at aws_instance.instance

How can I get it?

Best Answer

Use a null_resource combined with local-exec to grab the Output of a remote exec.

https://github.com/matti/terraform-shell-resource provides a nice implementation for this.