Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/aws elasticache cluster final snapshot #15592

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions aws/resource_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ func resourceAwsElasticacheCluster() *schema.Resource {
Computed: true,
ForceNew: true,
},
"final_snapshot_identifier": {
Type: schema.TypeString,
Optional: true,
},
"tags": tagsSchema(),
},

Expand Down Expand Up @@ -667,7 +671,8 @@ func (b byCacheNodeId) Less(i, j int) bool {
func resourceAwsElasticacheClusterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn

err := deleteElasticacheCacheCluster(conn, d.Id())
var finalSnapshotID = d.Get("final_snapshot_identifier").(string)
err := deleteElasticacheCacheCluster(conn, d.Id(), finalSnapshotID)
if err != nil {
if isAWSErr(err, elasticache.ErrCodeCacheClusterNotFoundFault, "") {
return nil
Expand Down Expand Up @@ -784,10 +789,12 @@ func waitForCreateElasticacheCacheCluster(conn *elasticache.ElastiCache, cacheCl
return err
}

func deleteElasticacheCacheCluster(conn *elasticache.ElastiCache, cacheClusterID string) error {
func deleteElasticacheCacheCluster(conn *elasticache.ElastiCache, cacheClusterID string, finalSnapshotID string) error {
input := &elasticache.DeleteCacheClusterInput{
CacheClusterId: aws.String(cacheClusterID),
}
input.FinalSnapshotIdentifier = aws.String(finalSnapshotID)

log.Printf("[DEBUG] Deleting Elasticache Cache Cluster: %s", input)
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteCacheCluster(input)
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_elasticache_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func testSweepElasticacheClusters(region string) error {
id := aws.StringValue(cluster.CacheClusterId)

log.Printf("[INFO] Deleting Elasticache Cluster: %s", id)
err := deleteElasticacheCacheCluster(conn, id)
err := deleteElasticacheCacheCluster(conn, id, "")
if err != nil {
log.Printf("[ERROR] Failed to delete Elasticache Cache Cluster (%s): %s", id, err)
}
Expand Down
16 changes: 12 additions & 4 deletions aws/resource_aws_elasticache_replication_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func resourceAwsElasticacheReplicationGroup() *schema.Resource {
ForceNew: true,
Optional: true,
},
"final_snapshot_identifier": {
Type: schema.TypeString,
Optional: true,
},
},
SchemaVersion: 1,

Expand Down Expand Up @@ -606,7 +610,8 @@ func resourceAwsElasticacheReplicationGroupUpdate(d *schema.ResourceData, meta i

// Kick off all the Cache Cluster deletions
for _, cacheClusterID := range removeClusterIDs {
err := deleteElasticacheCacheCluster(conn, cacheClusterID)
var finalSnapshotID = d.Get("final_snapshot_identifier").(string)
err := deleteElasticacheCacheCluster(conn, cacheClusterID, finalSnapshotID)
if err != nil {
// Future enhancement: we could retry deletion with random existing ID on missing name
// if isAWSErr(err, elasticache.ErrCodeCacheClusterNotFoundFault, "") { ... }
Expand Down Expand Up @@ -690,7 +695,8 @@ func resourceAwsElasticacheReplicationGroupUpdate(d *schema.ResourceData, meta i
}

// Finally retry deleting the cache cluster
err = deleteElasticacheCacheCluster(conn, cacheClusterID)
var finalSnapshotID = d.Get("final_snapshot_identifier").(string)
err = deleteElasticacheCacheCluster(conn, cacheClusterID, finalSnapshotID)
if err != nil {
return fmt.Errorf("error deleting Elasticache Cache Cluster (%s) (removing replica after setting new primary): %w", cacheClusterID, err)
}
Expand Down Expand Up @@ -835,7 +841,8 @@ func resourceAwsElasticacheReplicationGroupUpdate(d *schema.ResourceData, meta i
func resourceAwsElasticacheReplicationGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn

err := deleteElasticacheReplicationGroup(d.Id(), conn)
var finalSnapshotID = d.Get("final_snapshot_identifier").(string)
err := deleteElasticacheReplicationGroup(d.Id(), conn, finalSnapshotID)
if err != nil {
return fmt.Errorf("error deleting Elasticache Replication Group (%s): %w", d.Id(), err)
}
Expand Down Expand Up @@ -891,10 +898,11 @@ func cacheReplicationGroupStateRefreshFunc(conn *elasticache.ElastiCache, replic
}
}

func deleteElasticacheReplicationGroup(replicationGroupID string, conn *elasticache.ElastiCache) error {
func deleteElasticacheReplicationGroup(replicationGroupID string, conn *elasticache.ElastiCache, finalSnapshotID string) error {
input := &elasticache.DeleteReplicationGroupInput{
ReplicationGroupId: aws.String(replicationGroupID),
}
input.FinalSnapshotIdentifier = aws.String(finalSnapshotID)

// 10 minutes should give any creating/deleting cache clusters or snapshots time to complete
err := resource.Retry(10*time.Minute, func() *resource.RetryError {
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_elasticache_replication_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func testSweepElasticacheReplicationGroups(region string) error {
id := aws.StringValue(replicationGroup.ReplicationGroupId)

log.Printf("[INFO] Deleting Elasticache Replication Group: %s", id)
err := deleteElasticacheReplicationGroup(id, conn)
err := deleteElasticacheReplicationGroup(id, conn, "")
if err != nil {
log.Printf("[ERROR] Failed to delete Elasticache Replication Group (%s): %s", id, err)
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/elasticache_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ SNS topic to send ElastiCache notifications to. Example:

* `preferred_availability_zones` - (Optional, Memcached only) A list of the Availability Zones in which cache nodes are created. If you are creating your cluster in an Amazon VPC you can only locate nodes in Availability Zones that are associated with the subnets in the selected subnet group. The number of Availability Zones listed must equal the value of `num_cache_nodes`. If you want all the nodes in the same Availability Zone, use `availability_zone` instead, or repeat the Availability Zone multiple times in the list. Default: System chosen Availability Zones. Detecting drift of existing node availability zone is not currently supported. Updating this argument by itself to migrate existing node availability zones is not currently supported and will show a perpetual difference.

* `final_snapshot_identifier` - (Optional) The name of your final cluster snapshot. If omitted, no final snapshot will be made.

* `tags` - (Optional) A map of tags to assign to the resource

## Attributes Reference
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/elasticache_replication_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ before being deleted. If the value of SnapshotRetentionLimit is set to zero (0),
Please note that setting a `snapshot_retention_limit` is not supported on cache.t1.micro or cache.t2.* cache nodes
* `apply_immediately` - (Optional) Specifies whether any modifications are applied immediately, or during the next maintenance window. Default is `false`.
* `tags` - (Optional) A map of tags to assign to the resource. Adding tags to this resource will add or overwrite any existing tags on the clusters in the replication group and not to the group itself.
* `final_snapshot_identifier` - (Optional) The name of your final node group (shard) snapshot. ElastiCache creates the snapshot from the primary node in the cluster. If omitted, no final snapshot will be made.
* `cluster_mode` - (Optional) Create a native redis cluster. `automatic_failover_enabled` must be set to true. Cluster Mode documented below. Only 1 `cluster_mode` block is allowed.

Cluster Mode (`cluster_mode`) supports the following:
Expand Down