30
loading...
This website collects cookies to deliver better user experience
Encryption at rest: DynamoDB secures all of your data in an encrypted table, including the primary key. Users can choose three options: an AWS-owned customer master key (CMK), an AWS-managed CMK (i.e., the key is stored in your account and managed by the AWS Key Management Service), or a customer-managed CMK (i.e., the key is stored in your account and created/owned/managed by you.) The cool thing is that each table can utilize a different option. To read more about encryption at rest, here are the official AWS docs.
On-demand backups/point-in-time recovery: DynamoDB allows you to restore a table to any point in time during the last ~30 days. So long are the days of worrying about accidental writes or deletes!
{
"HouseID": 1,
"AddressLine1": "123 Main St",
"City": "Atlanta",
"State": "GA",
"Zip": 30322,
"Rented": true,
"Tenant": "John Smith"
}
{
"HouseID": 2,
"AddressLine1": "456 Square St",
"City": "Nashville",
"State": "TN",
"Zip": 37211,
"Rented": true,
"Tenant": "Mary Jane"
}
row
and an attribute to a column
in a traditional SQL database. However, I suggest trying to avoid comparing a NoSQL database, such as DynamoDB, with a traditional SQL database, as they are very different beasts.item
has a unique primary key (DynamoDB supports both single partition keys and composite primary keys). However, other than HouseID
, the Houses
table is schemaless. Thus, the attributes and data types need not be defined ahead of time, unlike a traditional SQL database, which would require Zip
to be defined in schema with the integer
type specified.rented
and zip
attributes of my Houses
table (i.e., given the zip code, get all rented properties in that zip code). If I were using a SQL database, I might create a migration that looks like this:CREATE INDEX RentedAndZipIndex
ON Houses (rented, zip);
Gemfile
, add the following gems and run bundle install
:gem 'dynamoid'
gem 'aws-sdk'
aws.rb
in the config/initializers
directory and add the following:house.rb
file in the app/models
directory that looks like this:include Dynamoid::Document
when defining your tables.read_capacity
and write_capacity
. However, since I have defined capacity_mode
as on_demand
, these options would be ignored since on_demand
capacity mode is telling AWS to automatically scale up or down.string
if omitted.)dynamoid
gem comes with a nifty rake task to create your table.rake dynamoid:create_tables
dynamoid
gem is that a lot of the other features you're used to with Active Record work very much the same way.has_many, has_one, has_and_belongs_to_many, and belongs_to
can all be defined just as you would normally with ActiveRecord. You could imagine my rental properties app having another document table called Lease
, so on the House
table, I might define something like this:has_one :lease
Lease
table:belongs_to :house
before_
or after_
callbacks are available for save, update, destroy
actions, so I could define something like this in my lease.rb
:before_save :validate_signature
h = House.new(address_line_1: "123 Main St", city: "Cool City", state: "Iowa", zip: 52302)
h.rented = true
h.save
.create
method:House.create(address_line_1: "123 Main St", city: "Cool City", state: "Iowa", zip: 52302)
.find
will take an ID.where
will take any number of matching criteria.find_by_x
where x
is an attribute.dynamoid
gem with your Rails project.