@@ -24,7 +24,6 @@ This gem also brings in the following AWS gems:
24
24
* ` aws-sdk-sqs `
25
25
* ` aws-sdk-sns `
26
26
* ` aws-record `
27
- * ` aws-sessionstore-dynamodb `
28
27
29
28
You will have to ensure that you provide credentials for the SDK to use. See the
30
29
latest [ AWS SDK for Ruby Docs] ( https://docs.aws.amazon.com/sdk-for-ruby/v3/api/index.html#Configuration )
@@ -72,7 +71,35 @@ configuration: they will be loaded automatically.
72
71
73
72
You can configure session storage in Rails to use DynamoDB instead of cookies,
74
73
allowing access to sessions from other applications and devices. You will need
75
- to have an existing Amazon DynamoDB session table to use this feature.
74
+ to have or create an existing Amazon DynamoDB session table to use this feature.
75
+
76
+ To enable this feature, add the following to your Gemfile :
77
+
78
+ ` ` ` ruby
79
+ gem 'aws-sessionstore-dynamodb', '~> 2'
80
+ ` ` `
81
+
82
+ For more information about this feature and configuration options, see the
83
+ [API reference](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/)
84
+ and the
85
+ [GitHub repository](https://github.com/aws/aws-sessionstore-dynamodb-ruby).
86
+
87
+ # ## Configuration generator
88
+
89
+ You can generate a configuration file for the session store using the following
90
+ command (--environment=<environment> is optional) :
91
+
92
+ ` ` ` bash
93
+ rails generate dynamo_db:session_store_config --environment=<Rails.env>
94
+ ` ` `
95
+
96
+ The session store configuration generator command will generate a YAML file
97
+ ` config/dynamo_db_session_store.yml` with default options. If provided an
98
+ environment, the file will be named
99
+ ` config/dynamo_db_session_store/<Rails.env>.yml` , which takes precedence over
100
+ the default file.
101
+
102
+ # ## ActiveRecord Migration generator
76
103
77
104
You can generate a migration file for the session table using the following
78
105
command (<MigrationName> is optional) :
@@ -81,29 +108,55 @@ command (<MigrationName> is optional):
81
108
rails generate dynamo_db:session_store_migration <MigrationName>
82
109
` ` `
83
110
84
- The session store migration generator command will generate two files : a
85
- migration file, `db/migration/#{VERSION}_#{MIGRATION_NAME}.rb`, and a
86
- configuration YAML file, `config/dynamo_db_session_store.yml`.
111
+ The session store migration generator command will generate a
112
+ migration file `db/migration/#{VERSION}_#{MIGRATION_NAME}.rb`.
87
113
88
114
The migration file will create and delete a table with default options. These
89
- options can be changed prior to running the migration and are documented in the
90
- [Table](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Table.html) class.
115
+ options can be changed prior to running the migration either in code by editing
116
+ the migration file or in the config YAML file. These options are documented in
117
+ the
118
+ [Table](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Table.html)
119
+ class.
91
120
92
121
To create the table, run migrations as normal with :
93
122
94
123
` ` ` bash
95
124
rails db:migrate
96
125
` ` `
97
126
98
- Next, configure the Rails session store to be `:dynamodb_store` by editing
99
- `config/initializers/session_store.rb` to contain the following :
127
+ To delete the table and rollback, run the following command :
128
+
129
+ ` ` ` bash
130
+ rails db:migrate:down VERSION=<VERSION>
131
+ ` ` `
132
+
133
+ # ## Migration Rake tasks
134
+
135
+ If you are not using ActiveRecord, you can manage the table using the provided
136
+ Rake tasks :
137
+
138
+ ` ` ` bash
139
+ rake dynamo_db:session_store:create_table
140
+ rake dynamo_db:session_store:delete_table
141
+ ` ` `
142
+
143
+ The rake tasks will create and delete a table with default options. These
144
+ options can be changed prior to running the task in the config YAML file. These
145
+ options are documented in the
146
+ [Table](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Table.html)
147
+ class.
148
+
149
+ # ## Usage
150
+
151
+ To use the session store, add or edit your
152
+ `config/initializers/session_store.rb` file :
100
153
101
154
` ` ` ruby
102
- # config/initializers/session_store.rb
103
- Rails.application.config.session_store :dynamodb_store, key: '_your_app_session'
155
+ options = { table_name: '_your_app_session' } # overrides from YAML or ENV
156
+ Rails.application.config.session_store :dynamo_db_store, **options
104
157
` ` `
105
158
106
- You can now start your Rails application with session support.
159
+ You can now start your Rails application with DynamoDB session support.
107
160
108
161
# ## Configuration
109
162
@@ -113,38 +166,41 @@ initializer like so:
113
166
114
167
` ` ` ruby
115
168
# config/initializers/session_store.rb
116
- Rails.application.config.session_store :dynamodb_store ,
117
- key : '_your_app_session ',
118
- table_name : 'foo ',
169
+ Rails.application.config.session_store :dynamo_db_store ,
170
+ table_name : 'your-table-name ',
171
+ table_key : 'your-session-key ',
119
172
dynamo_db_client: my_ddb_client
120
173
` ` `
121
174
122
- Alternatively, you can use the generated YAML configuration file
123
- ` config/dynamo_db_session_store.yml` . YAML configuration may also be specified
124
- per environment, with environment configuration having precedence. To do this,
125
- create `config/dynamo_db_session_store/#{Rails.env}.yml` files as needed.
126
-
127
- For configuration options, see the [Configuration](https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Configuration.html) class.
175
+ Alternatively, you can use the generated YAML configuration files
176
+ ` config/dynamo_db_session_store.yml` or
177
+ ` config/dynamo_db_session_store/<Rails.env>.yml` .
128
178
129
- # ### Rack Configuration
179
+ For configuration options, see the [Configuration]
180
+ (https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Configuration.html)
181
+ class.
130
182
131
- DynamoDB session storage is implemented in the [\`aws-sessionstore-dynamodb\`](https://github.com/aws/aws-sessionstore-dynamodb-ruby)
132
- gem. The Rack middleware inherits from the [\ `Rack::Session::Abstract::Persisted\ `](https://www. rubydoc.info/github/rack/rack/Rack/Session/Abstract/Persisted)
183
+ The session store inherits from the
184
+ [ `Rack::Session::Abstract::Persisted`](https://rubydoc.info/github/rack/rack-session/main /Rack/Session/Abstract/Persisted)
133
185
class, which also includes additional options (such as `:key`) that can be
134
186
passed into the Rails initializer.
135
187
136
188
# ## Cleaning old sessions
137
189
138
- By default sessions do not expire. See `config/dynamo_db_session_store.yml ` to
190
+ By default sessions do not expire. You can use `:max_age` and `:max_stale ` to
139
191
configure the max age or stale period of a session.
140
192
141
- You can use the DynamoDB [Time to Live (TTL) feature](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html)
142
- on the `expire_at` attribute to automatically delete expired items.
193
+ You can use the DynamoDB
194
+ [Time to Live (TTL) feature](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html)
195
+ on the `expire_at` attribute to automatically delete expired items, saving you
196
+ the trouble of manually deleting them and reducing costs.
143
197
144
- Alternatively, a Rake task for garbage collection is provided :
198
+ If you wish to delete old sessions based on creation age (invalidating valid
199
+ sessions) or if you want control over the garbage collection process, you can
200
+ use the provided Rake task :
145
201
146
202
` ` ` bash
147
- rake dynamo_db:collect_garbage
203
+ rake dynamo_db:session_store:clean
148
204
` ` `
149
205
150
206
# # Amazon Simple Email Service (SES) as an ActionMailer Delivery Method
0 commit comments