Skip to content

Commit 2a4f3a3

Browse files
authored
IN-326 add elasticache cluster for cacheing
2 parents c2660eb + 18502f6 commit 2a4f3a3

File tree

8 files changed

+98
-7
lines changed

8 files changed

+98
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#Update this date to trigger update of layers: 290416
1+
#Update this date to trigger update of layers: 290418
22
Flask
33
Werkzeug
44
pyjwt
5+
redis

terraform/environment/dns.tf

+9
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,12 @@ resource "aws_route53_record" "environment_record" {
4949
zone_id = aws_api_gateway_domain_name.lpa_data.regional_zone_id
5050
}
5151
}
52+
53+
resource "aws_route53_record" "lpa_redis" {
54+
name = "redis"
55+
type = "CNAME"
56+
zone_id = data.aws_route53_zone.environment_cert.id
57+
records = [aws_elasticache_replication_group.lpa_redis.primary_endpoint_address]
58+
ttl = 300
59+
provider = aws.management
60+
}

terraform/environment/elasticache.tf

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
resource "aws_elasticache_replication_group" "lpa_redis" {
2+
automatic_failover_enabled = local.account.elasticache_count == 1 ? false : true
3+
engine = "redis"
4+
engine_version = "5.0.6"
5+
replication_group_id = "lpa-data-redis-${local.environment}"
6+
replication_group_description = "Replication Group for LPA Data"
7+
node_type = "cache.t2.small"
8+
number_cache_clusters = local.account.elasticache_count
9+
parameter_group_name = "default.redis5.0"
10+
port = 6379
11+
subnet_group_name = "private-redis"
12+
security_group_ids = [aws_security_group.lpa_redis_sg.id]
13+
tags = local.default_tags
14+
apply_immediately = true
15+
16+
lifecycle {
17+
ignore_changes = [number_cache_clusters]
18+
}
19+
}
20+
21+
resource "aws_security_group" "lpa_redis_sg" {
22+
name_prefix = "${local.environment}-redis-sg"
23+
vpc_id = local.account.vpc_id
24+
25+
lifecycle {
26+
create_before_destroy = true
27+
}
28+
29+
revoke_rules_on_delete = true
30+
31+
tags = merge(
32+
local.default_tags,
33+
{
34+
"Name" = "${local.environment}-redis-sg"
35+
},
36+
)
37+
}
38+
39+
locals {
40+
redis_rules = {
41+
cache_out = {
42+
port = 6379
43+
type = "egress"
44+
protocol = "tcp"
45+
target_type = "self"
46+
target = true
47+
}
48+
cache_in = {
49+
port = 6379
50+
type = "ingress"
51+
protocol = "tcp"
52+
target_type = "self"
53+
target = true
54+
}
55+
}
56+
}
57+
58+
resource "aws_security_group_rule" "lpa_redis_rules" {
59+
for_each = local.redis_rules
60+
61+
type = each.value.type
62+
protocol = each.value.protocol
63+
from_port = each.value.port
64+
to_port = each.value.port
65+
security_group_id = aws_security_group.lpa_redis_sg.id
66+
source_security_group_id = each.value.target_type == "security_group_id" ? each.value.target : null
67+
prefix_list_ids = each.value.target_type == "prefix_list_id" ? [each.value.target] : null
68+
description = each.key
69+
cidr_blocks = each.value.target_type == "cidr_block" ? [each.value.target] : null
70+
self = each.value.target_type == "self" ? each.value.target : null
71+
}

terraform/environment/lambda.tf

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module "lambda_lpa_v1" {
99
openapi_version = "v1"
1010
rest_api = aws_api_gateway_rest_api.lpa
1111
account = local.account
12+
redis_sg_id = aws_security_group.lpa_redis_sg.id
1213
}
1314

1415
//Modify here for new version - create new one. keep original

terraform/environment/modules/lambda/lambda.tf

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ resource "aws_lambda_function" "lambda_function" {
1818
depends_on = [aws_cloudwatch_log_group.lambda]
1919
layers = [aws_lambda_layer_version.lambda_layer.arn]
2020
vpc_config {
21-
subnet_ids = var.aws_subnet_ids
22-
security_group_ids = [data.aws_security_group.lambda_api_ingress.id]
21+
subnet_ids = var.aws_subnet_ids
22+
security_group_ids = [
23+
data.aws_security_group.lambda_api_ingress.id,
24+
var.redis_sg_id
25+
]
2326
}
2427
environment {
2528
variables = {
@@ -49,7 +52,7 @@ resource "aws_lambda_permission" "lambda_permission" {
4952
resource "aws_lambda_layer_version" "lambda_layer" {
5053
filename = data.archive_file.lambda_layer_archive.output_path
5154
source_code_hash = data.archive_file.lambda_layer_archive.output_base64sha256
52-
layer_name = "requirement_${var.account.target_environment}"
55+
layer_name = "lpa_requirements_${var.account.target_environment}"
5356

5457
compatible_runtimes = ["python3.7"]
5558

terraform/environment/modules/lambda/variables.tf

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ variable "openapi_version" {}
2525
variable "rest_api" {}
2626

2727
variable "account" {}
28+
29+
variable "redis_sg_id" {}

terraform/environment/terraform.tfvars.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"target_environment": "dev",
1515
"vpc_id": "vpc-faf2d99e",
1616
"logger_level": "DEBUG",
17-
"threshold": 10
17+
"threshold": 10,
18+
"elasticache_count": 1
1819
},
1920
"preproduction": {
2021
"account_id": "492687888235",
@@ -29,7 +30,8 @@
2930
"target_environment": "preproduction",
3031
"vpc_id": "vpc-037acd53d9ce813b4",
3132
"logger_level": "INFO",
32-
"threshold": 10
33+
"threshold": 10,
34+
"elasticache_count": 1
3335
},
3436
"production": {
3537
"account_id": "649098267436",
@@ -44,7 +46,8 @@
4446
"target_environment": "production",
4547
"vpc_id": "vpc-6809cc0f",
4648
"logger_level": "INFO",
47-
"threshold": 1
49+
"threshold": 1,
50+
"elasticache_count": 2
4851
}
4952
}
5053
}

terraform/environment/variables.tf

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ variable "accounts" {
1919
session_data = string
2020
target_environment = string
2121
threshold = number
22+
elasticache_count = number
2223
})
2324
)
2425
}

0 commit comments

Comments
 (0)