generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from dxw/elasticache-redis
ElastiCache Redis
- Loading branch information
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
resource "aws_elasticache_parameter_group" "infrastructure_elasticache_cluster" { | ||
for_each = local.infrastructure_vpc_network_enable_public || local.infrastructure_vpc_network_enable_private ? { | ||
for k, v in local.infrastructure_elasticache : k => merge(v, { | ||
parameter_group_version = replace(v["engine_version"], "6.", "") != v["engine_version"] ? "6.x" : ( | ||
replace(v["engine_version"], "7.", "") != v["engine_version"] ? "7" : replace(v["engine_version"], "/\\.[\\d]+$/", "") | ||
) | ||
}) if v["engine"] == "redis" && v["type"] == "cluster" | ||
} : {} | ||
|
||
name = "pg-${local.resource_prefix_hash}-${each.key}-${replace(each.value["parameter_group_version"], ".", "-")}" | ||
|
||
description = "Parameter Group for ${local.resource_prefix_hash} ${each.key} ${each.value["parameter_group_version"]}" | ||
|
||
family = "${each.value["engine"]}${each.value["parameter_group_version"]}" | ||
|
||
dynamic "parameter" { | ||
for_each = each.value["parameters"] != null ? each.value["parameters"] : {} | ||
content { | ||
name = parameter.value.name | ||
value = parameter.value.value | ||
} | ||
} | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
resource "aws_elasticache_subnet_group" "infrastructure_elasticache_cluster_subnet_group" { | ||
for_each = local.infrastructure_vpc_network_enable_public || local.infrastructure_vpc_network_enable_private ? { | ||
for k, v in local.infrastructure_elasticache : k => v if v["engine"] == "redis" && v["type"] == "cluster" | ||
} : {} | ||
|
||
name = "id-${local.resource_prefix_hash}-${substr(sha512(each.key), 0, 6)}" | ||
subnet_ids = local.infrastructure_vpc_network_enable_private ? [for subnet in aws_subnet.infrastructure_private : subnet.id] : local.infrastructure_vpc_network_enable_public ? [for subnet in aws_subnet.infrastructure_public : subnet.id] : null | ||
} | ||
|
||
resource "aws_elasticache_replication_group" "infrastructure_elasticache_cluster" { | ||
for_each = local.infrastructure_vpc_network_enable_public || local.infrastructure_vpc_network_enable_private ? { | ||
for k, v in local.infrastructure_elasticache : k => v if v["engine"] == "redis" && v["type"] == "cluster" | ||
} : {} | ||
|
||
replication_group_id = "id-${local.resource_prefix_hash}-${substr(sha512(each.key), 0, 6)}" | ||
description = "ElastiCache replication group for ${local.resource_prefix} ${each.key}" | ||
|
||
node_type = each.value["cluster_node_type"] | ||
num_cache_clusters = each.value["cluster_node_count"] | ||
engine_version = each.value["engine_version"] | ||
port = local.elasticache_ports[each.value["engine"]] | ||
at_rest_encryption_enabled = true | ||
|
||
parameter_group_name = aws_elasticache_parameter_group.infrastructure_elasticache_cluster[each.key].id | ||
subnet_group_name = aws_elasticache_subnet_group.infrastructure_elasticache_cluster_subnet_group[each.key].id | ||
security_group_ids = [aws_security_group.infrastructure_elasticache[each.key].id] | ||
|
||
maintenance_window = "Mon:19:00-Mon:22:00" | ||
snapshot_window = "22:00-23:59" | ||
snapshot_retention_limit = each.value["snapshot_retention_limit"] != null ? each.value["snapshot_retention_limit"] : 0 | ||
|
||
automatic_failover_enabled = false | ||
apply_immediately = true | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-${each.key}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
resource "aws_security_group" "infrastructure_elasticache" { | ||
for_each = local.infrastructure_vpc_network_enable_public || local.infrastructure_vpc_network_enable_private ? local.infrastructure_elasticache : {} | ||
|
||
name = "${local.resource_prefix}-infrastructure-elasticache-${each.key}" | ||
description = "Infrastructure ElastiCache" | ||
vpc_id = aws_vpc.infrastructure[0].id | ||
} | ||
|
||
resource "aws_security_group_rule" "infrastructure_elasticache_ingress_tcp" { | ||
for_each = local.infrastructure_vpc_network_enable_public || local.infrastructure_vpc_network_enable_private ? local.infrastructure_elasticache : {} | ||
|
||
description = "Allow ElastiCache port tcp ingress from ECS instances if launched, otherwise from the subnet" | ||
type = "ingress" | ||
from_port = local.elasticache_ports[each.value["engine"]] | ||
to_port = local.elasticache_ports[each.value["engine"]] | ||
protocol = "tcp" | ||
cidr_blocks = local.enable_infrastructure_ecs_cluster ? null : local.infrastructure_vpc_network_enable_private ? [for subnet in aws_subnet.infrastructure_private : subnet.cidr_block] : local.infrastructure_vpc_network_enable_public ? [for subnet in aws_subnet.infrastructure_public : subnet.cidr_block] : null | ||
source_security_group_id = local.enable_infrastructure_ecs_cluster ? aws_security_group.infrastructure_ecs_cluster_container_instances[0].id : null | ||
security_group_id = aws_security_group.infrastructure_elasticache[each.key].id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
resource "aws_elasticache_serverless_cache" "infrastructure_elasticache" { | ||
for_each = local.infrastructure_vpc_network_enable_public || local.infrastructure_vpc_network_enable_private ? { | ||
for k, v in local.infrastructure_elasticache : k => v if v["engine"] == "redis" && v["type"] == "serverless" | ||
} : {} | ||
|
||
engine = each.value["engine"] | ||
name = "id-${local.resource_prefix_hash}-${substr(sha512(each.key), 0, 6)}" | ||
cache_usage_limits { | ||
data_storage { | ||
maximum = each.value["serverless_max_storage"] | ||
unit = "GB" | ||
} | ||
ecpu_per_second { | ||
maximum = each.value["serverless_max_ecpu"] | ||
} | ||
} | ||
description = "${local.resource_prefix} ${each.key}" | ||
kms_key_id = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null | ||
major_engine_version = each.value["engine_version"] | ||
snapshot_retention_limit = each.value["snapshot_retention_limit"] != null ? each.value["snapshot_retention_limit"] : 0 | ||
security_group_ids = [aws_security_group.infrastructure_elasticache[each.key].id] | ||
subnet_ids = local.infrastructure_vpc_network_enable_private ? [for subnet in aws_subnet.infrastructure_private : subnet.id] : local.infrastructure_vpc_network_enable_public ? [for subnet in aws_subnet.infrastructure_public : subnet.id] : null | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-${each.key}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters