これはなに

ElastiCache redisの理解を深めるために検証してみる
ElastiCache redisとEC2を接続させるまで試してみる

前提

同じVPC内に作成
EC2 x 1
redis x 1

手順

SG作成

EC2のSG:redis用にアウトバウンドを追加しておく

redis用のSG:作成段階で変更できなかったので作成後に関連付けられた「default」のSGのインバウンドを追加する

subnet,subnetgroup作成

redis用に作成

EC2作成

EC2名:nagano-test-redis
SG:nagano-test-sg-redis-ec2(先ほど作成したSG)
subnet:public-subnet-a
role:ssmを使用するために付与
EIP:付与

redis作成

subnet:private-subnet-a,private-subnet-c
上記以外は以下パラメータで作成

EC2へ接続

ssmで接続
パッケージインストール

 [ssm-user@ip-10-0-xx-xx ~]$ sudo yum -y install openssl-devel gcc
Amazon Linux 2023 repository                                                                                                                                            57 MB/s |  33 MB     00:00
Amazon Linux 2023 Kernel Livepatch repository                                                                                                                          102 kB/s |  14 kB     00:00
Dependencies resolved.
=======================================================================================================================================================================================================
 Package                                            Architecture                           Version                                                   Repository                                   Size
=======================================================================================================================================================================================================
Installing:
 gcc                                                x86_64                                 11.4.1-2.amzn2023.0.2                                     amazonlinux                                  32 M
 openssl-devel                                      x86_64                                 1:3.0.8-1.amzn2023.0.19                                   amazonlinux                                 3.0 M
Installing dependencies:
 annobin-docs                                       noarch                                 10.93-1.amzn2023.0.1                                      amazonlinux                                  92 k
 annobin-plugin-gcc                                 x86_64                                 10.93-1.amzn2023.0.1                                      amazonlinux                                 887 k
 cpp                                                x86_64                                 11.4.1-2.amzn2023.0.2                                     amazonlinux                                  10 M
 gc                                                 x86_64                                 8.0.4-5.amzn2023.0.2                                      amazonlinux                                 105 k
 glibc-devel                                        x86_64                                 2.34-117.amzn2023.0.1                                     amazonlinux                                  33 k
 glibc-headers-x86                                  noarch                                 2.34-117.amzn2023.0.1                                     amazonlinux                                 434 k
 guile22                                            x86_64                                 2.2.7-2.amzn2023.0.3                                      amazonlinux                                 6.4 M
 kernel-headers                                     x86_64                                 6.1.129-138.220.amzn2023                                  amazonlinux                                 1.4 M
 libmpc                                             x86_64                                 1.2.1-2.amzn2023.0.2                                      amazonlinux                                  62 k
 libtool-ltdl                                       x86_64                                 2.4.7-1.amzn2023.0.3                                      amazonlinux                                  38 k
 libxcrypt-devel                                    x86_64                                 4.4.33-7.amzn2023                                         amazonlinux                                  32 k
 make                                               x86_64                                 1:4.3-5.amzn2023.0.2                                      amazonlinux                                 534 k

Transaction Summary
=======================================================================================================================================================================================================
Install  14 Packages

