https://awsbloglink.wordpress.com
概要
AWSが提供しているツール「AWS SDK for Ruby」を使用して、DynamoDBにテーブルを作成する。パブリッククラウド「Google Cloud Platform」のGCEをクライアントとしてAWS SDK for Rubyを導入した。
AWS SDK for Ruby 導入環境
OS => CentOS7
CPU => 2コア
メモリ => 7.5GB
HDD => 10GB
AWS SDK for Ruby バージョン => V2
手順
[1] OSにログイン
# sudo su -
※GCEのためログインアカウントでログイン後に、rootにスイッチする。
※GCEのためログインアカウントでログイン後に、rootにスイッチする。
[2] パッケージアップデート
# yum -y update
# yum -y install rubygems
[4] AWS SDK for Ruby インストール
# gem install aws-sdk-core
[5] バージョン確認
# ruby -r aws-sdk-core -e 'puts Aws::VERSION'
2.3.4
※結果は導入した時期により異なる。バージョンが表示されればOK。
[6] IAM認証ファイル作成
# mkdir .aws
aws_access_key_id=アクセスキー
aws_secret_access_key=シークレットアクセスキー
※「アクセスキー」および「シークレットアクセスキー」には、IAMで作成したユーザの情報を設定すること。
# vi .aws/credentials
=================================================
[default]aws_access_key_id=アクセスキー
aws_secret_access_key=シークレットアクセスキー
=================================================
[7] DynamoDBを更新するRubyソースを記述
# vi dynamodb_sample.rb
=================================================
=================================================
#! /usr/bin/ruby
require "set"
require "bigdecimal"
require "aws-sdk-core"
Aws.config[:credentials] = Aws::SharedCredentials.new
Aws.config[:region] = "ap-northeast-1"
dynamodb = Aws::DynamoDB::Client.new
begin
dynamodb.describe_table(:table_name => "sample")
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
dynamodb.create_table(
:table_name => "sample",
:attribute_definitions => [
{
:attribute_name => :Id,
:attribute_type => :N
}
],
:key_schema => [
{
:attribute_name => :Id,
:key_type => :HASH
}
],
:provisioned_throughput => {
:read_capacity_units => 1,
:write_capacity_units => 1,
}
)
# wait for table to be created
puts "waiting for table to be created..."
dynamodb.wait_until(:table_exists, table_name: "sample")
puts "table created!"
end
=================================================
require "set"
require "bigdecimal"
require "aws-sdk-core"
Aws.config[:credentials] = Aws::SharedCredentials.new
Aws.config[:region] = "ap-northeast-1"
dynamodb = Aws::DynamoDB::Client.new
begin
dynamodb.describe_table(:table_name => "sample")
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
dynamodb.create_table(
:table_name => "sample",
:attribute_definitions => [
{
:attribute_name => :Id,
:attribute_type => :N
}
],
:key_schema => [
{
:attribute_name => :Id,
:key_type => :HASH
}
],
:provisioned_throughput => {
:read_capacity_units => 1,
:write_capacity_units => 1,
}
)
# wait for table to be created
puts "waiting for table to be created..."
dynamodb.wait_until(:table_exists, table_name: "sample")
puts "table created!"
end
=================================================
[8] テーブル作成確認(sampleテーブルが作成されていれば成功)
0 件のコメント:
コメントを投稿