|
1 | | -resource "random_password" "redis_auth_token" { |
2 | | - length = 32 |
3 | | - special = true |
4 | | - override_special = "!&#$^<>-" |
5 | | -} |
6 | | - |
7 | | -resource "aws_secretsmanager_secret" "redis_auth_token" { |
8 | | - name = "imms/redis/auth-token" |
9 | | - description = "Auth token for the immunisation Redis cache" |
10 | | -} |
11 | | - |
12 | | -resource "aws_secretsmanager_secret_version" "redis_auth_token" { |
13 | | - secret_id = aws_secretsmanager_secret.redis_auth_token.id |
14 | | - secret_string = random_password.redis_auth_token.result |
| 1 | +# Subnet Group for Redis |
| 2 | +resource "aws_elasticache_subnet_group" "redis_subnet_group" { |
| 3 | + name = "immunisation-redis-subnet-group" |
| 4 | + subnet_ids = values(aws_subnet.private)[*].id |
15 | 5 | } |
16 | 6 |
|
17 | | -resource "aws_elasticache_replication_group" "redis_cluster" { |
18 | | - replication_group_id = "immunisation-redis-cluster" |
19 | | - description = "Redis cache for immunisation configuration data" |
| 7 | +resource "aws_elasticache_cluster" "redis_cluster" { |
| 8 | + cluster_id = "immunisation-redis-cluster" |
20 | 9 | engine = "redis" |
21 | 10 | engine_version = "7.0" |
22 | 11 | node_type = "cache.t2.micro" |
23 | | - num_cache_clusters = 1 |
| 12 | + num_cache_nodes = 1 |
24 | 13 | parameter_group_name = "default.redis7" |
25 | 14 | port = 6379 |
26 | 15 | security_group_ids = [aws_security_group.lambda_redis_sg.id] |
27 | 16 | subnet_group_name = aws_elasticache_subnet_group.redis_subnet_group.name |
28 | | - |
29 | | - at_rest_encryption_enabled = true |
30 | | - transit_encryption_enabled = true |
31 | | - auth_token = random_password.redis_auth_token.result |
32 | | - auth_token_update_strategy = "SET" |
33 | 17 | } |
34 | 18 |
|
35 | | -# Subnet Group for Redis |
36 | | -resource "aws_elasticache_subnet_group" "redis_subnet_group" { |
37 | | - name = "immunisation-redis-subnet-group" |
38 | | - subnet_ids = values(aws_subnet.private)[*].id |
| 19 | +# CloudFormation dynamic references keep the generated auth token out of Terraform state. |
| 20 | +resource "aws_cloudformation_stack" "redis_replication_group" { |
| 21 | + name = "immunisation-redis-replication-group" |
| 22 | + |
| 23 | + template_body = jsonencode({ |
| 24 | + AWSTemplateFormatVersion = "2010-09-09" |
| 25 | + Description = "Redis replication group with Secrets Manager generated auth token" |
| 26 | + Resources = { |
| 27 | + RedisAuthToken = { |
| 28 | + Type = "AWS::SecretsManager::Secret" |
| 29 | + Properties = { |
| 30 | + Name = "imms/redis/auth-token" |
| 31 | + Description = "Auth token for the immunisation Redis cache" |
| 32 | + GenerateSecretString = { |
| 33 | + ExcludePunctuation = true |
| 34 | + PasswordLength = 32 |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + RedisReplicationGroup = { |
| 39 | + Type = "AWS::ElastiCache::ReplicationGroup" |
| 40 | + DependsOn = "RedisAuthToken" |
| 41 | + Properties = { |
| 42 | + ReplicationGroupId = "immunisation-redis-replication-group" |
| 43 | + ReplicationGroupDescription = "Redis cache for immunisation configuration data" |
| 44 | + Engine = "redis" |
| 45 | + EngineVersion = "7.0" |
| 46 | + CacheNodeType = "cache.t2.micro" |
| 47 | + NumCacheClusters = 1 |
| 48 | + CacheParameterGroupName = "default.redis7" |
| 49 | + Port = 6379 |
| 50 | + SecurityGroupIds = [aws_security_group.lambda_redis_sg.id] |
| 51 | + CacheSubnetGroupName = aws_elasticache_subnet_group.redis_subnet_group.name |
| 52 | + AtRestEncryptionEnabled = true |
| 53 | + TransitEncryptionEnabled = true |
| 54 | + AuthToken = "{{resolve:secretsmanager:imms/redis/auth-token:SecretString}}" |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + }) |
39 | 59 | } |
0 commit comments