An Engineers Guide to the AWS Ruby SDK
-
Upload
richard-spindler -
Category
Technology
-
view
99 -
download
1
Transcript of An Engineers Guide to the AWS Ruby SDK
An Engineer’s Guide to theAWS 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 SDKA high level view…
First: Some really obvious stuff:
▷Never put credentials in your code
▷Never check in files with credentialsNow, on with the code mix:
▷Quick overview▷Standard examples from the documentation
▷Some examples I came up with
Version 1:More straightforwardSimpler to get startedNo resources, paginationMore examplesLess functionalityNo longer current
Two versions of SDK:
Version 2:Less straightforwardSimpler to use long termResources, pagination, waitersLess examplesMore functionalityCurrent
…can use both in same code if needed
Client:More proceduralMore codeEasier for those used to the CLIFar more examplesBulk operations
SDK V2: Two approaches…
Resource: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 resources…
Version 2:
ec2 = Aws::EC2::Resource.newec2.instances.terminate
…in reality didn’t 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 resp.contents.map(&:key)end
SDK v2 has pagination…
Version 2:
client = Aws::S3::Client.newresp = 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 = 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) endend
SDK v2 has waiters…
Version 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 objects…
Version 2:
s3 = Aws::S3::Resource.newS3.bucket(name).objects.each do |object| puts object.keyend
…another 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_exists
…yet 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.rebootend
…yet 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 = “/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.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 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 [email protected]