|
| 1 | +provider "aws" { |
| 2 | + region = local.region |
| 3 | +} |
| 4 | + |
| 5 | +data "aws_availability_zones" "available" {} |
| 6 | + |
| 7 | +locals { |
| 8 | + region = "us-east-1" |
| 9 | + name = "ex-${basename(path.cwd)}" |
| 10 | + |
| 11 | + vpc_cidr = "10.0.0.0/16" |
| 12 | + azs = slice(data.aws_availability_zones.available.names, 0, 3) |
| 13 | + |
| 14 | + tags = { |
| 15 | + Name = local.name |
| 16 | + Example = local.name |
| 17 | + Repository = "https://github.com/clowdhaus/terraform-aws-elasticache" |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +################################################################################ |
| 22 | +# ElastiCache Module |
| 23 | +################################################################################ |
| 24 | + |
| 25 | +module "elasticache" { |
| 26 | + source = "../../" |
| 27 | + |
| 28 | + replication_group_id = local.name |
| 29 | + create_cluster = false |
| 30 | + create_replication_group = true |
| 31 | + engine_version = "7.1" |
| 32 | + node_type = "cache.t4g.small" |
| 33 | + |
| 34 | + # Cluster mode |
| 35 | + cluster_mode_enabled = true |
| 36 | + num_node_groups = 2 |
| 37 | + replicas_per_node_group = 3 |
| 38 | + automatic_failover_enabled = true |
| 39 | + multi_az_enabled = true |
| 40 | + |
| 41 | + user_group_ids = [module.elasticache_user_group.group_id] |
| 42 | + |
| 43 | + # Security group |
| 44 | + vpc_id = module.vpc.vpc_id |
| 45 | + security_group_rules = { |
| 46 | + ingress_vpc = { |
| 47 | + # Default type is `ingress` |
| 48 | + # Default port is based on the default engine port |
| 49 | + description = "VPC traffic" |
| 50 | + cidr_ipv4 = module.vpc.vpc_cidr_block |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + # subnet group |
| 55 | + subnet_group_name = local.name |
| 56 | + subnet_group_description = "${title(local.name)} subnet group" |
| 57 | + subnet_ids = module.vpc.private_subnets |
| 58 | + |
| 59 | + maintenance_window = "sun:05:00-sun:09:00" |
| 60 | + apply_immediately = true |
| 61 | + |
| 62 | + # parameter group |
| 63 | + create_parameter_group = true |
| 64 | + parameter_group_family = "redis7" |
| 65 | + parameter_group_description = "${title(local.name)} parameter group" |
| 66 | + parameters = [ |
| 67 | + { |
| 68 | + name = "latency-tracking" |
| 69 | + value = "yes" |
| 70 | + } |
| 71 | + ] |
| 72 | + |
| 73 | + tags = local.tags |
| 74 | +} |
| 75 | + |
| 76 | +################################################################################ |
| 77 | +# ElastiCache Module |
| 78 | +################################################################################ |
| 79 | + |
| 80 | +module "elasticache_user_group" { |
| 81 | + source = "../../modules/user-group" |
| 82 | + |
| 83 | + user_group_id = local.name |
| 84 | + |
| 85 | + default_user = { |
| 86 | + user_id = "default${lower(replace(local.name, "-", ""))}" |
| 87 | + passwords = ["password123456789"] |
| 88 | + } |
| 89 | + |
| 90 | + users = { |
| 91 | + moe = { |
| 92 | + access_string = "on ~* +@all" |
| 93 | + passwords = ["password123456789"] |
| 94 | + } |
| 95 | + |
| 96 | + larry = { |
| 97 | + access_string = "on ~* +@all" |
| 98 | + |
| 99 | + authentication_mode = { |
| 100 | + type = "iam" |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + curly = { |
| 105 | + access_string = "on ~* +@all" |
| 106 | + |
| 107 | + authentication_mode = { |
| 108 | + type = "password" |
| 109 | + passwords = ["password123456789", "password987654321"] |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + tags = local.tags |
| 115 | +} |
| 116 | + |
| 117 | +################################################################################ |
| 118 | +# Supporting Resources |
| 119 | +################################################################################ |
| 120 | + |
| 121 | +module "vpc" { |
| 122 | + source = "terraform-aws-modules/vpc/aws" |
| 123 | + version = "~> 5.0" |
| 124 | + |
| 125 | + name = local.name |
| 126 | + cidr = local.vpc_cidr |
| 127 | + |
| 128 | + azs = local.azs |
| 129 | + public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)] |
| 130 | + private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 10)] |
| 131 | + |
| 132 | + tags = local.tags |
| 133 | +} |
0 commit comments