Last active
January 15, 2018 03:43
-
-
Save saidsef/d2a6e234d7a10f26dd1af38a77e4753b to your computer and use it in GitHub Desktop.
Print IAM Role Policy as YAML
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Copyright (c) 2018, Said Sef. All rights reserved. | |
# Use of this source code is governed by a BSD-style license that can be | |
__author__ = 'Said Sef' | |
import hcl | |
import json | |
import yaml | |
from subprocess import check_output | |
def yaml_out(data): | |
return yaml.safe_dump(json.loads(data), default_flow_style=False, stream=None) | |
tf = check_output("find . -type f -iname *.tf -o -iname *.tpl", shell=True) | |
for f in tf.split("\n"): | |
try: | |
if 'tpl' in f: | |
with open(f, 'r') as fh: | |
data = fh.read() | |
print yaml_out(data), '#######', f, '#######', "\n" | |
else: | |
with open(f, 'r') as fh: | |
data = hcl.load(fh) | |
if 'aws_iam_role_policy' in data['resource']: | |
for p in data['resource']['aws_iam_role_policy']: | |
# terraform v9.x policy | |
policy = data['resource']['aws_iam_role_policy'][p]['policy'] | |
print yaml_out(policy), '#######', f, p, '#######', "\n" | |
elif 'aws_iam_policy' in data['resource']: | |
for p in data['resource']['aws_iam_policy']: | |
# terraform v8.x policy | |
policy = data['resource']['aws_iam_policy'][p]['policy'] | |
print yaml_out(policy), '#######', f, p, '#######', "\n" | |
except Exception as e: | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment