|
| 1 | +provider "aws" { |
| 2 | + region = local.region |
| 3 | +} |
| 4 | + |
| 5 | +provider "aws" { |
| 6 | + region = "us-west-2" |
| 7 | + alias = "us-west-2" |
| 8 | +} |
| 9 | + |
| 10 | +locals { |
| 11 | + region = "us-east-1" |
| 12 | + name = "memorydb-ex-${replace(basename(path.cwd), "_", "-")}" |
| 13 | + |
| 14 | + tags = { |
| 15 | + Example = local.name |
| 16 | + Environment = "dev" |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +################################################################################ |
| 21 | +# MemoryDB Module |
| 22 | +################################################################################ |
| 23 | + |
| 24 | +module "memory_db_disabled" { |
| 25 | + source = "../.." |
| 26 | + |
| 27 | + name = "${local.name}-disabled" |
| 28 | + create = false |
| 29 | +} |
| 30 | + |
| 31 | +module "multi_region_cluster" { |
| 32 | + source = "../../modules/multi-region-cluster" |
| 33 | + |
| 34 | + create = true |
| 35 | + multi_region_cluster_name_suffix = local.name |
| 36 | + tls_enabled = true |
| 37 | + engine = "valkey" |
| 38 | + engine_version = "7.3" |
| 39 | + node_type = "db.r7g.xlarge" |
| 40 | + num_shards = 2 |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +module "memory_db" { |
| 45 | + source = "../.." |
| 46 | + |
| 47 | + # Cluster |
| 48 | + name = local.name |
| 49 | + description = "Example MemoryDB cluster" |
| 50 | + |
| 51 | + # This makes it part of the multi region cluster |
| 52 | + multi_region_cluster_name = module.multi_region_cluster.multi_region_cluster_name |
| 53 | + |
| 54 | + # engine = "valkey" |
| 55 | + # engine_version = "7.3" |
| 56 | + auto_minor_version_upgrade = true |
| 57 | + node_type = "db.r7g.xlarge" |
| 58 | + num_shards = 2 |
| 59 | + num_replicas_per_shard = 2 |
| 60 | + |
| 61 | + # tls_enabled = true |
| 62 | + security_group_ids = [module.security_group.security_group_id] |
| 63 | + maintenance_window = "sun:23:00-mon:01:30" |
| 64 | + snapshot_retention_limit = 7 |
| 65 | + snapshot_window = "05:00-09:00" |
| 66 | + |
| 67 | + # Users |
| 68 | + users = { |
| 69 | + admin = { |
| 70 | + user_name = "admin-user" |
| 71 | + access_string = "on ~* &* +@all" |
| 72 | + type = "iam" |
| 73 | + tags = { user = "admin" } |
| 74 | + } |
| 75 | + readonly = { |
| 76 | + user_name = "readonly-user" |
| 77 | + access_string = "on ~* &* -@all +@read" |
| 78 | + passwords = [random_password.password.result] |
| 79 | + tags = { user = "readonly" } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + # ACL |
| 84 | + create_acl = false |
| 85 | + acl_name = "${local.name}-acl" |
| 86 | + acl_tags = { acl = "custom" } |
| 87 | + |
| 88 | + # Parameter group |
| 89 | + create_parameter_group = false |
| 90 | + |
| 91 | + # Subnet group |
| 92 | + subnet_group_name = "${local.name}-subnet-group" |
| 93 | + subnet_group_description = "Example MemoryDB subnet group" |
| 94 | + subnet_ids = module.vpc.database_subnets |
| 95 | + subnet_group_tags = { |
| 96 | + subnet_group = "custom" |
| 97 | + } |
| 98 | + |
| 99 | + tags = local.tags |
| 100 | +} |
| 101 | + |
| 102 | +module "secondary_memory_db" { |
| 103 | + source = "../.." |
| 104 | + |
| 105 | + # Cluster |
| 106 | + name = "${local.name}-secondary" |
| 107 | + description = "Example MemoryDB cluster" |
| 108 | + |
| 109 | + # This makes it part of the multi region cluster |
| 110 | + multi_region_cluster_name = module.multi_region_cluster.multi_region_cluster_name |
| 111 | + |
| 112 | + # engine = "valkey" |
| 113 | + # engine_version = "7.3" |
| 114 | + auto_minor_version_upgrade = true |
| 115 | + node_type = "db.r7g.xlarge" |
| 116 | + num_shards = 2 |
| 117 | + num_replicas_per_shard = 2 |
| 118 | + |
| 119 | + # tls_enabled = true |
| 120 | + security_group_ids = [module.secondary_security_group.security_group_id] |
| 121 | + maintenance_window = "sun:23:00-mon:01:30" |
| 122 | + snapshot_retention_limit = 7 |
| 123 | + snapshot_window = "05:00-09:00" |
| 124 | + |
| 125 | + # ACL |
| 126 | + create_acl = false |
| 127 | + acl_name = "${local.name}-acl" |
| 128 | + acl_tags = { acl = "custom" } |
| 129 | + |
| 130 | + # Parameter group |
| 131 | + create_parameter_group = false |
| 132 | + |
| 133 | + # Subnet group |
| 134 | + subnet_group_name = "${local.name}-subnet-group" |
| 135 | + subnet_group_description = "Example MemoryDB subnet group" |
| 136 | + subnet_ids = module.secondary_vpc.database_subnets |
| 137 | + subnet_group_tags = { |
| 138 | + subnet_group = "custom" |
| 139 | + } |
| 140 | + |
| 141 | + tags = local.tags |
| 142 | + |
| 143 | + providers = { |
| 144 | + aws = aws.us-west-2 |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +################################################################################ |
| 149 | +# Supporting Resources |
| 150 | +################################################################################ |
| 151 | + |
| 152 | +module "vpc" { |
| 153 | + source = "terraform-aws-modules/vpc/aws" |
| 154 | + version = "~> 5.0" |
| 155 | + |
| 156 | + name = local.name |
| 157 | + cidr = "10.99.0.0/18" |
| 158 | + |
| 159 | + azs = ["${local.region}a", "${local.region}b", "${local.region}d"] # Caution: check which zones are available |
| 160 | + private_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"] |
| 161 | + database_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"] |
| 162 | + |
| 163 | + create_database_subnet_group = true |
| 164 | + enable_nat_gateway = false |
| 165 | + |
| 166 | + manage_default_security_group = true |
| 167 | + default_security_group_ingress = [] |
| 168 | + default_security_group_egress = [] |
| 169 | + |
| 170 | + tags = local.tags |
| 171 | +} |
| 172 | + |
| 173 | +module "secondary_vpc" { |
| 174 | + source = "terraform-aws-modules/vpc/aws" |
| 175 | + version = "~> 5.0" |
| 176 | + |
| 177 | + name = local.name |
| 178 | + cidr = "10.99.0.0/18" |
| 179 | + |
| 180 | + azs = ["us-west-2a", "us-west-2b", "us-west-2d"] # Caution: check which zones are available |
| 181 | + private_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"] |
| 182 | + database_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"] |
| 183 | + |
| 184 | + create_database_subnet_group = true |
| 185 | + enable_nat_gateway = false |
| 186 | + |
| 187 | + manage_default_security_group = true |
| 188 | + default_security_group_ingress = [] |
| 189 | + default_security_group_egress = [] |
| 190 | + |
| 191 | + tags = local.tags |
| 192 | + |
| 193 | + providers = { |
| 194 | + aws = aws.us-west-2 |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +module "security_group" { |
| 199 | + source = "terraform-aws-modules/security-group/aws" |
| 200 | + version = "~> 4.0" |
| 201 | + |
| 202 | + name = local.name |
| 203 | + description = "Security group for ${local.name}" |
| 204 | + vpc_id = module.vpc.vpc_id |
| 205 | + |
| 206 | + ingress_cidr_blocks = module.vpc.private_subnets_cidr_blocks |
| 207 | + ingress_rules = ["redis-tcp"] |
| 208 | + |
| 209 | + egress_cidr_blocks = [module.vpc.vpc_cidr_block] |
| 210 | + egress_rules = ["all-all"] |
| 211 | + |
| 212 | + tags = local.tags |
| 213 | +} |
| 214 | + |
| 215 | +module "secondary_security_group" { |
| 216 | + source = "terraform-aws-modules/security-group/aws" |
| 217 | + version = "~> 4.0" |
| 218 | + |
| 219 | + name = local.name |
| 220 | + description = "Security group for ${local.name}" |
| 221 | + vpc_id = module.secondary_vpc.vpc_id |
| 222 | + |
| 223 | + ingress_cidr_blocks = module.secondary_vpc.private_subnets_cidr_blocks |
| 224 | + ingress_rules = ["redis-tcp"] |
| 225 | + |
| 226 | + egress_cidr_blocks = [module.secondary_vpc.vpc_cidr_block] |
| 227 | + egress_rules = ["all-all"] |
| 228 | + |
| 229 | + tags = local.tags |
| 230 | + |
| 231 | + providers = { |
| 232 | + aws = aws.us-west-2 |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +resource "random_password" "password" { |
| 237 | + length = 16 |
| 238 | + special = true |
| 239 | + override_special = "_%@" |
| 240 | +} |
0 commit comments