Thursday, February 6, 2014

Amazon AWS basics in Rails

Listing Amazon EC2 instances

# Class for VM's 
class Vm
  include ActiveModel::Validations

  validates_presence_of :name, :status

  attr_accessor :id, :name, :dns, :status
  @@ec2 = AWS::EC2.new()

  def self.all
    return @@ec2.instances.inject([]) { |m, i|
      tags = i.tags.to_h
      name = tags['Name']
      m.push Vm.new( i.id, name, i.dns_name, i.status )
    }
  end

  def initialize( id, name, dns,  status )
    @id     = id
    @name   = name
    @dns    = dns
    @status = status
  end

end

Listing Amazon S3 buckets

# Class for S3 Storage
class Storage
include ActiveModel::Validations
 
validates_presence_of :name
 
attr_accessor :name
@@ec2 = AWS::S3.new()
 
def self.all
return @@ec2.buckets.inject([]) { |m, i|
    m.push Storage.new( i.name )
}
end
 
def initialize( name )
@name = name
end
 
end

AWS Config

config/initializers/aws-sdk.rb
AWS.config({
  :secret_access_key => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  :access_key_id => 'YYYYYYYYYYYYYYY',
  :region => 'us-west-2',
})



1 comment:

  1. I don't have any idea what this is all about but that can b helpful for someone having knowledge about it. Thanks for the share!

    ReplyDelete