An Engineer’s
Guide to the
AWS Ruby SDK
Caveat:
I’m an Engineer, not a developer.
I script, I don’t code, this won’t be pretty...
Goals of this presentation
▷Introduction
▷Quick overview of Ruby SDK
▷Pass on some lessons learned
▷Save you some time and pain
▷Recommendations based on
experience
So why use the Ruby SDK?
Besides CLI Naming inconsistencies…
Besides being a good way to
learn AWS…
▷I didn’t want to have to go to the web
interface every time I wanted to do a
simple task like adding a security group or
generating a key pair
▷Although the CLI has quite good help, the
naming and use of tags and switches is
frustratingly inconsistent
▷The SDK is actually nicer to deal with
than the CLI and is far more consistent
I already had my own Ruby
based deployment tool [1]
▷Supported packer, managing VMs and
many other things
▷I could create and deploy a simple
instance for testing with one or two
commands
▷Wrote the JSON/config for me based on
sensible defaults (also avoid typos)
▷Automatically check IP rules, keys, etc
[1] https://github.com/lateralblast/mode
Using the AWS Ruby SDK
A high level view…
First: Some really obvious stuff:
▷Never put credentials in your code
▷Never check in files with credentials
Now, on with the code mix:
▷Quick overview
▷Standard examples from the documentation
▷Some examples I came up with
Version 1:
More straightforward
Simpler to get started
No resources, pagination
More examples
Less functionality
No longer current
Two versions of SDK:
Version 2:
Less straightforward
Simpler to use long term
Resources, pagination, waiters
Less examples
More functionality
Current
…can use both in same code if needed
Client:
More procedural
More code
Easier for those used to the
CLI
Far more examples
Bulk operations
SDK V2: Two approaches…
Resource:
More object like
Less code
Easier for those used to
Ruby
Very few examples
Object manipulation
Using the AWS Ruby SDK
Some slightly heavier lifting… V1 v V2…
Version 1:
ec2 = AWS::EC2.new
ec2.instances.each do |instance|
instance.terminate
end
SDK v2 has resources…
Version 2:
ec2 = Aws::EC2::Resource.new
ec2.instances.terminate
…in reality didn’t use resources as much as I would normally
Version 1:
client = AWS::S3::Client.new
resp = client.list_objects(bucket_name:name)
while resp.truncated do
marker = resp.marker || resp.contents.last.key
resp = client.list_objects(
bucket_name: bucket_name,
marker: marker
)
puts resp.contents.map(&:key)
end
SDK v2 has pagination…
Version 2:
client = Aws::S3::Client.new
resp = client.list_objects(bucket_name:name)
resp.each do |page|
puts page.contents(&:key)
end
…main reason to use SDK v2
Version 1:
MAX_ATTEMPTS = 10
client = AWS::EC2::Client.new
instance = e2c.instances[instance_id]
running = false
attempts = 0
until running do
if instance.status == :running
running = true
else
attempts += 1
break if attempts >= MAX_ATTEMPTS
sleep(10)
end
end
SDK v2 has waiters…
Version 2:
client = Aws::EC2::Client.new
client.wait_until(
:instance_running,
instance_ids: [instance_id]
)
…yet another reason to use SDK v2
Using the AWS Ruby SDK
A mix of V2 SDK examples…
Iterating over some bucket objects…
Version 2:
s3 = Aws::S3::Resource.new
S3.bucket(name).objects.each do |object|
puts object.key
end
…another reason to use SDK v2
Version 2 Client:
client = Aws::S3::Client.new
client.wait_until(
:object_exists,
bucket: bucket_name,
key: object_key
)
SDK v2 client and resource
(S3)…
Version 2 Resource:
s3 = Aws::S3::Resource.new
S3.bucket(bucket_name).object(object_key).wait_until_exists
…yet another reason to use SDK v2
Version 2 Client:
ec2 = Aws::EC2::Client.new
instance_ids = []
ec2.describe_instances['reservations'].each do |reservation|
reservation['instances'].each do |instance|
instance_ids.push instance.instance_id
end
end
ec2.reboot_instances(instance_ids: instance_ids)
SDK v2 client and resource
(EC2)…
Version 2 Resource:
ec2 = Aws::EC2::Resource.new
ec2.instances.each do |instance|
instance.reboot
end
…yet another reason to use SDK v2
Version 2 Client:
ec2 = Aws::EC2::Client.new
key_pair = ec2.create_key_pair({
key_name: key_name
}).key_material
key_file = “/location_of_key_file.key”
File.open(key_file, ’w’) {
|f| f.write(key_pair)
}
Creating and saving a key pair…
…yet another reason to use SDK v2
Version 2 Resource Object Upload:
s3 = Aws::S3::Resource.new
s3.bucket(bucket_name).object(object_key).upload_file(file_name)
Version 2 Client Object Download:
s3 = Aws::S3::Client.new
s3.get_object({
bucket: bucket_name, key: object_key,
}, target: file_name )
S3 bucket uploads and downloads…
…yet another reason to use SDK v2
Summary:
SDK >> CLI, and take a look at the Python SDK…
So where’s this all going…
▷SDK is worth learning
▷Far nicer and more consistent than the CLI
▷Easy to iterate through items
▷Object like in nature, more malleable
▷More elegant, less code
▷Can wait rather than sleep
▷So take a look at the SDK
▷While you’re at it, take a look at the Python
SDK, it’s more straight forward than Ruby
Huh, what you say? Python SDK?
▷But regex is torture in Python…
▷If you’re familiar with Python, and haven’t
got an existing codebase of Ruby to
support I’d recommend using the Python
SDK
▷Python SDK is a bit more straight forward
▷A lot of other deployment and config tools
are written in Python (less confusion)
▷At some point you’ll want to get/do
something using REST (borked on Ruby)
Thanks for your patience
richard@lateralblast.com.au

