Skip to content

Instantly share code, notes, and snippets.

@saidsef
Last active January 15, 2018 03:43
Show Gist options
  • Save saidsef/d2a6e234d7a10f26dd1af38a77e4753b to your computer and use it in GitHub Desktop.
Save saidsef/d2a6e234d7a10f26dd1af38a77e4753b to your computer and use it in GitHub Desktop.
Print IAM Role Policy as YAML
#!/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