Skip to content

Instantly share code, notes, and snippets.

@inokappa
Last active December 18, 2016 09:46
Show Gist options
  • Save inokappa/55f494b61bd56cb2f348fb6d95d62591 to your computer and use it in GitHub Desktop.
Save inokappa/55f494b61bd56cb2f348fb6d95d62591 to your computer and use it in GitHub Desktop.
シンプルなテーブルを作ったり、削除したり、キャパシティを変更するタスク
require "capistrano_colors"
require "aws-sdk"
set :aws_region, "ap-northeast-1"
set :aws_profile, "oreno-profile"
#####################################################
def dynamo
Aws::DynamoDB::Client.new(region: aws_region, profile: aws_profile)
end
def self.dynamo_info(table_name)
begin
res = dynamo.describe_table({
table_name: table_name
})
puts "------------------------------------------------------"
puts "table name: " + res.table.table_name
puts "table status: " + res.table.table_status
puts "table read capacity: " + res.table.provisioned_throughput.read_capacity_units.to_s
puts "table write capacity: " + res.table.provisioned_throughput.write_capacity_units.to_s
puts "------------------------------------------------------"
rescue
puts "Error."
end
end
namespace :dynamodb do
namespace :table do
desc "テーブルを作成する(-S table_name=${テーブル名を指定} -S key_name=${キー名を指定})"
task :create do
begin
res = dynamo.create_table({
table_name: table_name,
key_schema:[
{
attribute_name: key_name,
key_type: "HASH"
}
],
attribute_definitions: [
{
attribute_name: key_name,
attribute_type: "S",
}
],
provisioned_throughput: {
read_capacity_units: 1,
write_capacity_units: 1,
},
})
rescue => e
puts e
end
dynamo_info(table_name)
end
desc "テーブルを削除する(-S table_name=${テーブル名を指定})"
task :delete do
begin
res = dynamo.delete_table({
table_name: table_name
})
rescue => e
puts e
end
dynamo_info(table_name)
end
desc "テーブルのキャパシティを変更する(-S table_name=${テーブル名を指定} -S read=${value} -S write=${value})"
task :update_capacity do
begin
resp = dynamo.update_table({
table_name: table_name,
provisioned_throughput: {
read_capacity_units: read,
write_capacity_units: write
}
})
rescue => e
puts e
end
dynamo_info(table_name)
end
desc "テーブルの情報を取得する(-S table_name=${環境名を指定})"
task :info do
dynamo_info(table_name)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment