Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ stacks:
purpose: vpc
```

## AWS Credentials

The AWS credential discovery support by the AWS SDKs will be used as default.
You can also set an AWS profile per stack. This can be set in
stack_defaults or region_defaults. For example

``` yaml
stack_defaults:
profile: company_a
```

## S3

StackMaster can optionally use S3 to store the templates before creating a stack.
Expand Down
22 changes: 20 additions & 2 deletions lib/stack_master/aws_driver/cloud_formation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ def region
@region ||= ENV['AWS_REGION'] || Aws.config[:region] || Aws.shared_config.region
end

def profile_name
@profile_name ||= ENV['AWS_PROFILE'] || Aws.config[:profile_name]
end

def set_region(value)
if region != value
@region = value
@cf = nil
end
end

def set_profile(value)
if profile_name != value
@profile_name = value
@cf = nil
end
end

def_delegators :cf, :create_change_set,
:describe_change_set,
:execute_change_set,
Expand All @@ -33,9 +44,16 @@ def set_region(value)
private

def cf
@cf ||= Aws::CloudFormation::Client.new(region: region, retry_limit: 10)
end
@cf ||= begin
params = {
region: region,
retry_limit: 10,
}
params[:credentials] = Aws::SharedCredentials.new(profile_name: profile_name) if profile_name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like it would be easier to just over-write the environment variable rather than pass it down the stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From memory I did it this way so that if you simply do stack_master apply different stacks can have different profiles. i.e. the Chine situation


Aws::CloudFormation::Client.new(params)
end
end
end
end
end
20 changes: 16 additions & 4 deletions lib/stack_master/aws_driver/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ def set_region(region)
@s3 = nil
end

def upload_files(bucket: nil, prefix: nil, region: nil, files: {})
def set_profile(value)
@profile_name = value
@s3 = nil
end

def upload_files(bucket: nil, prefix: nil, region: nil, profile: nil, files: {})
raise StackMaster::AwsDriver::S3ConfigurationError, 'A bucket must be specified in order to use S3' unless bucket

return if files.empty?

s3 = new_s3_client(region: region)
s3 = new_s3_client(region: region, profile: profile)

current_objects = s3.list_objects(
prefix: prefix,
Expand Down Expand Up @@ -58,8 +63,15 @@ def url(bucket:, prefix:, region:, template:)

private

def new_s3_client(region: nil)
Aws::S3::Client.new(region: region || @region)
def new_s3_client(region: nil, profile: nil)
params = {
region: region || @region
}
profile_name = profile || @profile_name

params[:credentials] = Aws::SharedCredentials.new(profile_name: profile_name) if profile_name

Aws::S3::Client.new(params)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/stack_master/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def execute!
end

StackMaster.cloud_formation_driver.set_region(region)
StackMaster::Commands::Delete.perform(region, args[1])
StackMaster::Commands::Delete.perform(config, region, args[1])
end
end

Expand Down Expand Up @@ -196,6 +196,7 @@ def execute_stacks_command(command, args, options)
end if options.changed
stack_definitions.each do |stack_definition|
StackMaster.cloud_formation_driver.set_region(stack_definition.region)
StackMaster.cloud_formation_driver.set_profile(stack_definition.profile)
StackMaster.stdout.puts "Executing #{command.command_name} on #{stack_definition.stack_name} in #{stack_definition.region}"
command_results.push command.perform(config, stack_definition, options).success?
end
Expand Down
9 changes: 8 additions & 1 deletion lib/stack_master/commands/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ class Delete
include Command
include StackMaster::Prompter

def initialize(region, stack_name)
def initialize(config, region, stack_name)
@config = config
@region = region
@stack_name = stack_name
@from_time = Time.now
end

def stack
return unless @config
@stack ||= @config.filter(@region, @stack_name).first
end

def perform
cf.set_profile(stack.profile) if stack

return unless check_exists

Expand Down
2 changes: 2 additions & 0 deletions lib/stack_master/stack_definition.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module StackMaster
class StackDefinition
attr_accessor :region,
:profile,
:stack_name,
:template,
:tags,
Expand Down Expand Up @@ -30,6 +31,7 @@ def initialize(attributes = {})
def ==(other)
self.class === other &&
@region == other.region &&
@profile == other.profile &&
@stack_name == other.stack_name &&
@template == other.template &&
@tags == other.tags &&
Expand Down
1 change: 1 addition & 0 deletions lib/stack_master/stack_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def no_echo_params?
def stack
return @stack if defined?(@stack)
StackMaster.cloud_formation_driver.set_region(stack_definition.region)
StackMaster.cloud_formation_driver.set_profile(stack_definition.profile)
@stack = find_stack
end

Expand Down
8 changes: 8 additions & 0 deletions lib/stack_master/test_driver/cloud_formation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ def region
@region ||= ENV['AWS_REGION'] || Aws.config[:region] || Aws.shared_config.region
end

def profile_name
@profile_name ||= ENV['AWS_PROFILE'] || Aws.config[:profile_name]
end

def set_region(region)
@region = region
end

def set_profile(profile_name)
@profile_name = profile_name
end

def reset
@stacks = {}
@templates = {}
Expand Down
5 changes: 4 additions & 1 deletion lib/stack_master/test_driver/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ def initialize
def set_region(_)
end

def set_profile(_)
end

def reset
@files = Hash.new { |hash, key| hash[key] = Hash.new }
end

def upload_files(bucket: nil, prefix: nil, region: nil, files: {})
def upload_files(bucket: nil, prefix: nil, region: nil, profile_name: nil, files: {})
return if files.empty?

files.each do |template, file|
Expand Down
2 changes: 1 addition & 1 deletion spec/stack_master/commands/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RSpec.describe StackMaster::Commands::Delete do

subject(:delete) { described_class.new(stack_name, region) }
subject(:delete) { described_class.new(nil, stack_name, region) }
let(:cf) { Aws::CloudFormation::Client.new }
let(:region) { 'us-east-1' }
let(:stack_name) { 'mystack' }
Expand Down
4 changes: 2 additions & 2 deletions spec/stack_master/commands/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
subject(:status) { described_class.new(config, false) }
let(:config) { instance_double(StackMaster::Config, stacks: stacks) }
let(:stacks) { [stack_definition_1, stack_definition_2] }
let(:stack_definition_1) { double(:stack_definition_1, region: 'us-east-1', stack_name: 'stack1') }
let(:stack_definition_2) { double(:stack_definition_2, region: 'us-east-1', stack_name: 'stack2', stack_status: 'CREATE_COMPLETE') }
let(:stack_definition_1) { double(:stack_definition_1, region: 'us-east-1', profile: nil, stack_name: 'stack1') }
let(:stack_definition_2) { double(:stack_definition_2, region: 'us-east-1', profile: nil, stack_name: 'stack2', stack_status: 'CREATE_COMPLETE') }
let(:cf) { Aws::CloudFormation::Client.new(region: 'us-east-1') }

before do
Expand Down