An Engineers Guide to the AWS Ruby SDK

  • 1.
  • 2.
    Caveat: I’m an Engineer,not a developer. I script, I don’t code, this won’t be pretty...
  • 3.
    Goals of thispresentation ▷Introduction ▷Quick overview of Ruby SDK ▷Pass on some lessons learned ▷Save you some time and pain ▷Recommendations based on experience
  • 4.
    So why usethe Ruby SDK? Besides CLI Naming inconsistencies…
  • 5.
    Besides being agood way to learn AWS… ▷I didn’t want to have to go to the web interface every time I wanted to do a simple task like adding a security group or generating a key pair ▷Although the CLI has quite good help, the naming and use of tags and switches is frustratingly inconsistent ▷The SDK is actually nicer to deal with than the CLI and is far more consistent
  • 6.
    I already hadmy own Ruby based deployment tool [1] ▷Supported packer, managing VMs and many other things ▷I could create and deploy a simple instance for testing with one or two commands ▷Wrote the JSON/config for me based on sensible defaults (also avoid typos) ▷Automatically check IP rules, keys, etc [1] https://github.com/lateralblast/mode
  • 7.
    Using the AWSRuby SDK A high level view…
  • 8.
    First: Some reallyobvious stuff: ▷Never put credentials in your code ▷Never check in files with credentials Now, on with the code mix: ▷Quick overview ▷Standard examples from the documentation ▷Some examples I came up with
  • 9.
    Version 1: More straightforward Simplerto get started No resources, pagination More examples Less functionality No longer current Two versions of SDK: Version 2: Less straightforward Simpler to use long term Resources, pagination, waiters Less examples More functionality Current …can use both in same code if needed
  • 10.
    Client: More procedural More code Easierfor those used to the CLI Far more examples Bulk operations SDK V2: Two approaches… Resource: More object like Less code Easier for those used to Ruby Very few examples Object manipulation
  • 11.
    Using the AWSRuby SDK Some slightly heavier lifting… V1 v V2…
  • 12.
    Version 1: ec2 =AWS::EC2.new ec2.instances.each do |instance| instance.terminate end SDK v2 has resources… Version 2: ec2 = Aws::EC2::Resource.new ec2.instances.terminate …in reality didn’t use resources as much as I would normally
  • 13.
    Version 1: client =AWS::S3::Client.new resp = client.list_objects(bucket_name:name) while resp.truncated do marker = resp.marker || resp.contents.last.key resp = client.list_objects( bucket_name: bucket_name, marker: marker ) puts resp.contents.map(&:key) end SDK v2 has pagination… Version 2: client = Aws::S3::Client.new resp = client.list_objects(bucket_name:name) resp.each do |page| puts page.contents(&:key) end …main reason to use SDK v2
  • 14.
    Version 1: MAX_ATTEMPTS =10 client = AWS::EC2::Client.new instance = e2c.instances[instance_id] running = false attempts = 0 until running do if instance.status == :running running = true else attempts += 1 break if attempts >= MAX_ATTEMPTS sleep(10) end end SDK v2 has waiters… Version 2: client = Aws::EC2::Client.new client.wait_until( :instance_running, instance_ids: [instance_id] ) …yet another reason to use SDK v2
  • 15.
    Using the AWSRuby SDK A mix of V2 SDK examples…
  • 16.
    Iterating over somebucket objects… Version 2: s3 = Aws::S3::Resource.new S3.bucket(name).objects.each do |object| puts object.key end …another reason to use SDK v2
  • 17.
    Version 2 Client: client= Aws::S3::Client.new client.wait_until( :object_exists, bucket: bucket_name, key: object_key ) SDK v2 client and resource (S3)… Version 2 Resource: s3 = Aws::S3::Resource.new S3.bucket(bucket_name).object(object_key).wait_until_exists …yet another reason to use SDK v2
  • 18.
    Version 2 Client: ec2= Aws::EC2::Client.new instance_ids = [] ec2.describe_instances['reservations'].each do |reservation| reservation['instances'].each do |instance| instance_ids.push instance.instance_id end end ec2.reboot_instances(instance_ids: instance_ids) SDK v2 client and resource (EC2)… Version 2 Resource: ec2 = Aws::EC2::Resource.new ec2.instances.each do |instance| instance.reboot end …yet another reason to use SDK v2
  • 19.
    Version 2 Client: ec2= Aws::EC2::Client.new key_pair = ec2.create_key_pair({ key_name: key_name }).key_material key_file = “/location_of_key_file.key” File.open(key_file, ’w’) { |f| f.write(key_pair) } Creating and saving a key pair… …yet another reason to use SDK v2
  • 20.
    Version 2 ResourceObject Upload: s3 = Aws::S3::Resource.new s3.bucket(bucket_name).object(object_key).upload_file(file_name) Version 2 Client Object Download: s3 = Aws::S3::Client.new s3.get_object({ bucket: bucket_name, key: object_key, }, target: file_name ) S3 bucket uploads and downloads… …yet another reason to use SDK v2
  • 21.
    Summary: SDK >> CLI,and take a look at the Python SDK…
  • 22.
    So where’s thisall going… ▷SDK is worth learning ▷Far nicer and more consistent than the CLI ▷Easy to iterate through items ▷Object like in nature, more malleable ▷More elegant, less code ▷Can wait rather than sleep ▷So take a look at the SDK ▷While you’re at it, take a look at the Python SDK, it’s more straight forward than Ruby
  • 23.
    Huh, what yousay? Python SDK? ▷But regex is torture in Python… ▷If you’re familiar with Python, and haven’t got an existing codebase of Ruby to support I’d recommend using the Python SDK ▷Python SDK is a bit more straight forward ▷A lot of other deployment and config tools are written in Python (less confusion) ▷At some point you’ll want to get/do something using REST (borked on Ruby)
  • 24.
    Thanks for yourpatience richard@lateralblast.com.au