There are more AWS SDK examples available in the AWS Doc SDK Examples
ElastiCache examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with ElastiCache.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use add-tags-to-resource.
- AWS CLI
-
To add tags to a resource
The following
add-tags-to-resourceexample adds up to 10 tags, key-value pairs, to a cluster or snapshot resource.aws elasticache add-tags-to-resource \ --resource-name"arn:aws:elasticache:us-east-1:1234567890:cluster:my-mem-cluster"\ --tags '{"20150202":15, "ElastiCache":"Service"}'Output:
{ "TagList": [ { "Value": "20150202", "Key": "APIVersion" }, { "Value": "ElastiCache", "Key": "Service" } ] }For more information, see Monitoring Costs with Cost Allocation Tags in the Elasticache User Guide.
-
For API details, see AddTagsToResource
in AWS CLI Command Reference.
-
The following code example shows how to use authorize-cache-security-group-ingress.
- AWS CLI
-
To authorize cache security group for ingress
The following
authorize-cache-security-group-ingressexample allows network ingress to a cache security group.aws elasticache authorize-cache-security-group-ingress \ --cache-security-group-name"my-sec-grp"\ --ec2-security-group-name"my-ec2-sec-grp"\ --ec2-security-group-owner-id"1234567890"The command produces no output.
For more information, see Self-Service Updates in Amazon ElastiCache in the Elasticache User Guide.
-
For API details, see AuthorizeCacheSecurityGroupIngress
in AWS CLI Command Reference.
-
The following code example shows how to use batch-apply-update-action.
- AWS CLI
-
To apply a service update
The following
batch-apply-update-actionexample applies a service update to a Redis cluster.aws elasticache batch-apply-update-action \ --service-update-nameelc-xxxxx406-xxx\ --replication-group-idstest-clusterOutput:
{ "ProcessedUpdateActions": [ { "ReplicationGroupId": "pat-cluster", "ServiceUpdateName": "elc-xxxxx406-xxx", "UpdateActionStatus": "waiting-to-start" } ], "UnprocessedUpdateActions": [] }For more information, see Self-Service Updates in Amazon ElastiCache in the Elasticache User Guide.
-
For API details, see BatchApplyUpdateAction
in AWS CLI Command Reference.
-
The following code example shows how to use batch-stop-update-action.
- AWS CLI
-
To stop a service update
The following
batch-stop-update-actionexample applies a service update to a Redis cluster.aws elasticache batch-stop-update-action \ --service-update-nameelc-xxxxx406-xxx\ --replication-group-idstest-clusterOutput:
{ "ProcessedUpdateActions": [ { "ReplicationGroupId": "pat-cluster", "ServiceUpdateName": "elc-xxxxx406-xxx", "UpdateActionStatus": "stopping" } ], "UnprocessedUpdateActions": [] }For more information, see Self-Service Updates in Amazon ElastiCache in the Elasticache User Guide.
-
For API details, see BatchStopUpdateAction
in AWS CLI Command Reference.
-
The following code example shows how to use copy-snapshot.
- AWS CLI
-
To copy a snapshot
The following
copy-snapshotexample makes a copy of an existing snapshot.aws elasticache copy-snapshot \ --source-snapshot-name"my-snapshot"\ --target-snapshot-name"my-snapshot-copy"Output:
{ "Snapshot":{ "Engine": "redis", "CacheParameterGroupName": "default.redis3.2", "VpcId": "vpc-3820329f3", "CacheClusterId": "my-redis4", "SnapshotRetentionLimit": 7, "NumCacheNodes": 1, "SnapshotName": "my-snapshot-copy", "CacheClusterCreateTime": "2016-12-21T22:24:04.955Z", "AutoMinorVersionUpgrade": true, "PreferredAvailabilityZone": "us-east-1c", "SnapshotStatus": "creating", "SnapshotSource": "manual", "SnapshotWindow": "07:00-08:00", "EngineVersion": "3.2.4", "NodeSnapshots": [ { "CacheSize": "3 MB", "SnapshotCreateTime": "2016-12-28T07:00:52Z", "CacheNodeId": "0001", "CacheNodeCreateTime": "2016-12-21T22:24:04.955Z" } ], "CacheSubnetGroupName": "default", "Port": 6379, "PreferredMaintenanceWindow": "tue:09:30-tue:10:30", "CacheNodeType": "cache.m3.large" } }For more information, see Exporting a Backup in the Elasticache User Guide.
-
For API details, see CopySnapshot
in AWS CLI Command Reference.
-
The following code example shows how to use create-cache-cluster.
- AWS CLI
-
To create a cache cluster
The following
create-cache-clusterexample creates a cache cluster using the Redis engine.aws elasticache create-cache-cluster \ --cache-cluster-id"cluster-test"\ --engineredis\ --cache-node-typecache.m5.large\ --num-cache-nodes1Output:
{ "CacheCluster": { "CacheClusterId": "cluster-test", "ClientDownloadLandingPage": "https://console.aws.amazon.com/elasticache/home#client-download:", "CacheNodeType": "cache.m5.large", "Engine": "redis", "EngineVersion": "5.0.5", "CacheClusterStatus": "creating", "NumCacheNodes": 1, "PreferredMaintenanceWindow": "sat:13:00-sat:14:00", "PendingModifiedValues": {}, "CacheSecurityGroups": [], "CacheParameterGroup": { "CacheParameterGroupName": "default.redis5.0", "ParameterApplyStatus": "in-sync", "CacheNodeIdsToReboot": [] }, "CacheSubnetGroupName": "default", "AutoMinorVersionUpgrade": true, "SnapshotRetentionLimit": 0, "SnapshotWindow": "06:30-07:30", "TransitEncryptionEnabled": false, "AtRestEncryptionEnabled": false } }For more information, see Creating a Cluster in the Elasticache User Guide.
-
For API details, see CreateCacheCluster
in AWS CLI Command Reference.
-
The following code example shows how to use create-cache-parameter-group.
- AWS CLI
-
To create a cache parameter group
The following
create-cache-parameter-groupexample creates a new Amazon ElastiCache cache parameter group.aws elasticache create-cache-parameter-group \ --cache-parameter-group-family"redis5.0"\ --cache-parameter-group-name"mygroup"\ --description"mygroup"Output:
{ "CacheParameterGroup": { "CacheParameterGroupName": "mygroup", "CacheParameterGroupFamily": "redis5.0", "Description": "my group" } }For more information, see Creating a Parameter Group in the Elasticache User Guide.
-
For API details, see CreateCacheParameterGroup
in AWS CLI Command Reference.
-
The following code example shows how to use create-cache-subnet-group.
- AWS CLI
-
To create a cache subnet group
The following
create-cache-subnet-groupexample creates a new cache subnet group.aws elasticache create-cache-subnet-group \ --cache-subnet-group-name"mygroup"\ --cache-subnet-group-description"my subnet group"\ --subnet-ids"subnet-xxxxec4f"Output:
{ "CacheSubnetGroup": { "CacheSubnetGroupName": "mygroup", "CacheSubnetGroupDescription": "my subnet group", "VpcId": "vpc-a3e97cdb", "Subnets": [ { "SubnetIdentifier": "subnet-xxxxec4f", "SubnetAvailabilityZone": { "Name": "us-west-2d" } } ] } }For more information, see Creating a Cache Subnet Group in the Elasticache User Guide.
-
For API details, see CreateCacheSubnetGroup
in AWS CLI Command Reference.
-
The following code example shows how to use create-global-replication-group.
- AWS CLI
-
To create a global replication group
The following
create-global-replication-groupexample creates a new global replication group.aws elasticache create-global-replication-group \ --global-replication-group-id-suffixmy-global-replication-group\ --primary-replication-group-idmy-primary-clusterOutput:
{ "GlobalReplicationGroup": { "GlobalReplicationGroupId": "sgaui-my-global-replication-group", "GlobalReplicationGroupDescription": " ", "Status": "creating", "CacheNodeType": "cache.r5.large", "Engine": "redis", "EngineVersion": "5.0.6", "Members": [ { "ReplicationGroupId": "my-primary-cluster", "ReplicationGroupRegion": "us-west-2", "Role": "PRIMARY", "AutomaticFailover": "enabled", "Status": "associating" } ], "ClusterEnabled": true, "GlobalNodeGroups": [ { "GlobalNodeGroupId": "sgaui-my-global-replication-group-0001", "Slots": "0-16383" } ], "AuthTokenEnabled": false, "TransitEncryptionEnabled": false, "AtRestEncryptionEnabled": false } }For more information, see Replication Across AWS Regions Using Global Datastore
in the Elasticache User Guide. -
For API details, see CreateGlobalReplicationGroup
in AWS CLI Command Reference.
-
The following code example shows how to use create-replication-group.
- AWS CLI
-
To create a replication group
The following
create-replication-groupexample creates a Redis (cluster mode disabled) or a Redis (cluster mode enabled) replication group. This operation is valid for Redis only.aws elasticache create-replication-group \ --replication-group-id"mygroup"\ --replication-group-description"my group"\ --engine"redis"\ --cache-node-type"cache.m5.large"Output:
{ "ReplicationGroup": { "ReplicationGroupId": "mygroup", "Description": "my group", "Status": "creating", "PendingModifiedValues": {}, "MemberClusters": [ "mygroup-001" ], "AutomaticFailover": "disabled", "SnapshotRetentionLimit": 0, "SnapshotWindow": "06:00-07:00", "ClusterEnabled": false, "CacheNodeType": "cache.m5.large", "TransitEncryptionEnabled": false, "AtRestEncryptionEnabled": false } }For more information, see Creating a Redis Replication Group in the Elasticache User Guide.
-
For API details, see CreateReplicationGroup
in AWS CLI Command Reference.
-
The following code example shows how to use create-snapshot.
- AWS CLI
-
To create a snapshot
The following
create-snapshotexample creates a snapshot using the Redis engine.aws elasticache create-snapshot \ --snapshot-namemysnapshot\ --cache-cluster-idcluster-testOutput:
{ "Snapshot": { "SnapshotName": "mysnapshot", "CacheClusterId": "cluster-test", "SnapshotStatus": "creating", "SnapshotSource": "manual", "CacheNodeType": "cache.m5.large", "Engine": "redis", "EngineVersion": "5.0.5", "NumCacheNodes": 1, "PreferredAvailabilityZone": "us-west-2b", "CacheClusterCreateTime": "2020-03-19T03:12:01.483Z", "PreferredMaintenanceWindow": "sat:13:00-sat:14:00", "Port": 6379, "CacheParameterGroupName": "default.redis5.0", "CacheSubnetGroupName": "default", "VpcId": "vpc-a3e97cdb", "AutoMinorVersionUpgrade": true, "SnapshotRetentionLimit": 0, "SnapshotWindow": "06:30-07:30", "NodeSnapshots": [ { "CacheNodeId": "0001", "CacheSize": "", "CacheNodeCreateTime": "2020-03-19T03:12:01.483Z" } ] } }For more information, see Backup and Restore for ElastiCache for Redis in the Elasticache User Guide.
-
For API details, see CreateSnapshot
in AWS CLI Command Reference.
-
The following code example shows how to use create-user-group.
- AWS CLI
-
To create a user group
The following
create-user-groupexample creates a new user group.aws elasticache create-user-group \ --user-group-idmyusergroup\ --engineredis\ --user-idsdefaultOutput:
{ "UserGroupId": "myusergroup", "Status": "creating", "Engine": "redis", "UserIds": [ "default" ], "ReplicationGroups": [], "ARN": "arn:aws:elasticache:us-west-2:xxxxxxxxxx52:usergroup:myusergroup" }For more information, see Authenticating Users with Role-Based Access Control (RBAC) in the Elasticache User Guide.
-
For API details, see CreateUserGroup
in AWS CLI Command Reference.
-
The following code example shows how to use create-user.
- AWS CLI
-
To create a user
The following
create-userexample creates a new user.aws elasticache create-user \ --user-iduser1\ --user-namemyUser\ --passwordsmYnuUzrpAxXw2rdzx\ --engineredis\ --access-string"on ~app::* -@all +@read"Output:
{ "UserId": "user2", "UserName": "myUser", "Status": "active", "Engine": "redis", "AccessString": "on ~app::* -@all +@read +@hash +@bitmap +@geo -setbit -bitfield -hset -hsetnx -hmset -hincrby -hincrbyfloat -hdel -bitop -geoadd -georadius -georadiusbymember", "UserGroupIds": [], "Authentication": { "Type": "password", "PasswordCount": 1 }, "ARN": "arn:aws:elasticache:us-west-2:xxxxxxxxxx52:user:user2" }For more information, see Authenticating Users with Role-Based Access Control (RBAC) in the Elasticache User Guide.
-
For API details, see CreateUser
in AWS CLI Command Reference.
-
The following code example shows how to use decrease-node-groups-in-global-replication-group.
- AWS CLI
-
To decrease the number of node groups in a global replication group
The following
decrease-node-groups-in-global-replication-groupdecreases the node group count using the Redis engine.aws elasticache decrease-node-groups-in-global-replication-group \ --global-replication-group-idsgaui-test\ --node-group-count1\ --apply-immediately \ --global-node-groups-to-retainsgaui-test-0003Output:
{ "GlobalReplicationGroup": { "GlobalReplicationGroupId": "sgaui-test", "GlobalReplicationGroupDescription": "test", "Status": "modifying", "CacheNodeType": "cache.r5.large", "Engine": "redis", "EngineVersion": "5.0.6", "Members": [ { "ReplicationGroupId": "test-2", "ReplicationGroupRegion": "us-east-1", "Role": "SECONDARY", "AutomaticFailover": "enabled", "Status": "associated" }, { "ReplicationGroupId": "test-1", "ReplicationGroupRegion": "us-west-2", "Role": "PRIMARY", "AutomaticFailover": "enabled", "Status": "associated" } ], "ClusterEnabled": true, "GlobalNodeGroups": [ { "GlobalNodeGroupId": "sgaui-test-0001", "Slots": "0-449,1816-5461" }, { "GlobalNodeGroupId": "sgaui-test-0002", "Slots": "6827-10922" }, { "GlobalNodeGroupId": "sgaui-test-0003", "Slots": "10923-14052,15418-16383" }, { "GlobalNodeGroupId": "sgaui-test-0004", "Slots": "450-1815,5462-6826,14053-15417" } ], "AuthTokenEnabled": false, "TransitEncryptionEnabled": false, "AtRestEncryptionEnabled": false } }For more information, see Replication Across AWS Regions Using Global Datastore in the Elasticache User Guide.
-
For API details, see DecreaseNodeGroupsInGlobalReplicationGroup
in AWS CLI Command Reference.
-
The following code example shows how to use decrease-replica-count.
- AWS CLI
-
To decrease replica count
The following
decrease-replica-countexample dynamically decreases the number of replicas in a Redis (cluster mode disabled) replication group or the number of replica nodes in one or more node groups (shards) of a Redis (cluster mode enabled) replication group. This operation is performed with no cluster downtime.aws elasticache decrease-replica-count \ --replication-group-idmy-cluster\ --apply-immediately \ --new-replica-count2Output:
{ "ReplicationGroup": { "ReplicationGroupId": "my-cluster", "Description": " ", "Status": "modifying", "PendingModifiedValues": {}, "MemberClusters": [ "myrepliace", "my-cluster-001", "my-cluster-002", "my-cluster-003" ], "NodeGroups": [ { "NodeGroupId": "0001", "Status": "modifying", "PrimaryEndpoint": { "Address": "my-cluster.xxxxx.ng.0001.usw2.cache.amazonaws.com", "Port": 6379 }, "ReaderEndpoint": { "Address": "my-cluster-ro.xxxxx.ng.0001.usw2.cache.amazonaws.com", "Port": 6379 }, "NodeGroupMembers": [ { "CacheClusterId": "myrepliace", "CacheNodeId": "0001", "ReadEndpoint": { "Address": "myrepliace.xxxxx.0001.usw2.cache.amazonaws.com", "Port": 6379 }, "PreferredAvailabilityZone": "us-west-2a", "CurrentRole": "replica" }, { "CacheClusterId": "my-cluster-001", "CacheNodeId": "0001", "ReadEndpoint": { "Address": "my-cluster-001.xxxxx.0001.usw2.cache.amazonaws.com", "Port": 6379 }, "PreferredAvailabilityZone": "us-west-2a", "CurrentRole": "primary" }, { "CacheClusterId": "my-cluster-002", "CacheNodeId": "0001", "ReadEndpoint": { "Address": "my-cluster-002.xxxxx.0001.usw2.cache.amazonaws.com", "Port": 6379 }, "PreferredAvailabilityZone": "us-west-2a", "CurrentRole": "replica" }, { "CacheClusterId": "my-cluster-003", "CacheNodeId": "0001", "ReadEndpoint": { "Address": "my-cluster-003.xxxxx.0001.usw2.cache.amazonaws.com", "Port": 6379 }, "PreferredAvailabilityZone": "us-west-2a", "CurrentRole": "replica" } ] } ], "AutomaticFailover": "disabled", "SnapshotRetentionLimit": 0, "SnapshotWindow": "07:30-08:30", "ClusterEnabled": false, "CacheNodeType": "cache.r5.xlarge", "TransitEncryptionEnabled": false, "AtRestEncryptionEnabled": false } }For more information, see Changing the Number of Replicas in the Elasticache User Guide.
-
For API details, see DecreaseReplicaCount
in AWS CLI Command Reference.
-
The following code example shows how to use delete-cache-cluster.
- AWS CLI
-
To delete a cache cluster
The following
delete-cache-clusterexample deletes the specified previously provisioned cluster. The command deletes all associated cache nodes, node endpoints. and the cluster itself. When you receive a successful response from this operation, Amazon ElastiCache immediately begins deleting the cluster; you can't cancel or revert this operation.This operation is not valid for the following:
Redis (cluster mode enabled) clustersA cluster that is the last read replica of a replication groupA node group (shard) that has Multi-AZ mode enabledA cluster from a Redis (cluster mode enabled) replication groupA cluster that is not in the available state
aws elasticache delete-cache-cluster \ --cache-cluster-id "my-cluster-002"Output:
{ "CacheCluster": { "CacheClusterId": "my-cluster-002", "ClientDownloadLandingPage": "https://console.aws.amazon.com/elasticache/home#client-download:", "CacheNodeType": "cache.r5.xlarge", "Engine": "redis", "EngineVersion": "5.0.5", "CacheClusterStatus": "deleting", "NumCacheNodes": 1, "PreferredAvailabilityZone": "us-west-2a", "CacheClusterCreateTime": "2019-11-26T03:35:04.546Z", "PreferredMaintenanceWindow": "mon:04:05-mon:05:05", "PendingModifiedValues": {}, "NotificationConfiguration": { "TopicArn": "arn:aws:sns:us-west-x:xxxxxxx4152:My_Topic", "TopicStatus": "active" }, "CacheSecurityGroups": [], "CacheParameterGroup": { "CacheParameterGroupName": "mygroup", "ParameterApplyStatus": "in-sync", "CacheNodeIdsToReboot": [] }, "CacheSubnetGroupName": "kxkxk", "AutoMinorVersionUpgrade": true, "SecurityGroups": [ { "SecurityGroupId": "sg-xxxxxxxxxx9836", "Status": "active" }, { "SecurityGroupId": "sg-xxxxxxxxxxxx7b", "Status": "active" } ], "ReplicationGroupId": "my-cluster", "SnapshotRetentionLimit": 0, "SnapshotWindow": "07:30-08:30", "TransitEncryptionEnabled": false, "AtRestEncryptionEnabled": false } }For more information, see Deleting a Cluster in the Elasticache User Guide.
-
For API details, see DeleteCacheCluster
in AWS CLI Command Reference.
-
The following code example shows how to use delete-cache-parameter-group.
- AWS CLI
-
To delete a cache parameter group
The following
delete-cache-parameter-groupexample deletes the specified cache parameter group. You can't delete a cache parameter group if it's associated with any cache clusters.aws elasticache delete-cache-parameter-group \ --cache-parameter-group-namemyparamgroupThis command produces no output.
For more information, see Deleting a Parameter Group in the Elasticache User Guide.
-
For API details, see
-