Skip to content

Instantly share code, notes, and snippets.

@scottcowan
Created July 8, 2024 10:54
Show Gist options
  • Save scottcowan/b70717fa5c7e2d9f6ea70eea195dbc8e to your computer and use it in GitHub Desktop.
Save scottcowan/b70717fa5c7e2d9f6ea70eea195dbc8e to your computer and use it in GitHub Desktop.
Get output from terraform state
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