|
1 | | -locals {} |
| 1 | +provider "aws" { |
| 2 | + region = local.region |
| 3 | +} |
| 4 | + |
| 5 | +locals { |
| 6 | + region = "us-east-1" |
| 7 | + name = "memorydb-ex-${replace(basename(path.cwd), "_", "-")}" |
| 8 | + |
| 9 | + tags = { |
| 10 | + Example = local.name |
| 11 | + Environment = "dev" |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +################################################################################ |
| 16 | +# Supporting Resources |
| 17 | +################################################################################ |
| 18 | + |
| 19 | +module "vpc" { |
| 20 | + source = "terraform-aws-modules/vpc/aws" |
| 21 | + version = "~> 3.0" |
| 22 | + |
| 23 | + name = local.name |
| 24 | + cidr = "10.99.0.0/18" |
| 25 | + |
| 26 | + azs = ["${local.region}a", "${local.region}b", "${local.region}d"] # Caution: check which zones are available |
| 27 | + private_subnets = ["10.99.0.0/24", "10.99.1.0/24", "10.99.2.0/24"] |
| 28 | + database_subnets = ["10.99.3.0/24", "10.99.4.0/24", "10.99.5.0/24"] |
| 29 | + |
| 30 | + create_database_subnet_group = true |
| 31 | + enable_nat_gateway = false |
| 32 | + |
| 33 | + manage_default_security_group = true |
| 34 | + default_security_group_ingress = [] |
| 35 | + default_security_group_egress = [] |
| 36 | + |
| 37 | + tags = local.tags |
| 38 | +} |
| 39 | + |
| 40 | +module "security_group" { |
| 41 | + source = "terraform-aws-modules/security-group/aws" |
| 42 | + version = "~> 4.0" |
| 43 | + |
| 44 | + name = local.name |
| 45 | + description = "Security group for ${local.name}" |
| 46 | + vpc_id = module.vpc.vpc_id |
| 47 | + |
| 48 | + ingress_cidr_blocks = module.vpc.private_subnets_cidr_blocks |
| 49 | + ingress_rules = ["redis-tcp"] |
| 50 | + |
| 51 | + egress_cidr_blocks = [module.vpc.vpc_cidr_block] |
| 52 | + egress_rules = ["all-all"] |
| 53 | + |
| 54 | + tags = local.tags |
| 55 | +} |
| 56 | + |
| 57 | +resource "aws_sns_topic" "example" { |
| 58 | + name = local.name |
| 59 | + kms_master_key_id = "alias/aws/sns" |
| 60 | + |
| 61 | + tags = local.tags |
| 62 | +} |
| 63 | + |
| 64 | +resource "random_password" "password" { |
| 65 | + for_each = toset(["admin", "readonly"]) |
| 66 | + |
| 67 | + length = 16 |
| 68 | + special = true |
| 69 | + override_special = "_%@" |
| 70 | +} |
| 71 | + |
| 72 | +################################################################################ |
| 73 | +# MemoryDB Module |
| 74 | +################################################################################ |
| 75 | + |
| 76 | +module "memory_db_disabled" { |
| 77 | + source = "../.." |
| 78 | + |
| 79 | + name = "${local.name}-disabled" |
| 80 | + create = false |
| 81 | +} |
| 82 | + |
| 83 | +module "memory_db" { |
| 84 | + source = "../.." |
| 85 | + |
| 86 | + # Cluster |
| 87 | + name = local.name |
| 88 | + description = "Example MemoryDB cluster" |
| 89 | + |
| 90 | + engine_version = "6.2" |
| 91 | + auto_minor_version_upgrade = true |
| 92 | + node_type = "db.t4g.small" |
| 93 | + num_shards = 2 |
| 94 | + num_replicas_per_shard = 2 |
| 95 | + |
| 96 | + tls_enabled = true |
| 97 | + security_group_ids = [module.security_group.security_group_id] |
| 98 | + maintenance_window = "sun:23:00-mon:01:30" |
| 99 | + sns_topic_arn = aws_sns_topic.example.arn |
| 100 | + snapshot_retention_limit = 7 |
| 101 | + snapshot_window = "05:00-09:00" |
| 102 | + |
| 103 | + # Users |
| 104 | + users = { |
| 105 | + admin = { |
| 106 | + user_name = "admin-user" |
| 107 | + access_string = "on ~* &* +@all" |
| 108 | + passwords = [random_password.password["admin"].result] |
| 109 | + tags = { user = "admin" } |
| 110 | + } |
| 111 | + readonly = { |
| 112 | + user_name = "readonly-user" |
| 113 | + access_string = "on ~* &* -@all +@read" |
| 114 | + passwords = [random_password.password["readonly"].result] |
| 115 | + tags = { user = "readonly" } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + # ACL |
| 120 | + acl_name = "${local.name}-acl" |
| 121 | + acl_tags = { acl = "custom" } |
| 122 | + |
| 123 | + # Parameter group |
| 124 | + parameter_group_name = "${local.name}-param-group" |
| 125 | + parameter_group_description = "Example MemoryDB parameter group" |
| 126 | + parameter_group_family = "memorydb_redis6" |
| 127 | + parameter_group_parameters = [ |
| 128 | + { |
| 129 | + name = "activedefrag" |
| 130 | + value = "yes" |
| 131 | + } |
| 132 | + ] |
| 133 | + parameter_group_tags = { |
| 134 | + parameter_group = "custom" |
| 135 | + } |
| 136 | + |
| 137 | + # Subnet group |
| 138 | + subnet_group_name = "${local.name}-subnet-group" |
| 139 | + subnet_group_description = "Example MemoryDB subnet group" |
| 140 | + subnet_ids = module.vpc.database_subnets |
| 141 | + subnet_group_tags = { |
| 142 | + subnet_group = "custom" |
| 143 | + } |
| 144 | + |
| 145 | + tags = local.tags |
| 146 | +} |
0 commit comments