ElastiCache does not support direct stopping and starting of clusters. Instead, the best way to minimize the cost of ElastiCache when it’s not in use is to scale down the cluster to the minimum configuration or to take snapshots of your data, delete the cluster, and recreate it when needed.
Scaling Down the Cluster
You can scale down the ElastiCache cluster to the minimum configuration to reduce costs. This involves reducing the number of nodes and changing the instance type to a smaller, less expensive one.
AWS CLI Commands
aws elasticache modify-cache-cluster --cache-cluster-id your-cache-cluster-id --num-cache-nodes 1 --cache-node-type cache.t2.micro --apply-immediately
Script for Scaling Down and Restoring ElastiCache Cluster
#!/bin/bash | |
# Define your ElastiCache cluster identifier and configuration | |
CACHE_CLUSTER_ID="your-cache-cluster-id" | |
SNAPSHOT_NAME="your-snapshot-name" | |
CACHE_NODE_TYPE="cache.t2.micro" # Example node type | |
NUM_CACHE_NODES=1 | |
# Function to scale down the ElastiCache cluster | |
scale_down_elasticache() { | |
echo "Scaling down ElastiCache cluster: $CACHE_CLUSTER_ID" | |
aws elasticache modify-cache-cluster --cache-cluster-id $CACHE_CLUSTER_ID --num-cache-nodes $NUM_CACHE_NODES --cache-node-type $CACHE_NODE_TYPE --apply-immediately | |
if [ $? -eq 0 ]; then | |
echo "ElastiCache cluster scaled down successfully." | |
else | |
echo "Failed to scale down ElastiCache cluster." | |
fi | |
} | |
# Function to create a snapshot and delete the ElastiCache cluster | |
snapshot_and_delete_elasticache() { | |
echo "Creating snapshot for ElastiCache cluster: $CACHE_CLUSTER_ID" | |
aws elasticache create-snapshot --cache-cluster-id $CACHE_CLUSTER_ID --snapshot-name $SNAPSHOT_NAME | |
if [ $? -eq 0 ]; then | |
echo "Snapshot created successfully. Deleting ElastiCache cluster: $CACHE_CLUSTER_ID" | |
aws elasticache delete-cache-cluster --cache-cluster-id $CACHE_CLUSTER_ID | |
if [ $? -eq 0 ]; then | |
echo "ElastiCache cluster deleted successfully." | |
else | |
echo "Failed to delete ElastiCache cluster." | |
fi | |
else | |
echo "Failed to create snapshot." | |
fi | |
} | |
# Function to restore the ElastiCache cluster from a snapshot | |
restore_elasticache() { | |
echo "Restoring ElastiCache cluster: $CACHE_CLUSTER_ID from snapshot: $SNAPSHOT_NAME" | |
aws elasticache create-cache-cluster --cache-cluster-id $CACHE_CLUSTER_ID --snapshot-name $SNAPSHOT_NAME --cache-node-type $CACHE_NODE_TYPE --num-cache-nodes $NUM_CACHE_NODES | |
if [ $? -eq 0 ]; then | |
echo "ElastiCache cluster restored successfully." | |
else | |
echo "Failed to restore ElastiCache cluster." | |
fi | |
} | |
# Main script logic | |
case "$1" in | |
scale-down) | |
scale_down_elasticache | |
;; | |
snapshot-and-delete) | |
snapshot_and_delete_elasticache | |
;; | |
restore) | |
restore_elasticache | |
;; | |
*) | |
echo "Usage: $0 {scale-down|snapshot-and-delete|restore}" | |
exit 1 | |
;; | |
esac |
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND