Last active
April 11, 2016 11:34
-
-
Save gabriellett/c529e364c3ba51b9568599a8792607c5 to your computer and use it in GitHub Desktop.
AWS CloudFormation Template dumper
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
# This script will look for cloudformation stacks on your account and download all the templates for you! | |
# Author: Gabriel Lett Viviani <http://github.com/gabriellett> | |
# Make sure you have aws-cli installed and configured on your machine,. | |
require 'json' | |
require 'optparse' | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: cfn-template-dump.rb [options]" | |
opts.on('-p', '--profile NAME', 'AWS Profile, defaults to the credentials already set on the machine') { |v| options[:profile] = v } | |
opts.on('-r', '--region REGION', 'AWS Region, if none is passed, will search on all regions') { |v| options[:region] = v } | |
end.parse! | |
stock_command_sufix = "" | |
stock_command_sufix += " --profile #{options[:profile]}" if options[:profile] | |
regions = [] | |
if options[:region] | |
regions << options[:region] | |
else | |
regions_result = JSON.parse(`aws ec2 describe-regions`) | |
regions_result["Regions"].each do |r| | |
regions << r["RegionName"] | |
end | |
end | |
regions.each do |r| | |
puts "Looking for stacks on #{r} ... \n\n" | |
stack_list = JSON.parse(`aws cloudformation list-stacks #{stock_command_sufix} --region #{r}`) | |
stack_list["StackSummaries"].each do |s| | |
stack_status = s['StackStatus'] | |
stack_name = s['StackName'] | |
if !stack_status.start_with?('DELETE') | |
print " Creating json for #{stack_name}... " | |
template_body = JSON.parse(`aws cloudformation get-template --stack-name #{stack_name} #{stock_command_sufix} --region #{r}`) | |
File.open("templates/#{stack_name}.json", 'w') do |f| | |
f.write(JSON.pretty_generate(template_body['TemplateBody'])) | |
end | |
puts "Done!" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment