Skip to content

Instantly share code, notes, and snippets.

@beranm14
Created July 25, 2023 13:05
Show Gist options
  • Save beranm14/6c79731a3f020b3bcde07e8ee59f61ea to your computer and use it in GitHub Desktop.
Save beranm14/6c79731a3f020b3bcde07e8ee59f61ea to your computer and use it in GitHub Desktop.
Get terraform resource based on its address
import sys
import json
state = json.loads(sys.stdin.read())
address = sys.argv[1]
def get_by_address(state, address):
if type(state) is list:
for i in state:
found = get_by_address(i, address)
if found:
return found
elif type(state) is dict:
if state.get('address', '') == address:
return state
else:
for i in state:
found = get_by_address(state[i], address)
if found:
return found
print(json.dumps(get_by_address(state, address)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment