LoginSignup
0
0

More than 3 years have passed since last update.

TerraformでElasticCacheを立ち上げてEC2から疎通確認する

Posted at

運用しているプロダクトでキャッシュをしたい場面があり、ElasticCache(Redis)を導入したのでメモ的に残します。

前提

  • AWS CLI設定済み
  • Terraform設定済み

公式ドキュメント

TerraformでElasticCacheを立ち上げる

Elastic CacheのクラスタをTerraformで作ります。

elastic-cache.tf
provider "aws" {
  profile    = "default"
  region     = "ap-northeast-1"
}

resource "aws_elasticache_cluster" "redis-test" {
  cluster_id           = "your-redis-cache-name"
  engine               = "redis"
  node_type            = "cache.t2.micro"
  num_cache_nodes      = 1
  parameter_group_name = "default.redis5.0"
  engine_version       = "5.0.5"
  port                 = 6379
  security_group_ids   = ["your-sg-name"]
}

各種名前は読み替えてください。セキュリティグループはElasticCacheを使用するリソースと同じセキュリティグループのIDを指定します。今回はEC2から使用するので、EC2が使用しているセキュリティグループのIDを指定します。

次はこのテンプレートを使ってリソースを作ります。

  • 作られるリソースを確認
$ terraform plan 
  • 実際にリソースを作成
$ terraform apply

コマンドを実行すると実際にリソースを作るかyes/noで聞かれるので、よければyesを入力します。数分するとリソースが作られます。

EC2からRedisに疎通確認

次にEC2にsshで接続して、Redisにアクセスしてみます。

セキュリティグループの設定

Redisアクセス用にポートを開けます。

Screen Shot 2019-11-26 at 21.10.22 copy.png

EC2にRedisCLIをインストール

  • Redis CLIをインストールするためにパッケージをダウンロードします。確認のプロンプトが表示されるので"y"を入力します。
$ sudo yum install gcc
  • redis-cli ユーティリティをダウンロードし、コンパイルします。
$ wget http://download.redis.io/redis-stable.tar.gz
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make

Redis CLIへログイン

  • 下記コマンドをElasticCacheのノードのエンドポイントに置き換えて実行します。
$ ~/redis-stable/src/redis-cli -c -h mycachecluster.eaogs8.0001.usw2.cache.amazonaws.com -p 6379
  • 正しく設定できていればRedis CLIに入れるはずです。
redis mycachecluster.eaogs8.0001.usw2.cache.amazonaws.com 6379>
0
0
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
0
0