Class: AwsRegion

Inherits:
AwsBase show all
Defined in:
lib/aws_region.rb

Overview

AwsRegion is a simplified wrapper on top of a few of the Aws core objects The main goal is to expose a extremely simple interface for some our most frequently used Aws facilities.

Defined Under Namespace

Classes: AwsBucket, AwsCw, AwsDbInstance, AwsInstance, AwsSns

Constant Summary collapse

REGIONS =
{'or' => "us-west-2", 'ca' => "us-west-1", 'va' => 'us-east-1'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AwsBase

#log

Constructor Details

#initialize(region, account_id, access_key_id, secret_access_key, logger = nil) ⇒ AwsRegion

Returns a new instance of AwsRegion.

Parameters:

  • region (String)

    must be one of the keys of the REGIONS static hash

  • account_id (String)

    Aws account id

  • access_key_id (String)

    Aws access key id

  • secret_access_key (String)

    Aws secret access key


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aws_region.rb', line 23

def initialize(region, , access_key_id, secret_access_key, logger = nil)
  @logger = logger
  @region = REGIONS[region]
  @account_id = 
  Aws.config = {:access_key_id => access_key_id,
                :secret_access_key => secret_access_key}
  @ec2 = Aws::EC2::Client.new(region: @region)
  @rds = Aws::RDS::Client.new(region: @region)
  @elb = Aws::ElasticLoadBalancing::Client.new(region: @region)
  @cw = Aws::CloudWatch::Client.new(region: @region)
  @s3 = Aws::S3::Client.new(region: @region)
  @sns = Aws::SNS::Client.new(region: @region)
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id


16
17
18
# File 'lib/aws_region.rb', line 16

def 
  @account_id
end

#cwObject

Returns the value of attribute cw


16
17
18
# File 'lib/aws_region.rb', line 16

def cw
  @cw
end

#ec2Object

Returns the value of attribute ec2


16
17
18
# File 'lib/aws_region.rb', line 16

def ec2
  @ec2
end

#elbObject

Returns the value of attribute elb


16
17
18
# File 'lib/aws_region.rb', line 16

def elb
  @elb
end

#rdsObject

Returns the value of attribute rds


16
17
18
# File 'lib/aws_region.rb', line 16

def rds
  @rds
end

#regionObject

Returns the value of attribute region


16
17
18
# File 'lib/aws_region.rb', line 16

def region
  @region
end

#s3Object

Returns the value of attribute s3


16
17
18
# File 'lib/aws_region.rb', line 16

def s3
  @s3
end

#snsObject

Returns the value of attribute sns


16
17
18
# File 'lib/aws_region.rb', line 16

def sns
  @sns
end

Instance Method Details

#create_bucket(options = {}) ⇒ AwsBucket

Construct new AwsBucket instance

Parameters:

Returns:


128
129
130
# File 'lib/aws_region.rb', line 128

def create_bucket(options={})
  AwsBucket.new(self, options)
end

#create_cw_instance(options = {}) ⇒ AwsCw

Construct new CloudWatch instance

Parameters:

Returns:


120
121
122
# File 'lib/aws_region.rb', line 120

def create_cw_instance(options={})
  AwsCw.new(self, options)
end

#create_db_instance(options = {}) ⇒ AwsDbInstance

Construct new DB instance

Parameters:

Returns:


112
113
114
# File 'lib/aws_region.rb', line 112

def create_db_instance(options={})
  AwsDbInstance.new(self, options)
end

#create_instance(options = {}) ⇒ AwsInstance

Construct new EC2 instance

Parameters:

Returns:


104
105
106
# File 'lib/aws_region.rb', line 104

def create_instance(options={})
  AwsInstance.new(self, options)
end

#create_sns_instanceObject


132
133
134
# File 'lib/aws_region.rb', line 132

def create_sns_instance
  AwsSns.new(self)
end

#find_buckets(options = {}) ⇒ Array<AwsBucket>

Search region for a bucket by name

Parameters:

  • options (Hash) (defaults to: {})

    containing search criteria. Values can be:

    • :bucket - Bucket name

Returns:

  • (Array<AwsBucket>)

    instances found to match criteria


91
92
93
94
95
96
97
98
# File 'lib/aws_region.rb', line 91

def find_buckets(options={})
  buckets = []
  _buckets = @s3.list_buckets()
  _buckets[:buckets].each do |b|
    buckets << AwsBucket.new(self, {id: b[:name]}) if b[:name] == options[:bucket]
  end
  buckets
end

#find_db_instances(options = {}) ⇒ Array<AwsDbInstance>

Simple DB Instance finder. Can find using instance_id, or using :environment and :purpose instance tags which must both match.

Parameters:

  • options (Hash) (defaults to: {})

    containing search criteria. Values can be:

    • :instance_id - identifies an exact instance

    • :environment - instance tag

    • :purpose - instance tag

Returns:


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/aws_region.rb', line 70

def find_db_instances(options={})
  instances = []
  @rds.describe_db_instances[:db_instances].each do |i|
    instance = AwsDbInstance.new(self, {:instance => i})
    if options.has_key?(:instance_id)
      instance.id == options[:instance_id]
      instances << instance
    elsif instance.tags[:environment] == options[:environment] and
        instance.tags[:purpose] == options[:purpose]
      instances << instance
    end
  end
  instances
end

#find_instances(options = {}) ⇒ Array<AwsInstance>

Simple EC2 Instance finder. Can find using instance_id, or using :environment and :purpose instance tags which must both match.

Parameters:

  • options (Hash) (defaults to: {})

    containing search criteria. Values can be:

    • :instance_id - identifies an exact instance

    • :environment - instance tag

    • :purpose - instance tag

Returns:

  • (Array<AwsInstance>)

    instances found to match criteria


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aws_region.rb', line 45

def find_instances(options={})
  instances = []
  @ec2.describe_instances[:reservations].each do |i|
    i.instances.each do |y|
      instance = AwsInstance.new(self, {:instance => y})
      if instance.state != 'terminated'
        if options.has_key?(:environment) and options.has_key?(:purpose)
          instances << instance if  instance.tags[:environment] == options[:environment] and instance.tags[:purpose] == options[:purpose]
        elsif options.has_key?(:instance_id)
          instances << instance if instance.id == options[:instance_id]
        end
      end
    end
  end
  return instances
end