Skip to content

Commit

Permalink
Chore: Use AWS provider elasticache serverless resource
Browse files Browse the repository at this point in the history
  • Loading branch information
hazmei committed Jan 12, 2024
1 parent ea1db15 commit d64f575
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
16 changes: 8 additions & 8 deletions alarms.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" {
resource "aws_cloudwatch_metric_alarm" "cache_serverless_ecpu" {
count = var.enabled && var.use_serverless ? 1 : 0

alarm_name = "${awscc_elasticache_serverless_cache.this[0].serverless_cache_name}-ecpu-utilization"
alarm_name = "${aws_elasticache_serverless_cache.this[0].id}-ecpu-utilization"
alarm_description = "Redis serverless ECPU utilization"

comparison_operator = "GreaterThanThreshold"
Expand All @@ -81,21 +81,21 @@ resource "aws_cloudwatch_metric_alarm" "cache_serverless_ecpu" {
threshold = ceil(var.max_ecpu_per_second * var.alarm_ecpu_threshold_percent / 100)

dimensions = {
CacheClusterId = awscc_elasticache_serverless_cache.this[0].serverless_cache_name
CacheClusterId = aws_elasticache_serverless_cache.this[0].id
}

alarm_actions = var.alarm_actions
ok_actions = var.ok_actions

depends_on = [
awscc_elasticache_serverless_cache.this
aws_elasticache_serverless_cache.this
]
}

resource "aws_cloudwatch_metric_alarm" "cache_serverless_data" {
count = var.enabled && var.use_serverless ? 1 : 0

alarm_name = "${awscc_elasticache_serverless_cache.this[0].serverless_cache_name}-data-storage"
alarm_name = "${aws_elasticache_serverless_cache.this[0].id}-data-storage"
alarm_description = "Redis serverless data storage"

comparison_operator = "GreaterThanThreshold"
Expand All @@ -112,7 +112,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_serverless_data" {
tags = var.tags

dimensions = {
CacheClusterId = awscc_elasticache_serverless_cache.this[0].serverless_cache_name
CacheClusterId = aws_elasticache_serverless_cache.this[0].id
}

alarm_actions = var.alarm_actions
Expand All @@ -126,7 +126,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_serverless_data" {
resource "aws_cloudwatch_metric_alarm" "cache_serverless_throttled_commands" {
count = var.enabled && var.use_serverless ? 1 : 0

alarm_name = "${awscc_elasticache_serverless_cache.this[0].serverless_cache_name}-throttled-commands"
alarm_name = "${aws_elasticache_serverless_cache.this[0].id}-throttled-commands"
alarm_description = "Redis serverless throttled commands"

comparison_operator = "GreaterThanThreshold"
Expand All @@ -142,13 +142,13 @@ resource "aws_cloudwatch_metric_alarm" "cache_serverless_throttled_commands" {

tags = var.tags
dimensions = {
CacheClusterId = awscc_elasticache_serverless_cache.this[0].serverless_cache_name
CacheClusterId = aws_elasticache_serverless_cache.this[0].id
}

alarm_actions = var.alarm_actions
ok_actions = var.ok_actions

depends_on = [
awscc_elasticache_serverless_cache.this
aws_elasticache_serverless_cache.this
]
}
35 changes: 16 additions & 19 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,36 @@ resource "aws_elasticache_replication_group" "this" {
tags = var.tags
}

resource "awscc_elasticache_serverless_cache" "this" {
moved {
from = awscc_elasticache_serverless_cache.this
to = aws_elasticache_serverless_cache.this
}

resource "aws_elasticache_serverless_cache" "this" {
count = var.enabled && var.use_serverless ? 1 : 0

serverless_cache_name = var.name
description = "${var.name} ElastiCache Redis Serverless"
engine = "redis"
major_engine_version = var.engine_version
name = var.name
description = "${var.name} ElastiCache Redis Serverless"
engine = "redis"
major_engine_version = var.engine_version

cache_usage_limits = {
data_storage = {
cache_usage_limits {
data_storage {
maximum = var.max_data_storage
unit = "GB"
}
ecpu_per_second = {
ecpu_per_second {
maximum = var.max_ecpu_per_second
}
}

user_group_id = var.user_group_id

final_snapshot_name = "${var.name}-elasticache-serverless-final-snapshot"
kms_key_id = var.kms_key_id
security_group_ids = var.security_groups
subnet_ids = var.subnets
kms_key_id = var.kms_key_id
security_group_ids = var.security_groups
subnet_ids = var.subnets

daily_snapshot_time = var.daily_snapshot_time
snapshot_arns_to_restore = var.snapshot_arns_to_restore
snapshot_retention_limit = var.snapshot_retention_limit

tags = [
for key, value in var.tags : {
key = key
value = value
}
]
}
4 changes: 2 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
output "endpoint" {
description = "Redis primary or configuration endpoint, whichever is appropriate for the given cluster mode"
value = var.use_serverless ? try(awscc_elasticache_serverless_cache.this[0].endpoint.address, null) : try(aws_elasticache_replication_group.this[0].primary_endpoint_address, null)
value = var.use_serverless ? try(aws_elasticache_serverless_cache.this[0].endpoint, null) : try(aws_elasticache_replication_group.this[0].primary_endpoint_address, null)
}

output "reader_endpoint_address" {
Expand All @@ -15,7 +15,7 @@ output "member_clusters" {

output "arn" {
description = "Elasticache Replication Group ARN"
value = var.use_serverless ? try(awscc_elasticache_serverless_cache.this[0].arn, null) : try(aws_elasticache_replication_group.this[0].arn, null)
value = var.use_serverless ? try(aws_elasticache_serverless_cache.this[0].arn, null) : try(aws_elasticache_replication_group.this[0].arn, null)
}

output "cluster_enabled" {
Expand Down
6 changes: 1 addition & 5 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.0"
}
awscc = {
source = "hashicorp/awscc"
version = ">= 0.67.0"
version = ">= 5.32"
}
}
}

0 comments on commit d64f575

Please sign in to comment.