3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ElastiCacheのredisをEC2で使う

Posted at

EC2二台が使用するキャッシュサーバーとしてElastiCacheを導入しました。
今まではセッションをsessionsテーブルに持っていたのをredisに移行するすることが主な目的だったのですが、将来的によく利用するデータはredisに持たせたりしたいなという目論みもありました。

redisのインスタンス作成

ElastiCacheの左側のメニューでredisを選択して「作成」からredisのインスタンスを作成します。

https://gyazo.com/13a7c619258295da8987420a5b5fa9bf

Amazon ElastiCache クラスターの作成 画面が表示されます。
入力すべきは以下の項目です。

  • 名前
  • ノードタイプ

パラメータグループもチューニングして設定すべきできすが、今のところデフォルトで困っていないので後回しにしています。

ElastiCacheの料金表は以下のリンクから確認してください。
https://aws.amazon.com/jp/elasticache/pricing/

https://gyazo.com/ccabd8b9a00296012311929f5ba806a8

詳細設定に進みます。

https://gyazo.com/1e6addedd11d2c1266685e2997b8dab3

入力すべきは以下の項目です。

  • サブネットグループ
  • セキュリティグループ

セキュリティグループはインバウンドでredisのポート(6379)を開けてあげたものを設定します。

EC2からElastiCacheのredisへの疎通確認

$ redis-cli -h redis-name.xxx.xx.9999.xxxx.cache.amazonaws.com
$ ping
PONG

redis-cliが入っていない場合はインストールする必要があります。
(ubuntuの場合は $ sudo apt-get install redis-tools -y

Railsの設定変更

config/initializers/session_store.rb

Rails.application.config.session_store :redis_store, servers: ENV['REDIS_URL'], expire_after: 3.months

redisのURLは環境変数で設定しているので、各サーバーの環境変更(REDIS_URL)にredisのエンドポイント(portは不要)を入れてあげればOKです。

エンドポイントはダッシュボードのプライマリエンドポイントを見てください。

https://gyazo.com/cc04366daa59f9c63ead12d31db1bfb5


_この記事は[ElastiCacheのredisをEC2で使う](https://tackeyy.com/blog/posts/use-elasti-cache-with-ec2)をQiita用に加筆修正したものです_
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?