An Engineers Guide to the AWS Ruby SDK

download An Engineers Guide to the AWS Ruby SDK

of 24

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of An Engineers Guide to the AWS Ruby SDK


An Engineers Guide to theAWS Ruby SDK

Caveat:Im an Engineer, not a developer.I script, I dont code, this wont be pretty...

Goals of this presentationIntroductionQuick overview of Ruby SDKPass on some lessons learnedSave you some time and painRecommendations based on experience

So why use the Ruby SDK?Besides CLI Naming inconsistencies

Besides being a good way to learn AWSI didnt 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 pairAlthough the CLI has quite good help, the naming and use of tags and switches is frustratingly inconsistentThe 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 thingsI could create and deploy a simple instance for testing with one or two commandsWrote the JSON/config for me based on sensible defaults (also avoid typos)Automatically check IP rules, keys, etc


Using the AWS Ruby SDKA high level view

First: Some really obvious stuff:Never put credentials in your codeNever check in files with credentials

Now, on with the code mix:Quick overviewStandard examples from the documentationSome examples I came up with

Version 1:More straightforwardSimpler to get startedNo resources, paginationMore examplesLess functionalityNo longer currentTwo versions of SDK:Version 2:Less straightforwardSimpler to use long termResources, pagination, waitersLess examplesMore functionalityCurrentcan use both in same code if needed

Client:More proceduralMore codeEasier for those used to the CLIFar more examplesBulk operationsSDK V2: Two approachesResource:More object likeLess codeEasier for those used to RubyVery few examplesObject manipulation

Using the AWS Ruby SDKSome slightly heavier lifting V1 v V2

Version 1:

ec2 = AWS::EC2.newec2.instances.each do |instance| instance.terminateend SDK v2 has resourcesVersion 2:

ec2 = Aws::EC2::Resource.newec2.instances.terminatein reality didnt use resources as much as I would normally

Version 1:

client = AWS::S3::Client.newresp = 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 SDK v2 has paginationVersion 2:

client = Aws::S3::Client.newresp = client.list_objects(bucket_name:name)resp.each do |page| puts page.contents(&:key)endmain reason to use SDK v2

Version 1:

MAX_ATTEMPTS = 10client = AWS::EC2::Client.newinstance = e2c.instances[instance_id]running = falseattempts = 0until running do if instance.status == :running running = true else attempts += 1 break if attempts >= MAX_ATTEMPTS sleep(10) endendSDK v2 has waitersVersion 2:

client = Aws::EC2::Client.newclient.wait_until( :instance_running, instance_ids: [instance_id])yet another reason to use SDK v2

Using the AWS Ruby SDKA mix of V2 SDK examples

Iterating over some bucket objectsVersion 2:

s3 = Aws::S3::Resource.newS3.bucket(name).objects.each do |object| puts object.keyendanother reason to use SDK v2

Version 2 Client:

client = Aws::S3::Client.newclient.wait_until( :object_exists, bucket: bucket_name, key: object_key)SDK v2 client and resource (S3)Version 2 Resource:

s3 = Aws::S3::Resource.newS3.bucket(bucket_name).object(object_key).wait_until_existsyet another reason to use SDK v2

Version 2 Client:

ec2 = Aws::EC2::Client.newinstance_ids = []ec2.describe_instances['reservations'].each do |reservation| reservation['instances'].each do |instance| instance_ids.push instance.instance_id endendec2.reboot_instances(instance_ids: instance_ids)SDK v2 client and resource (EC2)Version 2 Resource:

ec2 = Aws::EC2::Resource.newec2.instances.each do |instance| instance.rebootendyet another reason to use SDK v2

Version 2 Client:

ec2 = Aws::EC2::Client.newkey_pair = ec2.create_key_pair({ key_name: key_name}).key_materialkey_file = /, w) { |f| f.write(key_pair) }Creating and saving a key pairyet another reason to use SDK v2

Version 2 Resource Object Upload:

s3 = Aws::S3::Resource.news3.bucket(bucket_name).object(object_key).upload_file(file_name)

Version 2 Client Object Download:

s3 = Aws::S3::Client.news3.get_object({ bucket: bucket_name, key: object_key,}, target: file_name )

S3 bucket uploads and downloadsyet another reason to use SDK v2

Summary:SDK >> CLI, and take a look at the Python SDK

So wheres this all goingSDK is worth learningFar nicer and more consistent than the CLIEasy to iterate through itemsObject like in nature, more malleableMore elegant, less codeCan wait rather than sleepSo take a look at the SDKWhile youre at it, take a look at the Python SDK, its more straight forward than Ruby

Huh, what you say? Python SDK?But regex is torture in PythonIf youre familiar with Python, and havent got an existing codebase of Ruby to support Id recommend using the Python SDKPython SDK is a bit more straight forwardA lot of other deployment and config tools are written in Python (less confusion)At some point youll want to get/do something using REST (borked on Ruby)

Thanks for your