37
Introduction to AWS SDK for Ruby Introduction to AWS SDK for Ruby ISOBE Kazuhiko (cloudpack) JAWS-UG静岡 2011-11-27 Powered by Rabbit 1.0.4 and COZMIXNG

JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

Embed Size (px)

DESCRIPTION

JAWS-UG静岡の第一回 LT

Citation preview

Page 1: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

Introduction to AWS SDK

for Ruby

Introduction to AWS SDK

for RubyISOBE Kazuhiko

(cloudpack)JAWS-UG静岡 2011-11-27

Powered by Rabbit 1.0.4 and COZMIXNG

Page 2: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

提供

このLTはcloudpackの提供でお送りいたします

01 36

Page 3: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

cloudpack

AWS導入・構築支援、コンサルティング、システム構築サービス2010年4月 サービス開始2011年1月 AWS ソリューションプロバイダ認定

02 36

Page 4: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

cloudpack

月額費用固定型日本円で請求書発行

フルマネージドホスティング

03 36

Page 5: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

cloudpack

詳しくはWebで!

http://cloudpack.jp/

04 36

Page 6: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

自己紹介

Twitter: muramasa64

cloudpackでAWSを運用好きなAWSサービス: API

05 36

Page 7: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

AWS SDK for Ruby

AWS公式のSDK

2011年7月14日に初リリース最新版は1.2.3

06 36

Page 8: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

できること

RubyからAWSのAPIを実行できるEC2, ELB, S3, SNS, SQS, STS, SES, IAM, SimpleDB, VPCに対応

RDS未対応が惜しい07 36

Page 9: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

できること

Ruby on Rails の ActiveRecordで、SimpleDBが使えるまだリリースから間もないので、他のSDKに比べてできることはやや少なめ

08 36

Page 10: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

入手方法

gem

gem install aws-sdk

githubgit clone git://github.com/amazonwebservices/aws-sdk-for-ruby.git

09 36

Page 11: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

使う準備

config.yml

access_key_id: <YOUR_ACCESS_KEY>secret_access_key: <YOUR_SECRET_KEY>

10 36

Page 12: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

ライブラリと設定

require 'aws-sdk'

AWS.config( YAML.load(File.read('config.yml')))

 

11 36

Page 13: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

EC2

EC2を起動する

ec2 = AWS::EC2.newec2.instances.create( :image_id => 'ami-f49623f5' :key_name => 'keypair_name' :security_group_ids => ['default'])

12 36

Page 14: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

Snapshot

各ボリュームのスナップショットを作成する

ec2.volumes.each do |v| v.create_snapshot( "from #{v.id} at #{Time.now}") end

13 36

Page 15: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

Security Group

IPアドレスを一括で置換するsrc_ip = 'x.x.x.x/32'dst_ip = 'y.y.y.y/32'

ec2.security_groups.each do |src_sg| puts "#{src_sg.id}, #{src_sg.name}"

src_sg.ip_permissions.each do |p| if p.ip_ranges.include? src_ip src_sg.authorize_ingress(p.protocol, p.port_range, dst_ip) src_sg.revoke_ingress(p.protocol, p.port_range, src_ip) end endend

14 36

Page 16: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

S3

期間限定URIを生成s3 = AWS::S3.newb = s3.buckets['your-bucket-name']o = b.objects['object-name']puts o.url_for(:read, :expires => 60*60*24*31)

一ヶ月間有効なURLが生成される15 36

Page 17: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

S3 Presignd-URLhttps://your-bucket-name.s3.amazonaws.com/object-name?AWSAccessKeyId=AKIAIFCOKTCTDSDMPX2A&Expires=1315125436&Signature=zEmS9f4%2Fm%2BFLgxEsVA9ppfQDhks%3D16 36

Page 18: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

ちょっとしたハマり所

AWS SDK for Rubyで遭遇した ハマった事例

17 36

Page 19: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

最新のSnapshotas = []ec2.volumes.each do |v| v.attachments.each do |a| as << a endend

ss = {}r.snapshots.sort {|a,b| b.start_time <=> a.start_time}.map do |s| ss[s.volume_id] = s unless ss.include? s.volume_idend

as.each do |a| vid = a.volume.id if ss.include? vid puts "#{a.instance.id} => (#{ss[vid].start_time})" endend

18 36

Page 20: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

タイムアウトして失敗Snapshotの作成日時を参照するたびにAPIが呼び出されてしまうらしい大量にあるSnapshotをソートすると、必ずタイムアウトしてしまう

APIの呼び出し制限?19 36

Page 21: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

AWS.memoize

AWS.memoize do ss = {} r.snapshots.sort {|a,b| b.start_time <=> a.start_time }.map do |s| ss[s.volume_id] = s unless ss.include? s.volume_id endend

ソートしている部分をAWS.memoizeで囲むだけ

20 36

Page 22: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

Snapshotの一覧自分のSnapshotの一覧が欲しい

ec2.snapshots.each do |s| # ...end

これだと、publicなSnapshotも取れてしまう!21 36

Page 23: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

AWS Developer Forum

Forum: Ruby Developmenthttps://forums.aws.amazon.com/forum.jspa?forumID=125

 

22 36

Page 24: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

質問した

23 36

Page 25: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

次の日24 36

Page 26: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

AWSの中の人から回答

25 36

Page 27: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

Snapshotの一覧

解決策: filter

ec2.snapshots.filter( 'owner-id', 'YOUR_ACCOUNT_NUMBER').each do |s| # ...end

自分のアカウント番号を指定すると自分のSnapshotのみ取得できる。

26 36

Page 28: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

公式のSDKのメリット

AWSのサポートが得られる

27 36

Page 29: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

16日後28 36

Page 30: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

AWS SDK for Ruby 1.1.0 Release

29 36

Page 31: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

Release Note

30 36

Page 32: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

forumにもコメント

31 36

Page 33: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

自分のSnapshotの取得が簡単に

ec2.snapshots.with_owner(:self).each do |s| # ...end

AMIの一覧も同じようにできます。 

32 36

Page 34: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

公式SDKのメリット

 フォーラムに困ってることや要望を書きこむと、新しい機能が実装されることも!

33 36

Page 35: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

困ったことや要望があったら

フォーラムに書くなどのフィードバックしよう次のリリースに反映されるかも

34 36

Page 36: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

サンタクラウド

#jawsug #サンタクラウド集計ページ

http://www.suz-lab.com/santacloud/35 36

Page 37: JAWS-UG静岡 #1 Introduction to AWS SDK for Ruby

 

ご静聴ありがとうございました

Powered by Rabbit 1.0.4 and COZMIXNG

36 36