Skip to content

Instantly share code, notes, and snippets.

@dtaivpp
Created September 28, 2021 15:14
Show Gist options
  • Save dtaivpp/9ea479b27e3735acb63e85467a40a34f to your computer and use it in GitHub Desktop.
Save dtaivpp/9ea479b27e3735acb63e85467a40a34f to your computer and use it in GitHub Desktop.
This is a quick script for scraping over a terraform directory and extracting the resources used
import yaml
import os
yaml_files = []
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
for fname in fileList:
if fname[-4:] == 'yaml':
yaml_files.append( dirName + "/" + fname)
def find(d, tag):
if tag in d:
yield d[tag]
for k, v in d.items():
if isinstance(v, dict):
for i in find(v, tag):
yield i
resources = []
for file in yaml_files:
with open(file) as f:
data = yaml.load(f)
for val in find(data, "resource_type"):
resources.append(val)
distinct_resources = set(resources)
with open("resources.txt", '+w') as outfile:
outfile.writelines('\n'.join(distinct_resources))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment