Learn how to streamline your Terraform workflow by saving the output of your infrastructure deployments to a file for easy access and automation.
Terraform is a powerful tool for infrastructure as code, but it doesn't directly support saving output values to a file during the terraform apply process. However, there are several workarounds to achieve this. This article explores three common methods: using terraform output with shell redirection, leveraging the local_file resource with templates, and employing scripting languages like Bash or Python. We'll also touch upon important considerations such as security and alternative tools for enhanced output management.
Terraform itself doesn't have a direct way to save output values to a file during terraform apply.
Workarounds:
Using terraform output and shell redirection:
terraform apply, run:
terraform output my_output_value > output.txtoutput.txt.Using local_file resource with templatefile or template_cloudinit_config:
local_file resource and use templatefile or template_cloudinit_config to populate its content with output values.
resource "local_file" "output_file" {
content = templatefile("${path.module}/output.tpl", {
my_value = module.example.my_output_value
})
filename = "output.txt"
}Scripting (e.g., Bash, Python):
terraform output and write to a file.
#!/bin/bash
terraform output -json > output.json
# Process output.json and write desired values to a fileImportant Considerations:
terraform plan output to a file using terraform plan -out=planfile, treat these files as potentially sensitive.terragrunt or CI/CD systems for more advanced output handling and automation.This code provides three examples of how to save Terraform output values to a file. The first example uses shell redirection with the terraform output command. The second example uses the local_file resource with the templatefile function to dynamically generate the file content. The third example uses a Python script to execute the terraform output command, parse the JSON output, and write the desired value to a file.
This document provides code examples for the workarounds mentioned in the article about saving Terraform output values to a file.
1. Using terraform output and shell redirection:
# After running terraform apply
terraform output my_output_value > output.txtThis command will redirect the value of the output variable my_output_value to a file named output.txt.
2. Using local_file resource with templatefile:
# Define the output variable in your module
output "my_output_value" {
value = "This is the output value"
}
# Create a local file resource
resource "local_file" "output_file" {
content = templatefile("${path.module}/output.tpl", {
my_value = module.example.my_output_value
})
filename = "output.txt"
}output.tpl:
My output value is: ${my_value}
This code defines a local_file resource that uses the templatefile function to populate the content of the file. The templatefile function takes a template file (output.tpl) and a map of variables. The template file can then use these variables to dynamically generate the content of the output file.
3. Using Python Scripting:
import json
import subprocess
# Execute terraform output command and capture the output
output = subprocess.check_output(["terraform", "output", "-json"])
# Parse the JSON output
data = json.loads(output)
# Extract the desired output value
my_value = data['my_output_value']['value']
# Write the output value to a file
with open("output.txt", "w") as f:
f.write(f"My output value is: {my_value}")This script uses the subprocess module to execute the terraform output -json command and capture its output. It then uses the json module to parse the JSON output and extract the desired value. Finally, it writes the value to a file named output.txt.
Remember:
my_output_value with the actual name of your output variable.General Considerations:
terraform output might fail or return unexpected data.Specific to local_file Resource:
local_file resource. Changes to the template or output values will trigger a recreation of the file.file or remote-exec with the local_file resource for more complex file operations after creation.Security Best Practices:
Beyond Workarounds:
Example Use Cases:
While Terraform doesn't directly support saving output values to files during terraform apply, here are three common workarounds:
1. Shell Redirection:
terraform output my_output_value > output.txt after running terraform apply.2. Terraform Resources:
local_file resource and populate its content using templatefile or template_cloudinit_config with desired output values.3. Scripting:
terraform output using scripts (e.g., Bash, Python) and write extracted values to a file.Key Considerations:
terraform plan output files (created with terraform plan -out=planfile) with care due to potential sensitivity.terragrunt or CI/CD systems for enhanced output management and automation.In conclusion, while Terraform doesn't natively support saving output values to files during terraform apply, you can employ workarounds like shell redirection, the local_file resource with templates, or scripting to achieve this. When choosing a method, prioritize security by avoiding storing sensitive data in output files and setting appropriate file permissions. For more robust output management and automation, consider integrating Terraform with tools like terragrunt or CI/CD systems. By understanding these techniques and considerations, you can effectively leverage Terraform's output values for various use cases, such as dynamic inventory generation, application configuration, and monitoring setup.
Output data from Terraform | Terraform | HashiCorp Developer | Terraform output values let you export structured data about your resources. You can use this data to configure other parts of your infrastructure with ...
Command: plan | Terraform | HashiCorp Developer | ... Terraform's terminal output, it will be saved in cleartext in the plan file. You should therefore treat any saved plan files as potentially-sensitive artifacts.
Terraform plan output to file – faultbucket | Nov 27, 2020 ... Sometimes you want to be able to distribute this as a file. In the past, I've tried commands like: terraform plan > tfplan.txt. However that ...
Terraform outputs with "heredoc" syntax leaves <<EOT in file ... | Hi, I have a terraform script that outputs a number of EKS related config files - previously this has worked exactly as expected - over the last week or two I’ve noticed a change which has sadly broken the scripts. The outputs which use the heredoc syntax; <<EOT EOT Have the <<EOT & EOT in the output files, has something changed I’ve missed in the release notes?
Output public_ip into file using template_file - Terraform - HashiCorp ... | I am trying to create a file with the public_ip addresses to use with ansible. So, I am looking to create a file to look like: [server-type] 10.0.0.2 10.0.0.3 10.0.0.4 … Is there a way to do this in terraform?
How to process terraform apply logs for catching errors in CI ... | I’m running terragrunt in CI to automatically apply changes once they’re merged, and am having trouble collecting useful data when an apply failure is encountered. the problem is that run-all apply output is a little messy; it’s difficult to programmatically parse out error messages. i guess an ideal configuration would be if each module wrote its apply output to a separate file, and then if terragrunt wrote a json object with the return code of each module and the filepath of the apply output f...