Files
engine/lib/aws/services/redis/main.j2.tf
2022-02-09 16:51:51 +01:00

115 lines
2.7 KiB
HCL

data "aws_vpc" "selected" {
filter {
name = "tag:ClusterId"
values = [var.kubernetes_cluster_id]
}
}
data "aws_subnet_ids" "selected" {
vpc_id = data.aws_vpc.selected.id
filter {
name = "tag:ClusterId"
values = [var.kubernetes_cluster_id]
}
filter {
name = "tag:Service"
values = ["Elasticache"]
}
}
data "aws_security_group" "selected" {
filter {
name = "tag:Name"
values = ["qovery-eks-workers"]
}
filter {
name = "tag:kubernetes.io/cluster/${var.kubernetes_cluster_id}"
values = ["owned"]
}
}
resource "helm_release" "elasticache_instance_external_name" {
name = "${aws_elasticache_cluster.elasticache_cluster.id}-externalname"
chart = "external-name-svc"
namespace = "{{namespace}}"
atomic = true
max_history = 50
set {
name = "target_hostname"
value = aws_elasticache_cluster.elasticache_cluster.cache_nodes.0.address
}
set {
name = "source_fqdn"
value = "{{database_fqdn}}"
}
set {
name = "app_id"
value = "{{database_id}}"
}
set {
name = "service_name"
value = "{{service_name}}"
}
set {
name = "publicly_accessible"
value = var.publicly_accessible
}
depends_on = [
aws_elasticache_cluster.elasticache_cluster
]
}
resource "aws_elasticache_cluster" "elasticache_cluster" {
cluster_id = var.elasticache_identifier
tags = local.redis_database_tags
# Elasticache instance basics
port = var.port
engine_version = var.elasticache_version
# Thanks GOD AWS for not using SemVer and adding your own versioning system,
# need to add this dirty trick while Hashicorp fix this issue
# https://github.com/hashicorp/terraform-provider-aws/issues/15625
lifecycle {
ignore_changes = [engine_version]
}
{%- if replication_group_id is defined %}
# todo: add cluster mode and replicas support
{%- else %}
engine = "redis"
node_type = var.instance_class
num_cache_nodes = var.elasticache_instances_number
parameter_group_name = var.parameter_group_name
{%- endif %}
{%- if snapshot is defined and snapshot["snapshot_id"] %}
# Snapshot
snapshot_name = var.snapshot_identifier
{%- endif %}
# Network
# WARNING: this value cna't get fetch from data sources and is linked to the bootstrap phase
subnet_group_name = "elasticache-${data.aws_vpc.selected.id}"
# Security
security_group_ids = data.aws_security_group.selected.*.id
# Maintenance and upgrades
apply_immediately = var.apply_changes_now
maintenance_window = var.preferred_maintenance_window
# Backups
snapshot_window = var.preferred_backup_window
snapshot_retention_limit = var.backup_retention_period
{%- if not skip_final_snapshot %}
final_snapshot_identifier = var.final_snapshot_name
{%- endif %}
}