Last active
May 27, 2016 21:27
-
-
Save a2ikm/23529a7452bbc702f13c to your computer and use it in GitHub Desktop.
Rake tasks for Terraform
This file contains 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
task :environment do | |
errors = [] | |
%w( | |
TF_VAR_dnsimple_email | |
TF_VAR_dnsimple_token | |
AWS_ACCESS_KEY_ID | |
AWS_SECRET_ACCESS_KEY | |
AWS_DEFAULT_REGION | |
).each do |name| | |
errors << name if ENV[name].nil? | |
end | |
if errors.any? | |
abort "One or more environment variables are empty: #{errors.join(", ")}" | |
end | |
end | |
task :configure => :environment do | |
bucket = "example-terraform-state" | |
key = "terraform.tfstate" | |
sh %Q!terraform remote config -backend=S3 -backend-config="bucket=#{bucket}" -backend-config="key=#{key}"! | |
end | |
task :pull => :configure do | |
sh "terraform remote pull" | |
end | |
task :push => :configure do | |
sh "terraform remote push" | |
end | |
task :plan do | |
sh "terraform plan" | |
end | |
task :apply do | |
sh "terraform apply" | |
end | |
Rake::Task[:plan].enhance([:pull]) | |
Rake::Task[:apply].enhance([:pull]) do | |
Rake::Task[:push].invoke | |
end | |
task :default => :plan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment