Skip to content

Commit ed7fd34

Browse files
committed
adding redis cache main configuration
1 parent c8023a9 commit ed7fd34

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

examples/complete/main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ module "redis" {
1111
resource_group_name = "rg-shared-westeurope-01"
1212
location = "westeurope"
1313

14+
redis_server_settings = {
15+
demoredischache-shared = {
16+
sku_name = "Standard"
17+
capacity = 2
18+
}
19+
}
20+
21+
redis_configuration = {
22+
maxmemory_reserved = 2
23+
maxmemory_delta = 2
24+
maxmemory_policy = "allkeys-lru"
25+
}
1426

1527
# Tags for Azure Resources
1628
tags = {

main.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,43 @@ resource "azurerm_resource_group" "rg" {
2121
location = var.location
2222
tags = merge({ "Name" = format("%s", var.resource_group_name) }, var.tags, )
2323
}
24+
25+
resource "azurerm_redis_cache" "main" {
26+
for_each = var.redis_server_settings
27+
name = format("%s", each.key)
28+
resource_group_name = local.resource_group_name
29+
location = local.location
30+
capacity = each.value["capacity"]
31+
family = lookup(var.redis_family, each.value.sku_name)
32+
sku_name = each.value["sku_name"]
33+
enable_non_ssl_port = each.value["enable_non_ssl_port"]
34+
minimum_tls_version = each.value["minimum_tls_version"]
35+
private_static_ip_address = each.value["private_static_ip_address"]
36+
public_network_access_enabled = each.value["public_network_access_enabled"]
37+
replicas_per_master = each.value["sku_name"] == "Premium" && { for shard_count, v in var.redis_server_settings : shard_count => v } == null ? each.value["replicas_per_master"] : null
38+
shard_count = { for sku_name, v in var.redis_server_settings : sku_name => v } == "Premium" && { for replicas_per_master, v in var.redis_server_settings : replicas_per_master => v } == null ? each.value["shard_count"] : null
39+
subnet_id = each.value["sku_name"] == "Premium" ? var.subnet_id : null
40+
zones = each.value["zones"]
41+
tags = merge({ "Name" = format("%s", each.key) }, var.tags, )
42+
43+
redis_configuration {
44+
enable_authentication = lookup(var.redis_configuration, "enable_authentication", true)
45+
maxfragmentationmemory_reserved = lookup(var.redis_configuration, "maxfragmentationmemory_reserved", null)
46+
maxmemory_delta = lookup(var.redis_configuration, "maxmemory_delta")
47+
maxmemory_policy = lookup(var.redis_configuration, "maxmemory_policy")
48+
maxmemory_reserved = lookup(var.redis_configuration, "maxmemory_reserved")
49+
notify_keyspace_events = lookup(var.redis_configuration, "notify_keyspace_events")
50+
rdb_backup_enabled = lookup(var.redis_configuration, "rdb_backup_enabled", false)
51+
rdb_backup_frequency = { for rdb_backup_enabled, v in var.redis_configuration : rdb_backup_enabled => v } == true ? lookup(var.redis_configuration, "rdb_backup_frequency") : null
52+
rdb_backup_max_snapshot_count = { for rdb_backup_enabled, v in var.redis_configuration : rdb_backup_enabled => v } == true ? lookup(var.redis_configuration, "rdb_backup_max_snapshot_count") : null
53+
rdb_storage_connection_string = { for rdb_backup_enabled, v in var.redis_configuration : rdb_backup_enabled => v } == true ? lookup(var.redis_configuration, "rdb_storage_connection_string") : null
54+
}
55+
56+
lifecycle {
57+
# A bug in the Redis API where the original storage connection string isn't being returned
58+
ignore_changes = [redis_configuration.0.rdb_storage_connection_string]
59+
}
60+
61+
}
62+
63+

variables.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,65 @@ variable "location" {
1414
}
1515

1616

17+
variable "redis_instance_name" {
18+
description = "The name of the Redis instance"
19+
default = ""
20+
}
21+
22+
variable "redis_family" {
23+
type = map(any)
24+
description = "The SKU family/pricing group to use. Valid values are `C` (for `Basic/Standard` SKU family) and `P` (for `Premium`)"
25+
default = {
26+
Basic = "C"
27+
Standard = "C"
28+
Premium = "P"
29+
}
30+
}
1731

32+
variable "redis_server_settings" {
33+
type = map(object({
34+
capacity = number
35+
sku_name = string
36+
enable_non_ssl_port = optional(bool)
37+
minimum_tls_version = optional(string)
38+
private_static_ip_address = optional(string)
39+
public_network_access_enabled = optional(string)
40+
replicas_per_master = optional(number)
41+
shard_count = optional(number)
42+
zones = optional(list(string))
43+
patch_schedule = optional(object({
44+
days_of_week = optional(list(string))
45+
start_hour_utc = optional(number)
46+
}))
47+
}))
48+
description = "optional redis server setttings for both Premium and Standard/Basic SKU"
49+
default = {}
50+
}
51+
52+
variable "subnet_id" {
53+
description = "The ID of the Subnet within which the Redis Cache should be deployed. Only available when using the Premium SKU and this subnet s "
54+
default = null
55+
}
56+
57+
variable "redis_configuration" {
58+
type = object({
59+
aof_backup_enabled = optional(bool)
60+
aof_storage_connection_string_0 = optional(string)
61+
aof_storage_connection_string_1 = optional(string)
62+
enable_authentication = optional(bool)
63+
maxmemory_reserved = optional(number)
64+
maxmemory_delta = optional(number)
65+
maxmemory_policy = optional(string)
66+
maxfragmentationmemory_reserved = optional(number)
67+
rdb_backup_enabled = optional(bool)
68+
rdb_backup_frequency = optional(number)
69+
rdb_backup_max_snapshot_count = optional(number)
70+
rdb_storage_connection_string = optional(string)
71+
notify_keyspace_events = optional(string)
72+
})
73+
description = "Configuration for the Redis instance"
74+
default = {}
75+
}
1876

1977
variable "tags" {
2078
description = "A map of tags to add to all resources"

versions.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
terraform {
2+
experiments = [module_variable_optional_attrs]
23
required_providers {
34
azurerm = {
45
source = "hashicorp/azurerm"

0 commit comments

Comments
 (0)