20120723 aws meister-reloaded-awssd-kfor_ruby-php-python-public
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

20120723 aws meister-reloaded-awssd-kfor_ruby-php-python-public

  • 3,419 views
Uploaded on

ほぼ週間AWSマイスターシリーズReloadedのAWS SDK for Ruby,PHP,Python(Boto)の回の資料です。

ほぼ週間AWSマイスターシリーズReloadedのAWS SDK for Ruby,PHP,Python(Boto)の回の資料です。

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,419
On Slideshare
3,363
From Embeds
56
Number of Embeds
1

Actions

Shares
Downloads
75
Comments
0
Likes
3

Embeds 56

http://localhost 56

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. AWSマイスターシリーズ Reloaded AWS SDK for PHP & AWS SDK for Ruby & Boto(Python) 2012.07.23 エバンジェリスト 堀内 康弘(@horiuchi プリンシパルソリューションアーキテクト 荒木 靖宏(@ar1) シニアソリューションアーキテクト 片山 暁雄(@c9katayama)© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 2. Agenda AWS SDK オーバービュー AWS SDK for PHP AWS SDK for Ruby Boto(Python) まとめ© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 3. AWS SDK オーバービュー© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 4. AWSのサービス操作と言えば AWS Management Console AWSコマンドラインツール© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 5. これらの裏側では・・・ 各サービスの各操作に対して、APIが定義されている AWSでは・・・ • 人間がGUIまたはCUI越しに叩く • プログラミングしてそれを自動化・簡易化・カスタマイズ • 人間が手でやらなくてはいけない事をプログラミングして自動化で きる、これがSDKで実現可能 • しかも簡単 • AWSの実は最も優れた一面© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 6. AWS SDKとは AWSのサービスをプログラムで操作できるSDK • さまざまな言語で提供 • AWS SDK for Java • AWS SDK for .Net • AWS SDK for Ruby • AWS SDK for PHP • AWS SDK for Android • AWS SDK for iOS • 有志の方による実装(PythonやActionScript)も • 通信は原則HTTPS© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 7. 動作イメージ 起動、停止 Put,Get REST AWS SDK SOAP DB起動 バックアップ 認証情報 情報取得© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 8. 認証情報 AWSの認証キー(アクセスキー、シークレットキ-) • AWSログイン後、[アカウント]>[セキュリティ証明書]の画面 から取得 • IAMユーザーは、ManagementConsoleのIAMタブから取得© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 9. 操作の種類 例:EC2 • インスタンス起動・・・RunInstances • リブート・・・ RebootInstances • IPアドレス付与・・AllocateAddress • などさまざまな操作が、プログラムから実行可能 • http://docs.amazonwebservices.com/AWSEC2/latest/APIR eference/Welcome.html • SDKによってサポートする操作が異なる© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 10. AWS SDK FOR PHP© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 11. AWS SDK for PHPとは Amazon提供のAWS開発用のPHP向けSDK http://aws.amazon.com/jp/sdkforphp/ 環境:PHP5.2以降(5.2.17 または 5.3.5を推奨) 依存Extension • SimpleXML • JSON • PCRE • SPL • cURL© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 12. 操作可能サービス Auto Scaling IAM CloudFormation ImportExport CloudFront RDS CloudSearch S3 CloudWatch SimpleDB DynamoDB SES EC2 SNS ElastiCache SQS ElasticBeanstalk StorageGateway ELB SWF EMR© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 13. 利用方法 ダウンロードから • http://aws.amazon.com/sdkforphp Gitから • git clone git://github.com/amazonwebservices/aws-sdk- for-php.git AWSSDKforPHP Subversionから • svn co http://svn.github.com/amazonwebservices/aws- sdk-for-php.git AWSSDKforPHP PEARから • sudo pear channel-discover pear.amazonwebservices.com • sudo pear install aws/sdk© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 14. 初期設定 config.inc.php • SDK添付のconfig-sample.inc.phpにアクセスキー、 シークレットアクセスキーを記述し、ファイル名を config.inc.phpにリネーム PHPコードからSDKを呼び出し require_once sdk.class.php;© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 15. config.inc.phpCFCredentials::set(array( // 認証情報 複数設定可能 development => array( ‘key’ => ‘AKIAIXJYFIXXXXXXXXXXX, secret => xEwVD4nEun1FsYIxIS6ibj9XXXXXX, default_cache_config => ./cache, certificate_authority => false ), // デフォルトで利用する認証情報 @default => development)); © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 16. サンプル:EC2起動 <?php require_once sdk.class.php; // 呼び出しクライアントのインスタンス化 $ec2 = new AmazonEC2(); // EC2起動 $response = $ec2 -> run_instances( ami-e565ba8c, 1, 1, array( KeyName => akiok, SecurityGroup => default, InstanceType => t1.micro)); //結果表示 print_r($response);© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 17. サンプル:DynamoDBへPUT<?phprequire_once sdk.class.php;//クライアント作成$dynamodb = new AmazonDynamoDB();//アクセス先リージョン指定$dynamodb-> set_region(dynamodb.ap-northeast-1.amazonaws.com);//データをput$response = $dynamodb->put_item(array( TableName => $table_name, Item => array( id => array(AmazonDynamoDB::TYPE_NUMBER => 1 ), firstname => array(AmazonDynamoDB::TYPE_STRING => ‘Akio), lastname => array(AmazonDynamoDB::TYPE_STRING => ‘Katayama)))); © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 18. 活用法 PHPアプリのバックエンドストレージ呼び出し • DynamoDB,SimpleDB,S3を簡単に呼び出し可能 PHPベースのアプリケーションの拡張 • WordpressやEC-CubeなどのPHPベースのアプリケーションと AWSの連携 独自の管理用Webインターフェースの作成 • 定型作業を簡略化したり、管理を容易にするための独自Webイ ンターフェースの作成© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 19. PHP版SDKの便利なクラス DynamoDBSessionHandler DynamoDBを使った、HTTPのセッション共有が可能 EC2 DynamoDB ELB EC2 速い 消えない 管理不要 EC2© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 20. DynamoDBSessionHandlerの利用require_once sdk.class.php;$dynamodb = new AmazonDynamoDB();// DynamoDB Session Handler登録// 事前にDynamoDBにテーブルを作成。キー名を「id」に設定$handler = $dynamodb->register_session_handler(array( table_name => my-sessions-table));session_start();//セッションにデータ登録$_SESSION[username] = jeremy;$_SESSION[role] = admin;session_commit(); © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 21. その他Tips EC2 インスタンスRoleに対応済み • インスタンスRole = EC2インスタンス起動時に、メタ情報にア クセスキーを埋め込める仕組み • Management ConsoleのIAMタブでRoleを作成 • EC2起動時にRoleを選択 • SDK呼び出し時、設定ファイルから認証情報が取得できない場 合、メタデータからの取得を試行 • Roleを利用することで、認証情報をファイルとしてEC2上に配 置する必要がなくなる© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 22. AWS SDK FOR RUBY© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 23. AWS SDK for Rubyとは Amazon提供のAWS開発用Ruby SDK http://aws.amazon.com/sdkforruby/ 環境:ruby1.8以降 最新版 1.5.7 依存ライブラリ • crack,httparty,json,nokogiri,uuidtools Rails Integration© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 24. 操作可能サービス Amazon DynamoDB Amazon Elastic Compute Cloud (Amazon EC2) Amazon SimpleDB Amazon Simple Email Service Amazon Simple Notification Service Amazon Simple Queue Service (Amazon SQS) Amazon Simple Storage Service (Amazon S3) Amazon Simple Workflow Service Amazon Virtual Private Cloud Auto Scaling AWS Identity and Access Management Elastic Load Balancing© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 25. Ruby SDKの基本 require ‘aws-sdk’ クレデンシャル情報を渡す • アクセスキー • シークレットキー 方法としては • config.yml:YAML形式ファイル読み込み© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 26. 呼び出し例require rubygemsrequire yamlrequire aws-sdk’config = YAML.load(File.read(config_file))AWS.config(config) IAM利用時は、IAMユーザーのアクセスキー、シークレットキーを利用。 IAMユーザーでアクセス権のないメソッドを呼ぶと、例外が発生。 © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 27. サンプル:EC2 (AMI指定と鍵作成)ec2 = AWS::EC2.new # find the latest 32-bit EBS Amazon Linux AMI image = AWS.memoize do amazon_linux = ec2.images.with_owner("amazon"). filter("root-device-type", "ebs"). filter("architecture", "i386"). filter("name", "amzn-ami*") # this only makes one request due to memoization amazon_linux.to_a.sort_by(&:name).last end puts "Using AMI: #{image.id}”# generate a key pair key_pair = ec2.key_pairs.create("ruby-sample- #{Time.now.to_i}") puts "Generated keypair #{key_pair.name}, fingerprint: #{key_pair.fingerprint}" © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 28. サンプル:EC2(起動とssh接続)# launch the instanceinstance = image.run_instance(:key_pair => key_pair, :security_groups => group)sleep 1 until instance.status != :pendingputs "Launched instance #{instance.id}, status: #{instance.status}"exit 1 unless instance.status == :runningNet::SSH.start(instance.ip_address, "ec2-user", :key_data => [key_pair.private_key]) do |ssh| puts "Running uname -a on the instance yields:" puts ssh.exec!("uname -a")end © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 29. サンプル:S3# get an instance of the S3 interface using thedefault configurations3 = AWS::S3.new# create a bucketb = s3.buckets.create(bucket_name)# upload a filebasename = File.basename(file_name)o = b.objects[basename]o.write(:file => file_name)puts o.url_for(:read)o.delete © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 30. Railsでの利用(1) SimpleDBの使用 gem “aws-sdk” bundle install config/aws.yml access_key_id: REPLACE_WITH_ACCESS_KEY_ID secret_access_key: REPLACE_WITH_SECRET_ACCESS_KEY config/initializers/aws.rb require aws AWS.config(:logger => Rails.logger) config_path = File.expand_path(File.dirname(__FILE__)+"/../aws.yml") AWS.config(YAML.load(File.read(config_path)))© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 31. Railsでの利用(2) app/models/my_record.rb class MyRecord < AWS::Record::Base string_attr :name end SimpleDBドメインの作成 $ rails console > MyRecord.create_domain© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 32. BOTO(PYTHON)© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 33. Boto Python用のAWS SDK 作者は中の人(ほぼ公式といってよい?) 最新版は2.5.2 (2012年7月現在) Python 2.6.6, 2.7.1 on Mac OSX and Ubuntu Maverick で動作確認している。 Python 2.5での互換性もなるべく考慮しているが保証外 Python 3.xユーザーはneoブランチをお試しください© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 34. Botoのゴール Amazon Web Serviceを幅、深さの面で全てをサポート する。 他のパブリックサービスもサポート • Google Storage プライベートクラウドもサポート • Eucalyptus • OpenStack • Open Nebula© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 35. 対応サービス 23個 (2012年7月現在)• Simple Storage Service (S3) • Simple Notification Server (SNS)• SimpleQueue Service (SQS) • Google Storage • Identity and Access Management• Elastic Compute Cloud (EC2) (IAM)• Mechanical Turk • Route53 DNS Service (route53)• SimpleDB • Simple Email Service (SES)• CloudFront • Flexible Payment Service (FPS)• CloudWatch • CloudFormation • Amazon DynamoDB• AutoScale • Amazon SimpleWorkflow• Elastic Load Balancer (ELB) • CloudSearch• Virtual Private Cloud (VPC) • Marketplace Web Services• Elastic Map Reduce (EMR)• Relational Data Service (RDS) © 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 36. botoに関するリソース ソースコードリポジトリ • https://github.com/boto/boto PyPI • http://pypi.python.org/pypi/boto オンラインドキュメント • http://docs.pythonboto.org/ IRC • http://webchat.freenode.net/?channels=boto© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 37. インストール $pip install boto© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 38. 初期設定 設定ファイルの作成 /etc/boto.cfg - グローバルな設定を記述 ~/.boto ユーザー毎の設定を記述© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 39. 設定ファイルの例[Credentials]aws_access_key_id = AKIOISTHEMOSTSUBERUARCHITECTaws_secret_access_key = Question1sWhyAr5Th5yCall5dSliXXXX[Boto]debug = 0num_retries = 10[DynamoDB]region = ap-northeast-1© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 40. EC2の操作例>>> from boto import ec2>>> con = ec2.connect_to_region(ap-northeast-1)#全てのインスタンス取得>>> con.get_all_instances()#スナップショットの作成>>> con.create_snapshot(‘vol-xxxxxxxxx’)© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 41. DynamoDBの操作例# コネクション作成conn = boto.connect_dynamodb()# スキーマを定義 (ハッシュキー)table_schema = conn.create_schema( hash_key_name=frame_no, hash_key_proto_value=12345, )# テーブルの作成table = conn.create_table( name=Table Name, schema=table_schema, read_units=10, write_units=10, )© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 42. DynamoDBの操作例# テーブルにアイテムをインサートfor x in xrange(1000): item = table.new_item( hash_key=x, attrs={comment: xxxxx}) item.put()© 2012 Amazon.com, Inc. and its affiliates. All All rights reserved. May not be copied, modified distributed in whole or in part without thethe express consent Amazon.com, Inc. © 2012 Amazon.com, Inc. and its affiliates. rights reserved. May not be copied, modified or or distributed in whole or in part without express consent of of Amazon.com, Inc.
  • 43. まとめ© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 44. SDKを使うことで • 運用管理での煩雑な手間をコーディングで楽にすることが可能 • どのサービスでも同じような使い勝手で利用可能 • 実際のサービス開発のお供に • S3 • SNS/SQS/SWF • DynamoDB AWSはコーディングする方の力を最大限に引き出すイ ンフラ • HAPPY CODING!!© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 45. ご参加ありがとう ございました© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.