Total download size: 55 M
Installed size: 172 M
Downloading Packages:
(1/14): annobin-docs-10.93-1.amzn2023.0.1.noarch.rpm                                                                                                                   1.4 MB/s |  92 kB     00:00
(2/14): annobin-plugin-gcc-10.93-1.amzn2023.0.1.x86_64.rpm                                                                                                              10 MB/s | 887 kB     00:00
(3/14): gc-8.0.4-5.amzn2023.0.2.x86_64.rpm                                                                                                                             4.4 MB/s | 105 kB     00:00
(4/14): glibc-devel-2.34-117.amzn2023.0.1.x86_64.rpm                                                                                                                   1.7 MB/s |  33 kB     00:00
(5/14): glibc-headers-x86-2.34-117.amzn2023.0.1.noarch.rpm                                                                                                             5.6 MB/s | 434 kB     00:00
(6/14): cpp-11.4.1-2.amzn2023.0.2.x86_64.rpm                                                                                                                            41 MB/s |  10 MB     00:00
(7/14): kernel-headers-6.1.129-138.220.amzn2023.x86_64.rpm                                                                                                              22 MB/s | 1.4 MB     00:00
(8/14): libmpc-1.2.1-2.amzn2023.0.2.x86_64.rpm                                                                                                                         3.3 MB/s |  62 kB     00:00
(9/14): libtool-ltdl-2.4.7-1.amzn2023.0.3.x86_64.rpm                                                                                                                   2.1 MB/s |  38 kB     00:00
(10/14): guile22-2.2.7-2.amzn2023.0.3.x86_64.rpm                                                                                                                        29 MB/s | 6.4 MB     00:00
(11/14): libxcrypt-devel-4.4.33-7.amzn2023.x86_64.rpm                                                                                                                  602 kB/s |  32 kB     00:00
(12/14): make-4.3-5.amzn2023.0.2.x86_64.rpm                                                                                                                            7.8 MB/s | 534 kB     00:00
(13/14): openssl-devel-3.0.8-1.amzn2023.0.19.x86_64.rpm                                                                                                                 25 MB/s | 3.0 MB     00:00
(14/14): gcc-11.4.1-2.amzn2023.0.2.x86_64.rpm                                                                                                                           43 MB/s |  32 MB     00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                   64 MB/s |  55 MB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                               1/1
  Installing       : libmpc-1.2.1-2.amzn2023.0.2.x86_64                                                                                                                                           1/14
  Installing       : cpp-11.4.1-2.amzn2023.0.2.x86_64                                                                                                                                             2/14
  Installing       : libtool-ltdl-2.4.7-1.amzn2023.0.3.x86_64                                                                                                                                     3/14
  Installing       : kernel-headers-6.1.129-138.220.amzn2023.x86_64                                                                                                                               4/14
  Installing       : glibc-headers-x86-2.34-117.amzn2023.0.1.noarch                                                                                                                               5/14
  Installing       : libxcrypt-devel-4.4.33-7.amzn2023.x86_64                                                                                                                                     6/14
  Installing       : glibc-devel-2.34-117.amzn2023.0.1.x86_64                                                                                                                                     7/14
  Installing       : gc-8.0.4-5.amzn2023.0.2.x86_64                                                                                                                                               8/14
  Installing       : guile22-2.2.7-2.amzn2023.0.3.x86_64                                                                                                                                          9/14
  Installing       : make-1:4.3-5.amzn2023.0.2.x86_64                                                                                                                                            10/14
  Installing       : gcc-11.4.1-2.amzn2023.0.2.x86_64                                                                                                                                            11/14
  Running scriptlet: gcc-11.4.1-2.amzn2023.0.2.x86_64                                                                                                                                            11/14
  Installing       : annobin-docs-10.93-1.amzn2023.0.1.noarch                                                                                                                                    12/14
  Installing       : annobin-plugin-gcc-10.93-1.amzn2023.0.1.x86_64                                                                                                                              13/14
  Running scriptlet: annobin-plugin-gcc-10.93-1.amzn2023.0.1.x86_64                                                                                                                              13/14
  Installing       : openssl-devel-1:3.0.8-1.amzn2023.0.19.x86_64                                                                                                                                14/14
  Running scriptlet: openssl-devel-1:3.0.8-1.amzn2023.0.19.x86_64                                                                                                                                14/14
  Verifying        : annobin-docs-10.93-1.amzn2023.0.1.noarch                                                                                                                                     1/14
  Verifying        : annobin-plugin-gcc-10.93-1.amzn2023.0.1.x86_64                                                                                                                               2/14
  Verifying        : cpp-11.4.1-2.amzn2023.0.2.x86_64                                                                                                                                             3/14
  Verifying        : gc-8.0.4-5.amzn2023.0.2.x86_64                                                                                                                                               4/14
  Verifying        : gcc-11.4.1-2.amzn2023.0.2.x86_64                                                                                                                                             5/14
  Verifying        : glibc-devel-2.34-117.amzn2023.0.1.x86_64                                                                                                                                     6/14
  Verifying        : glibc-headers-x86-2.34-117.amzn2023.0.1.noarch                                                                                                                               7/14
  Verifying        : guile22-2.2.7-2.amzn2023.0.3.x86_64                                                                                                                                          8/14
  Verifying        : kernel-headers-6.1.129-138.220.amzn2023.x86_64                                                                                                                               9/14
  Verifying        : libmpc-1.2.1-2.amzn2023.0.2.x86_64                                                                                                                                          10/14
  Verifying        : libtool-ltdl-2.4.7-1.amzn2023.0.3.x86_64                                                                                                                                    11/14
  Verifying        : libxcrypt-devel-4.4.33-7.amzn2023.x86_64                                                                                                                                    12/14
  Verifying        : make-1:4.3-5.amzn2023.0.2.x86_64                                                                                                                                            13/14
  Verifying        : openssl-devel-1:3.0.8-1.amzn2023.0.19.x86_64                                                                                                                                14/14

