diff --git a/README.md b/README.md index b938108..2f1eea0 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ set :ec2_project_tag, 'Project' set :ec2_roles_tag, 'Roles' set :ec2_stages_tag, 'Stages' +set :ec2_profile, 'myservice' # use ~/.aws/credentials with profile_name set :ec2_access_key_id, nil set :ec2_secret_access_key, nil set :ec2_region, %w{} # REQUIRED @@ -59,7 +60,7 @@ set :ec2_filter_by_status_ok?, nil #### Order of inheritance `cap-ec2` supports multiple methods of configuration. The order of inheritance is: -YAML File > User Capistrano Config > Default Capistrano Config > ENV variables. +YAML File > ~/.aws/credentials > User Capistrano Config > Default Capistrano Config > ENV variables. #### Regions diff --git a/lib/cap-ec2/utils.rb b/lib/cap-ec2/utils.rb index ae23be8..a6adbb0 100644 --- a/lib/cap-ec2/utils.rb +++ b/lib/cap-ec2/utils.rb @@ -1,3 +1,5 @@ +require 'aws-sdk' + module CapEC2 module Utils @@ -40,6 +42,14 @@ def self.contact_point(instance) end def load_config + if fetch(:ec2_profile) + credentials = Aws::SharedCredentials.new(profile_name: fetch(:ec2_profile)).credentials + if credentials + set :ec2_access_key_id, credentials.access_key_id + set :ec2_secret_access_key, credentials.secret_access_key + end + end + config_location = File.expand_path(fetch(:ec2_config), Dir.pwd) if fetch(:ec2_config) if config_location && File.exists?(config_location) config = YAML.load_file fetch(:ec2_config)