hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.62k stars 9k forks source link

elasticache redis: autoscaling #20917

Open pimperator opened 2 years ago

pimperator commented 2 years ago

Community Note

Description

Since AWS announced autoscaling on elasticache-redis instances here on Aug 20, 2021 it would be awesome if we could profit from in on terraform

New or Affected Resource(s)

Potential Terraform Configuration

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key.

References

JustinRainwater commented 1 year ago

In case it isn't obvious, I would like to make sure the aws_elasticache_replication_group resource is included in this request.

wfgamal commented 1 year ago

hey, any updates on this issue? and if not can AWS auto scaling be used in case of Redis cluster mode on to auto scale the cluster? does terraform support that somehow?

buddyledungarees commented 7 months ago

You can attach an autoscalingpolicy via the App Autoscaling resources.

example code:

locals {
  redis_cluster = "redis-cluster-name"
}

resource "aws_appautoscaling_target" "redis_cluster_target" {
  # replication-group is constant and the unique identifier is the replication group name
  resource_id        = "replication-group/${local.redis_cluster}"
  service_namespace  = "elasticache"

  # this scales the number of shards
  scalable_dimension = "elasticache:replication-group:NodeGroups"
  # alternate scaling to add read replicas to each shard of a clustered mode redis cluster
  #scalable_dimension = "elasticache:replication-group:Replicas"

  max_capacity       = 4
  min_capacity       = 1
}

resource "aws_appautoscaling_policy" "redis_shards" {
  name               = "redis-shard-scaling"
  service_namespace  = aws_appautoscaling_target.redis_cluster_target.service_namespace
  scalable_dimension = aws_appautoscaling_target.redis_cluster_target.scalable_dimension
  resource_id        = aws_appautoscaling_target.redis_cluster_target.resource_id

  policy_type        = "TargetTrackingScaling"
  target_tracking_scaling_policy_configuration {
    predefined_metric_specification {
      # see https://docs.aws.amazon.com/autoscaling/application/userguide/monitor-cloudwatch-metrics.html#predefined-metrics
      predefined_metric_type = "ElastiCacheDatabaseMemoryUsageCountedForEvictPercentage"
    }
    disable_scale_in   = false
    target_value       = 75   #percentage
    scale_in_cooldown  = 900  #secs
    scale_out_cooldown = 60   #secs
  }
}