Created
May 18, 2017 19:27
-
-
Save indyarocks/5ff0b9400efa2b4a6cce92a487d303c7 to your computer and use it in GitHub Desktop.
Rake task to create a DynamoDB table in Rails application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/tasks/dynamodb_tables_v1/create_activity_table.rake | |
# Rake task to create activities table | |
namespace :dynamodb_tables_v1 do | |
desc "bundle exec rake dynamodb_tables_v1:create_activity_table RAILS_ENV=<ENV>" | |
task :create_activity_table => :environment do | |
puts "Creating activities table in #{Rails.env}\n" | |
create_activity_table | |
puts "Completed task\n" | |
end | |
def create_activity_table | |
params = { | |
table_name: 'activities', # required | |
key_schema: [ # required | |
{ | |
attribute_name: 'actor', # required User:1 | |
key_type: 'HASH', # required, accepts HASH, RANGE | |
}, | |
{ | |
attribute_name: 'created_at', # timestamp | |
key_type: 'RANGE' | |
} | |
], | |
attribute_definitions: [ # required | |
{ | |
attribute_name: 'actor', # required | |
attribute_type: 'S', # required, accepts S, N, B | |
}, | |
{ | |
attribute_name: 'created_at', # Timestamp | |
attribute_type: 'N', # required, accepts S, N, B | |
} | |
], | |
provisioned_throughput: { # required | |
read_capacity_units: 5, # required | |
write_capacity_units: 5, # required | |
} | |
} | |
begin | |
result = DynamodbClient.client.create_table(params) | |
puts 'Created table: activities\n Status: ' + result.table_description.table_status; | |
rescue Aws::DynamoDB::Errors::ServiceError => error | |
puts 'Unable to create table: activities\n' | |
puts "#{error.message}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment