Re-creating RDS instance from snapshot with Cloudformation - amazon-web-services

I would like to create a RDS instance with Cloudformation and keep a snapshot when the stack is deleted. According to AWS I can do that by setting DeletionPolicy: Snapshot in my Cloudformation template.
When I create a stack from the same template I want to use the snapshot. I can do that with DBSnapshotIdentifier.
What does a single Cloudformation template look like that:
works when the RDS instance is created for the very first time and there no snapshot available?
updates the DBSnapshotIdentifier every time the stack gets deleted?
Thank you!

Related

How to manage a RDS Database including recovery from snapshots in CDK

I am managing an RDS Database in an CDK Project. From time to time it is necasary to recover an old state of the database from snapshot.
To make this possible, I have CDK Context Parameter specifying the Snapshot from which the Database should be recovered. If it is given, the snapshot is bassed to the RDS Construct. This leads to a replacement of the database (which is fine).
Now, if the database has been recovered from snapshot and I do some other update in the CDK Project and want to update the project (which is done via CD Pipeline) I usally start the update without giving any snapshot. This leads to the database to be replaced by an empty one, which is of course not what I want.
To summarize, this is how it currently works:
Current state of the stack
Deployment Paramter
Result
Not created from snapshot
No snapshot
DB is not replaced
Not created from snapshot
snapshot
DB is replaced
created from snapshot
No snapshot
DB is replaced
created from snapshot
snapshot (same)
DB is not replaced
created from snapshot
snapshot (different)
DB is replaced
This is IMHO not managable.
But what I want is something like this:
Current state of the stack
Deployment Paramter
Result
Not created from snapshot
No snapshot
DB is not replaced
Not created from snapshot
snapshot
DB is replaced
created from snapshot
No snapshot
DB is not replaced
created from snapshot
snapshot (same)
DB is replaced
created from snapshot
snapshot (different)
DB is replaced
Or, to put it in other words:
If I specify no snapshot, don't replace the DB
If I specify a snapshot, repalce the DB
Is this possible? Or is there another good way to manage an RDS Database using CDK?

CloudFormation / CDK: how to replace an EC2 instance and keep EBS storage?

Using AWS CDK, an EC2 instance with attached EBS can be created like this:
BlockDevice durableStorage = BlockDevice.builder()
.deviceName("/dev/sdf")
.volume(BlockDeviceVolume.ebs(
DURABLE_STORAGE_GB,
EbsDeviceOptions.builder()
.deleteOnTermination(false)
.encrypted(true)
.volumeType(EbsDeviceVolumeType.GP2)
.build()))
.build();
Instance instance = new Instance(
this,
"MyInstance",
InstanceProps.builder()
.blockDevices(List.of(durableStorage))
// more config here
.build());
If there is an update to the stack that involves a replacement to the EC2 instance, how is the EBS attachment managed? The old instance is kept until the new one has been created and only then is it destroyed, so how should one manage the transfer of the EBS volume to the new server? Is would this be managed in CloudFormation?
so how should one manage the transfer of the EBS volume to the new server? Is would this be managed in CloudFormation?
Its not managed. The update which requires replacement of instance (e.g. AMI id change) will fail with the following error message:
Update to resource type AWS::EC2::VolumeAttachment is not supported.
One way to deal with this is to do update in stages. First, you remove the attachment in your template (just comment it out) and update the stack to dissociate instance with volume. Then you do replacement update of your instance. Finally, you uncomment the attachment and update again. This result in reattachment of the volume to new instance.
p.s.
I wrote this answer based on my quick experiments for this specific scenario in CloudFormation that I did for this question. Maybe there is better way, I don't know at present.

AWS RDS : Delete Protection or Snapshot on instance level through cloud formation

I have enabled DeleteProtection and DeletionPolicy Snapshot but from Cloud formation it only possible from cluster level not instance level.
My problem if any how any instance of the cluster will removed how can I get back all its tables, data back if I am not able to take snapshot of instance level from cloud formation. And DeleteProtection also not possible in instance level as well.
Any help will really appreciable..
For DeletionPolicy Snapshot got below error
DeletionPolicy:Snapshot cannot be specified for a cluster instance, use deletion policy on the cluster instead."

AWS Cloudformation stack deletion after EC2 UserData has finished execution

I need to automatically delete a cloudformation stack after the EC2 instance created using that stack has finished running its UserData. I have tried to run deletion from the ec2 instance but it gives me permission error as the ec2 instance itself is deleted before deleting the whole stack.
I quite don't understand what you want in your question. But in general, if you delete the CloudFormation stack, all the resources created by that stack will also be deleted. All your resources, including EC2, will be deleted first before you can see successful deletion of your CloudFormation stack.

Delete previous snapshots and create new snapshot of EBS volume using Terraform

I need to create a snapshot of EBS volume using Terraform. I also have to consider that if the EBS volume has previously snapshots or not. If the EBS volume contains some previous snapshots then i need to delete them from my Terraform code and create a new snapshot of EBS volume.
I am not sure if deletion of resources is possible through Terraform code. If it is possible, how can i delete the previous snapshots and create new snapshot of EBS volume with Terraform.
No it is not possible. However if the previous snapshots were created by terraform, than running terraform destroy before applying the latest version would do the job. But if those snapshots were created by other means than terraform, deleting them using terraform isn't possible at all.