© 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.
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.
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.
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.
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.
これらの裏側では・・・
各サービスの各操作に対して、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.
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.
動作イメージ
起動、停止
Put,Get
DB起動
バックアップ
情報取得
REST
SOAP 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.
認証情報
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.
操作の種類
例:EC2 • インスタンス起動・・・RunInstances
• リブート・・・ RebootInstances
• IPアドレス付与・・AllocateAddress
• などさまざまな操作が、プログラムから実行可能
• http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/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.
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.
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.
操作可能サービス
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.
利用方法
ダウンロードから
• 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.
初期設定
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.
config.inc.php
CFCredentials::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.
サンプル: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.
サンプル:DynamoDBへPUT
<?php require_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.
活用法
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.
PHP版SDKの便利なクラス
DynamoDBSessionHandler DynamoDBを使った、HTTPのセッション共有が可能
EC2 DynamoDB
EC2
EC2
ELB 速い
消えない
管理不要
© 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.
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.
その他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.
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.
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.
操作可能サービス
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.
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.
呼び出し例
require 'rubygems' require 'yaml' require '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.
サンプル: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.
サンプル:EC2(起動とssh接続)
# launch the instance instance = image.run_instance(:key_pair => key_pair, :security_groups => group) sleep 1 until instance.status != :pending puts "Launched instance #{instance.id}, status: #{instance.status}" exit 1 unless instance.status == :running Net::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.
サンプル:S3
# get an instance of the S3 interface using the default configuration s3 = AWS::S3.new # create a bucket b = s3.buckets.create(bucket_name) # upload a file basename = 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.
Railsでの利用(1)
SimpleDBの使用
gem “aws-sdk”
bundle install
config/aws.yml
config/initializers/aws.rb
access_key_id: REPLACE_WITH_ACCESS_KEY_ID
secret_access_key: REPLACE_WITH_SECRET_ACCESS_KEY
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.
Railsでの利用(2)
app/models/my_record.rb
SimpleDBドメインの作成
class MyRecord < AWS::Record::Base
string_attr :name
end
$ 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.
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. © 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.
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 rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc. © 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.
Botoのゴール
Amazon Web Serviceを幅、深さの面で全てをサポートする。
他のパブリックサービスもサポート
• Google Storage
プライベートクラウドもサポート
• Eucalyptus
• OpenStack
• Open Nebula
© 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. © 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個 (2012年7月現在)
• Simple Storage Service (S3)
• SimpleQueue Service (SQS)
• Elastic Compute Cloud (EC2)
• Mechanical Turk
• SimpleDB
• CloudFront
• CloudWatch
• AutoScale
• Elastic Load Balancer (ELB)
• Virtual Private Cloud (VPC)
• Elastic Map Reduce (EMR)
• Relational Data Service (RDS)
• Simple Notification Server (SNS)
• Google Storage
• Identity and Access Management
(IAM)
• Route53 DNS Service (route53)
• Simple Email Service (SES)
• Flexible Payment Service (FPS)
• CloudFormation
• Amazon DynamoDB
• Amazon SimpleWorkflow
• CloudSearch
• Marketplace Web Services
© 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. © 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.
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 rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc. © 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.
インストール
$pip install boto
© 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. © 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.
初期設定
設定ファイルの作成
/etc/boto.cfg - グローバルな設定を記述
~/.boto ユーザー毎の設定を記述
© 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. © 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.
設定ファイルの例
[Credentials] aws_access_key_id = AKIOISTHEMOSTSUBERUARCHITECT aws_secret_access_key = Question1sWhyAr5Th5yCall5dSliXXXX [Boto] debug = 0 num_retries = 10 [DynamoDB] region = ap-northeast-1
© 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. © 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.
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 rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc. © 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.
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 rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc. © 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.
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 rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
まとめ
© 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.
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.
ご参加ありがとう ございました