0
0

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 1 year has passed since last update.

ElastiCacheの定期再起動

Posted at

■概要

ElastiCacheの定期再起動について、EventBridge Schedulerで行ったのでメモです。
lambda使用を想定したコードも記載します。

■手順について

EventBridge Schedulerについては以前Auroraの定期再起動で使ったのですが、
今回はアクション内容の設定部分のみが以前と異なる部分になります。

スケジュール設定や、IAMロールの準備は以下に記載しています。

EventBridge SchedulerでAuroraの定期的な再起動
https://qiita.com/zenden/items/8e014a69bc02b804f21f

ではアクション内容についてですが、今回もターゲット指定するだけなのでとても簡単です。
まず「すべての API」を選択し、「ElastiCache」で検索をかけます。

EC1_20231003111549.png

「ElastiCache」を選択し、次は「reboot」で検索をかけます。
「RebootCacheCluster」を選択しましょう。

EC3_20231003115603.png

後は入力部分に再起動したいクラスターIDとノードIDを記載するのみですが、入力のデフォルトは以下のようになっています。

{
  "CacheClusterId": "MyData",
  "CacheNodeIdsToReboot": [
    "MyData"
  ]
}

コンソールからだとクラスターごと再起動が可能ですが、EventBridgeではノードを指定する必要があります。
逆に言うと、ノード単位で再起動をかけたい場合はよいかも…?

例えば、クラスター「test-memcached」に「0001、0002、0003、0004」という4つのノードがある場合、以下の入力でクラスター内全ての再起動ができます。

{
  "CacheClusterId": "test-memcached",
  "CacheNodeIdsToReboot": [
    "0001",
    "0002",
    "0003",
    "0004"
  ]
}

■lambdaで定期再起動する際は

以下は今回検証してないですが、AWS SDK for Python(Boto3)を使用してElastiCacheの再起動する方法です。

・ElastiCacheのクラスターを指定して再起動するためのPythonコード(CacheClusterIdパラメータに再起動するクラスターのIDを指定する)

import boto3

def restart_elasticache_cluster(cluster_id):
    client = boto3.client('elasticache')
    response = client.reboot_cache_cluster(CacheClusterId=cluster_id)
    return response

# クラスターIDを指定してElastiCacheクラスターを再起動する例
response = restart_elasticache_cluster('your-cluster-id')
print(response)

・ElastiCacheのノードを指定して再起動するためのPythonコード(your-cache-cluster-idとyour-node-idを実際の値に置き換える)

import boto3

def restart_elasticache_node(node_id):
    client = boto3.client('elasticache')
    response = client.reboot_cache_cluster(CacheClusterId='your-cache-cluster-id', CacheNodeIdsToReboot=[node_id])
    return response

# ノード名を指定して再起動
restart_elasticache_node('your-node-id')

以上です!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?