Installed:
  annobin-docs-10.93-1.amzn2023.0.1.noarch           annobin-plugin-gcc-10.93-1.amzn2023.0.1.x86_64     cpp-11.4.1-2.amzn2023.0.2.x86_64                   gc-8.0.4-5.amzn2023.0.2.x86_64
  gcc-11.4.1-2.amzn2023.0.2.x86_64                   glibc-devel-2.34-117.amzn2023.0.1.x86_64           glibc-headers-x86-2.34-117.amzn2023.0.1.noarch     guile22-2.2.7-2.amzn2023.0.3.x86_64
  kernel-headers-6.1.129-138.220.amzn2023.x86_64     libmpc-1.2.1-2.amzn2023.0.2.x86_64                 libtool-ltdl-2.4.7-1.amzn2023.0.3.x86_64           libxcrypt-devel-4.4.33-7.amzn2023.x86_64
  make-1:4.3-5.amzn2023.0.2.x86_64                   openssl-devel-1:3.0.8-1.amzn2023.0.19.x86_64

Complete!

redisのソースコード取得(安定版)

[ssm-user@ip-10-0-xx-xx ~]$ wget http://download.redis.io/redis-stable.tar.gz
--2025-03-10 08:52:53--  http://download.redis.io/redis-stable.tar.gz
Resolving download.redis.io (download.redis.io)... 104.18.27.34, 104.18.26.34, 2606:4700::6812:1b22, ...
Connecting to download.redis.io (download.redis.io)|104.18.27.34|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3630113 (3.5M) [application/octet-stream]
Saving to: ‘redis-stable.tar.gz’

redis-stable.tar.gz                               100%[============================================================================================================>]   3.46M  --.-KB/s    in 0.07s

2025-03-10 08:52:53 (51.2 MB/s) - ‘redis-stable.tar.gz’ saved [3630113/3630113]
[ssm-user@ip-10-0-xx-xx ~]$

解凍

[ssm-user@ip-10-0-xx-xx ~]$ tar xvzf redis-stable.tar.gz

階層移動

[ssm-user@ip-10-0-xx-xx ~]$ cd redis-stable

コンパイル

[ssm-user@ip-10-0-xx-xx redis-stable]$ make distclean
[ssm-user@ip-10-0-xx-xx redis-stable]$ make redis-cli BUILD_TLS=yes

パスを通す(コンパイルパッケージをインストール)

[ssm-user@ip-10-0-xx-xx redis-stable]$ sudo install -m 755 src/redis-cli /usr/local/bin/

動作確認

[ssm-user@ip-10-0-xx-xx redis-stable]$ redis-cli -h clustercfg.nagano-test-redis.xxxxx.apne1.cache.amazonaws.com -p 6379
clustercfg.nagano-test-redis.xxxxx.apne1.cache.amazonaws.com:6379>

最後に

無事接続を試すことができました。
引き続きElastiCacheをうまく活用する方法を検証していきたいです。