Amazon ElastiCache for Redisの認証トークンの設定・変更

2022-06-15

Amazon ElastiCache for RedisはAWSが提供するRedis互換の高性能なインメモリデータストアサービスです。
この記事では、Amazon ElastiCache for Redisの認証トークンの設定・変更方法を Terraform の設定を交えてご紹介します。

Terraform

Terraform にて Amazon ElastiCache for Redis を構築する例を交えて認証トークン方法について記載します。
以下は Terraform の例です。

resource "aws_elasticache_replication_group" "sample" {
  replication_group_id       = "${var.base_name}-sample"
  description                = "Redis cluster for ${var.base_name}-sample."
  automatic_failover_enabled = var.sample_elasticache_param["automatic_failover_enabled"]
  engine                     = "redis"
  engine_version             = var.sample_elasticache_param["engine_version"]
  multi_az_enabled           = var.sample_elasticache_param["multi_az_enabled"]
  node_type                  = var.sample_elasticache_param["node_type"]
  num_cache_clusters         = var.sample_elasticache_param["number_cache_clusters"]
  parameter_group_name       = aws_elasticache_parameter_group.sample.id
  subnet_group_name          = aws_elasticache_subnet_group.sample.name
  security_group_ids         = [aws_security_group.sample_elasticache.id]
  port                       = 6379
  maintenance_window         = var.sample_elasticache_param["maintenance_window"]
  snapshot_window            = var.sample_elasticache_param["snapshot_window"]
  snapshot_retention_limit   = 35
  final_snapshot_identifier  = "${var.base_name}-sample-final-snapshot"
  apply_immediately          = false
  auto_minor_version_upgrade = true
  at_rest_encryption_enabled = true
  transit_encryption_enabled = true
  kms_key_id                 = aws_kms_key.sample_elasticache.arn
  auth_token                 = var.sample_elasticache_param["auth_token"]
  lifecycle {
    ignore_changes = [
      number_cache_clusters,
      apply_immediately
    ]
  }
  log_delivery_configuration {
    destination      = aws_cloudwatch_log_group.sample_elasticache_slowlog.name
    destination_type = "cloudwatch-logs"
    log_format       = "json"
    log_type         = "slow-log"
  }

  tags = merge(tomap({ "Service" = "sample" }), tomap({ "Name" = "${var.base_name}-sample" }))
}

transit_encryption_enabled = true (転送中の暗号化を有効)にして auth_token に認証トークンを設定しておくのがポイントです。

認証トークンの変更・設定

認証トークンの変更は、ROTATE と SET の 2 つの戦略をサポートしています。
ROTATE 戦略では、以前のトークンを保持しながら、サーバーに別の AUTH トークンを追加します。
SET 戦略では、1 つの AUTH トークンのみをサポートするように、サーバーを更新します。

$ aws elasticache modify-replication-group \
  --replication-group-id replication-group-sample \
  --auth-token new-token \
  --auth-token-update-strategy ROTATE \
  --apply-immediately

上記の ROTATE 戦略コマンドで新規認証トークン new-token が追加されます。
認証トークンは最大2つまで保持されるため、この状態ではまだ以前の認証トークンで認証可能な状態です。
以下の SET 戦略コマンドで new-token でのみ認証可能な状態とします。

$ aws elasticache modify-replication-group \
  --replication-group-id replication-group-sample \
  --auth-token new-token \
  --auth-token-update-strategy SET \
  --apply-immediately

なお原因は不明ですが、筆者が Terraform にて Amazon ElastiCache for Redis を構築した段階では認証トークンによる認証が必須ではなく Redis に接続・操作可能でした。
本節でご紹介した認証トークンの変更・設定を実施したところ、 Redis への接続・操作にトークンによる認証が必須となりました。

おすすめ書籍

おすすめ記事