Project

aws_ro

0.02
No commit activity in last 3 years
No release in over 3 years
Wrpper library of AWS SDK objects to enable to access properties more easily, more ruby-likely.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

AwsRo

Wrapper class of AWS Resource objects to enable to access properties more easily, more ruby-likely.

The targets of this library are small and medium scale AWS systems.

Now supported only Aws::EC2, Aws::ElasticLoadBalancing and Aws::ElasticLoadBalancingV2.

Installation

Add this line to your application's Gemfile:

gem 'aws_ro', '~> 1.1'

And then execute:

$ bundle

Purpose of this gem

Easy access to AWS Resources.

For example, I'd like to print values of 'MyTag' tag of instances which also contains 'MyEnv'=='develop' tag.

By using plain AWS SDK v2:

client = Aws::EC2::Client.new(some_options)
res = client.describe_instances(filters: [{name: 'tag:MyEnv', values: ['develop']}, {name: 'tag:MyTag', values: ['*']}])
instances = res.reservations.flat_map(&:instances)
instances.each do |i|
  v = i.tags.find { |t| t.key == 'MyTag' }.value
  puts "ID: #{i.instance_id}, MyTag value: #{v}"
end

This is not difficult, but is a little boring.

On the other hand, by using the aws_ro gem:

repo = AwsRo::EC2::Repository.new(some_options)
repo.tags({'MyEnv' => 'develop', 'MyTag' => '*'}).each do |i|
  puts "ID: #{i.instance_id}, MyTag value: #{i.my_tag}"
end

This is simplified and keeping readability.

Usage

Basic Usage

ec2_options = { region: 'ap-northeast-1' }
repo = AwsRo::EC2::Repository.new(ec2_options)
instances = repo.running.tags({'MyAttrName' => 'MyValue'})
instances.each do |i|
  puts "#{i.name} #{i.public_ip_address}, #{i.my_attr_name}"
end
puts "Print ALB rules and targets"
options = { region: 'ap-northeast-1' }
albs = AwsRo::ElasticLoadBalancingV2::Repository.new(options).all
albs.all.each do |lb|
  puts "LoadBalancer: #{lb.name}"
  lb.listeners.each do |listener|
    puts "- #{listener.protocol} (#{listener.port})"
    listener.rules.each do |rule|
      pattern = rule.default? ? '<blank>' : rule.path_pattern
      puts "  - #{pattern} : #{rule.forward_target_group.name} (#{rule.priority})"
    end
  end
end

Classes

class : AwsRo::EC2::Repository

Repository is wrapper of EC2 client. It supports chainable queries to describe instances.

Initialize
# initialize with Aws::EC2::Client
client.class
# => Aws::EC2::Client
repo = AwsRo::EC2::Repository.new(client)

# initialize with option hash
repo = AwsRo::EC2::Repository.new({ region: 'ap-northeast-1' })
accessor
# get raw client
repo.client
# => Aws::EC2::Client
query methods
# all running instance
repo.running.all? do |i|
  i.class
  # => AwsRo::EC2::Instance
  i.state.name == 'running'
end
# => true

# All query methods are chainable and return an `Array` like object.
## list security group of running-public-instances
repo.running.filters([{name: 'ip-address', values: ['*'] }]).each do |i|
  puts i.ec2.security_groups.map(&:group_name)
end

## list my 'InUse' tag instances
repo.not_terminated.tags('InUse' => '*').map(&:instance_id)
# => ["i-xxxxxxxx","i-yyyyyyyy","i-zzzzzzzz", ...]

class : AwsRo::EC2::Instance

Instance is wrapper of EC2 instace object.

ins = repo.all.first
static accessors
# get raw ec2 object
ins.ec2
# => Aws::EC2::Instance

# some delegated methods and aliases for accessibility
[
  ins.instance_id == ins.ec2.instance_id,
  ins.public_ip_address == ins.ec2.public_ip_address && ins.public_ip_address == ins.public_ip,
  ins.private_ip_address == ins.ec2.private_ip_address && ins.private_ip == ins.private_ip,
  ins.key_name == ins.ec2.key_name,
  ins.state == ins.ec2.state
].all?
# => true
dynamic tag accessors
# Instance#tags returns `Struct` of tags.
ins.tags.Name
# => 'Value of Name tag'
ins.tags.roles
# => 'staging, somerole'
ins.tags.xyz_enabled
# => 'True'

## and dynamically-defined reader methods are available as snake_case-ed tag name.
## their values are stripped and formatted as `Array` if the value include whitespaces,
## as boolean if the value like  boolean and when use `?`-terminated method.
ins.name
# => 'Value of Name tag'
ins.roles
# => ['staging', 'somerole']
ins.xyz_enabled?
# => true

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/gree/aws_ro/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

This library is distributed under the Apache License, version 2.0

copyright 2016. GREE, Inc. all rights reserved.

licensed under the apache license, version 2.0 (the "license");
you may not use this file except in compliance with the license.
you may obtain a copy of the license at

    http://www.apache.org/licenses/license-2.0

unless required by applicable law or agreed to in writing, software
distributed under the license is distributed on an "as is" basis,
without warranties or conditions of any kind, either express or implied.
see the license for the specific language governing permissions and
limitations under the license.