feat: add elasticache support

This commit is contained in:
Pierre Mavro
2020-11-24 22:11:18 +01:00
committed by Pierre Mavro
parent 643461ca43
commit b686035926
4 changed files with 250 additions and 12 deletions

View File

@@ -62,6 +62,7 @@ resource "aws_route_table_association" "elasticache_cluster_zone_c" {
resource "aws_elasticache_subnet_group" "elasticache" {
description = "Elasticache linked to ${var.eks_cluster_id}"
# WARNING: this "name" value is used into elasticache clusters, you need to update it accordingly
name = "elasticache-${aws_vpc.eks.id}"
subnet_ids = flatten([aws_subnet.elasticache_zone_a.*.id, aws_subnet.elasticache_zone_b.*.id, aws_subnet.elasticache_zone_c.*.id])
}
@@ -70,7 +71,7 @@ resource "aws_elasticache_subnet_group" "elasticache" {
resource "aws_security_group_rule" "elasticache_remote_access" {
cidr_blocks = ["0.0.0.0/0"]
description = "Allow Elasticache incoming access from anywhere"
description = "Allow Redis incoming access from anywhere"
from_port = 6379
protocol = "tcp"
security_group_id = aws_security_group.eks_cluster_workers.id

View File

@@ -0,0 +1,104 @@
data "aws_vpc" "selected" {
filter {
name = "tag:ClusterId"
values = [var.eks_cluster_id]
}
}
data "aws_subnet_ids" "selected" {
vpc_id = data.aws_vpc.selected.id
filter {
name = "tag:ClusterId"
values = [var.eks_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.eks_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}}"
}
depends_on = [
aws_elasticache_cluster.elasticache_cluster
]
}
resource "aws_elasticache_cluster" "elasticache_cluster" {
cluster_id = var.elasticache_identifier
tags = {
cluster_name = var.cluster_name
region = var.region
q_client_id = var.q_customer_id
q_environment_id = var.q_environment_id
q_project_id = var.q_project_id
database_identifier = var.elasticache_identifier
{% if resource_expiration_in_seconds is defined %}ttl = var.resource_expiration_in_seconds{% endif %}
}
# Elasticache instance basics
engine_version = var.elasticache_version
port = var.port
{%- 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
}

View File

@@ -0,0 +1,133 @@
# Qovery
variable "cluster_name" {
description = "Kubernetes cluster name"
default = "{{ cluster_name }}"
type = string
}
variable "region" {
description = "AWS region to store terraform state and lock"
default = "{{ region }}"
type = string
}
variable "eks_cluster_id" {
description = "Kubernetes cluster name with region"
default = "{{ eks_cluster_id }}"
type = string
}
variable "region_cluster_name" {
description = "AWS region to store terraform state and lock"
default = "{{ region }}-{{ cluster_name }}"
type = string
}
variable "q_project_id" {
description = "Qovery project ID"
default = "{{ project_id }}"
type = string
}
variable "q_customer_id" {
description = "Qovery customer ID"
default = "{{ owner_id }}"
type = string
}
variable "q_environment_id" {
description = "Qovery client environment"
default = "{{ environment_id }}"
type = string
}
# elasticache instance basics
variable "elasticache_identifier" {
description = "Elasticache cluster name (Cluster identifier)"
default = "{{ fqdn_id }}"
type = string
}
variable "elasticache_version" {
description = "Elasticache version"
default = "{{ version }}"
type = string
}
variable "parameter_group_name" {
description = "Elasticache parameter group name"
default = "{% if version == 6 %}default.redis6.x{% else %}default.redis5.0{% endif %}"
type = string
}
variable "elasticache_instances_number" {
description = "Elasticache instance numbers"
default = 1
type = number
}
variable "port" {
description = "Elasticache instance port"
default = {{ database_port }}
type = number
}
variable "instance_class" {
description = "Type of instance: https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/CacheNodes.SupportedTypes.html"
default = "{{database_instance_type}}"
type = string
}
# Upgrades
variable "auto_minor_version_upgrade" {
description = "Indicates that minor engine upgrades will be applied automatically to the DB instance during the maintenance window"
default = true
type = bool
}
variable "apply_changes_now" {
description = "Apply changes now or during the during the maintenance window"
default = true
type = bool
}
variable "preferred_maintenance_window" {
description = "Maintenance window"
default = "Tue:02:00-Tue:04:00"
type = string
}
# Backups
variable "backup_retention_period" {
description = "Backup rentention period"
default = 7
type = number
}
variable "preferred_backup_window" {
description = "Maintenance window"
default = "00:00-01:00"
type = string
}
{%- if snapshot is defined %}
# Snapshots
variable "snapshot_identifier" {
description = "Snapshot ID to restore"
default = "{{ snapshot['snapshot_id']}}"
type = string
}
{% endif %}
{%- if resource_expiration_in_seconds is defined %}
# Pleco ttl
variable "resource_expiration_in_seconds" {
description = "Resource expiration in seconds"
default = {{ resource_expiration_in_seconds }}
type = number
}
{% endif %}

View File

@@ -825,14 +825,14 @@ fn redis_v6_deploy_a_working_environment() {
test_redis_configuration(context, environment, "6.0");
}
// test Redis v3.6 with production environment (Elasticcache)
// #[test]
// #[ignore]
// fn redis_v3_6_deploy_a_working_environment_with_production() {
// let context = context();
//
// let mut environment = test_utilities::aws::working_minimal_environment(&context);
// environment.kind = Kind::Production;
//
// test_redis_configuration(context, environment, "5.0");
// }
// test Redis 5.0 with production environment (Elasticcache)
#[test]
#[ignore]
fn redis_v3_6_deploy_a_working_environment_with_production() {
let context = context();
let mut environment = test_utilities::aws::working_minimal_environment(&context);
environment.kind = Kind::Production;
test_redis_configuration(context, environment, "5.0");
}