Skip to content

Commit 667ac97

Browse files
authored
167 add cache (#177)
* added cache * allow EKS worker to access cache store * add cache service with template * small fix * updated after review * set default: no cache * small fix * a few changes after review
1 parent 291bc7a commit 667ac97

9 files changed

Lines changed: 108 additions & 0 deletions

File tree

templates/kubernetes/terraform/environments/prod/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ module "kubernetes" {
8484

8585
notification_service_enabled = <%if eq (index .Params `sendgridApiKey`) "" %>false<% else %>true<% end %>
8686
notification_service_highly_available = true
87+
88+
cache_store = "<% index .Params `cacheStore` %>"
8789
}

templates/kubernetes/terraform/environments/stage/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ module "kubernetes" {
8383

8484
notification_service_enabled = <%if eq (index .Params `sendgridApiKey`) "" %>false<% else %>true<% end %>
8585
notification_service_highly_available = false
86+
87+
cache_store = "<% index .Params `cacheStore` %>"
8688
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
data "aws_elasticache_replication_group" "redis" {
2+
count = var.cache_store == "redis" ? 1 : 0
3+
4+
replication_group_id = "${var.project}-${var.environment}-redis"
5+
}
6+
7+
data "aws_elasticache_cluster" "memcached" {
8+
count = var.cache_store == "memcached" ? 1 : 0
9+
10+
cluster_id = "${var.project}-${var.environment}-memcached"
11+
}
12+
13+
locals {
14+
endpoint_address = var.cache_store == "redis" ? data.aws_elasticache_replication_group.redis[0].primary_endpoint_address : var.cache_store == "memcached" ? data.aws_elasticache_cluster.memcached[0].cluster_address : ""
15+
}
16+
17+
resource "kubernetes_service" "app_cache" {
18+
count = local.endpoint_address == "" ? 0 : 1
19+
20+
## this should match the deployable backend's name/namespace
21+
metadata {
22+
namespace = kubernetes_namespace.app_namespace.metadata[0].name
23+
name = "cache-${var.cache_store}"
24+
}
25+
spec {
26+
type = "ExternalName"
27+
external_name = local.endpoint_address
28+
}
29+
}

templates/kubernetes/terraform/modules/kubernetes/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,9 @@ variable "notification_service_highly_available" {
141141
type = bool
142142
default = true
143143
}
144+
145+
variable "cache_store" {
146+
description = "Cache store - redis or memcached"
147+
type = string
148+
default = "none"
149+
}

templates/terraform/environments/prod/main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ module "prod" {
110110
sendgrid_enabled = <%if eq (index .Params `sendgridApiKey`) "" %>false<% else %>true<% end %>
111111
sendgrid_api_key_secret_name = "${local.project}-sendgrid-<% index .Params `randomSeed` %>"
112112

113+
# Cache configuration
114+
## you may define "redis" or "memcached" as your cache store. If you define "none", there will be no cache service launched.
115+
## Check https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/SelectEngine.html to compare redis or memcached.
116+
cache_store = "<% index .Params `cacheStore` %>"
117+
118+
## See how to define node and instance type: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/nodes-select-size.html
119+
cache_cluster_size = 1
120+
cache_instance_type = "cache.r6g.large"
121+
113122
# Roles configuration
114123
roles = [
115124
{

templates/terraform/environments/stage/main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ module "stage" {
130130
sendgrid_enabled = <%if eq (index .Params `sendgridApiKey`) "" %>false<% else %>true<% end %>
131131
sendgrid_api_key_secret_name = "${local.project}-sendgrid-<% index .Params `randomSeed` %>"
132132

133+
# Cache configuration
134+
## you may define "redis" or "memcached" as your cache store. If you define "none", there will be no cache service launched.
135+
## Check https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/SelectEngine.html to compare redis or memcached.
136+
cache_store = "<% index .Params `cacheStore` %>"
137+
138+
## See how to define node and instance type: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/nodes-select-size.html
139+
cache_cluster_size = 1
140+
cache_instance_type = "cache.t2.micro"
141+
133142
# Roles configuration
134143
roles = [
135144
{

templates/terraform/modules/environment/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ module "user_access" {
179179
users = local.user_access_users
180180
}
181181

182+
module "cache" {
183+
count = var.cache_store == "none" ? 0 : 1
184+
185+
source = "commitdev/zero/aws//modules/cache"
186+
version = "0.1.16"
187+
188+
project = var.project
189+
environment = var.environment
190+
191+
cache_store = var.cache_store
192+
193+
vpc_id = module.vpc.vpc_id
194+
subnet_ids = module.vpc.private_subnets
195+
196+
cluster_size = var.cache_cluster_size
197+
instance_type = var.cache_instance_type
198+
availability_zones = module.vpc.azs
199+
security_groups = [module.eks.worker_security_group_id]
200+
201+
redis_transit_encryption_enabled = var.cache_redis_transit_encryption_enabled
202+
}
182203

183204
output "s3_hosting" {
184205
description = "used by access policy for s3 hosting bucket"

templates/terraform/modules/environment/variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,27 @@ variable "ci_user_name" {
181181
type = string
182182
description = "CI user name"
183183
}
184+
185+
variable "cache_instance_type" {
186+
type = string
187+
default = "cache.t2.micro"
188+
description = "Elastic cache instance type"
189+
}
190+
191+
variable "cache_cluster_size" {
192+
type = number
193+
default = 1
194+
description = "Number of nodes in cluster"
195+
}
196+
197+
variable "cache_store" {
198+
type = string
199+
default = "none"
200+
description = "Cache store - redis or memcached"
201+
}
202+
203+
variable "cache_redis_transit_encryption_enabled" {
204+
description = "Enable TLS for redis traffic. When this is enabled, your application needs to handle TLS connection. Note: redis-cli can not handle TLS."
205+
type = bool
206+
default = true
207+
}

zero-module.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ parameters:
6666
options:
6767
- "postgres"
6868
- "mysql"
69+
- field: cacheStore
70+
label: "Cache store to use (default: no cache)"
71+
options:
72+
- "none"
73+
- "redis"
74+
- "memcached"
6975
- field: loggingType
7076
label: Application logging to configure. Cloudwatch is cheaper with a more limited feature set. Elasticsearch + Kibana will set up more infrastructure but enable a much richer logging search and visualization experience.
7177
options:

0 commit comments

Comments
 (0)