Created
July 8, 2024 10:54
-
-
Save scottcowan/b70717fa5c7e2d9f6ea70eea195dbc8e to your computer and use it in GitHub Desktop.
Get output from terraform state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
def get_terraform_output(state_file_path, output_name): | |
with open(state_file_path, 'r') as f: | |
state = json.load(f) | |
outputs = state.get('outputs', {}) | |
output_value = outputs.get(output_name, {}).get('value', None) | |
return output_value | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
print("Usage: python get_terraform_output.py <state_file_path> <output_name>") | |
sys.exit(1) | |
state_file_path = sys.argv[1] | |
output_name = sys.argv[2] | |
output_value = get_terraform_output(state_file_path, output_name) | |
if output_value: | |
print(f"::set-output name={output_name}::{output_value}") | |
else: | |
print(f"Output '{output_name}' not found in the state file.") | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment