Restoring snapshot to new EBS volume in Terraform - amazon-web-services

I'm looking at creating a disaster recovery script on AWS for restoring servers.
I've written the Terraform script to create a new EC2 instance, but I'd like to restore a snapshot from the old EBS volume to a new one and then attach that to the new EC2 instance.
I've been looking through Terraform though and can't see a way to restore a snapshot to a new volume, is it possible?

Related

Having trouble creating a backup and restore of an AWS instance in the console

I have a bitnami instance working fine on my AWS console. I want to make a backup of it so that if I mess something up I can simply deploy an older backup.
I followed the instructions here:
https://docs.bitnami.com/aws/faq/administration/backup-restore-server/
So I have the snapshot created. But how do I get a bitnami instance running based off of that snapshop?
That page says:
This feature creates a new snapshot of the disk, which can later be used to restore the server to an earlier state.
To restore the snapshot, you can use "Create Volume" to create a new Amazon EBS Volume from the snapshot. You could then restore the system to the state of the snapshot with these steps:
Stop the instance
Detach the current EBS volume
Attach the new (restored) EBS volume with the same device identifier
Start the instance
However, if you wish to launch a totally new instance using the backup, then the easier backup method would be:
Use "Create Image" in the EC2 console to create an Amazon Machine Image (AMI)
Launch a new instance using that AMI

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.

AWS Snapshots not saving data

I created several snapshots from volumes of my instances months ago. Today I had to restore an instance based on a snapshot, but this instance does not contain any of my data, it is a clean version of Ubuntu, exactly as the first time I created the instance.
How do I restore my data?
thanks
In AWS there are two snapshot options:
1- Snapshot: You can use snapshots to capture a point-in-time state of your individual volumes.
2- Amazon Machine Image (AMI): You can use an AMI to capture a point-in-time state of your whole EC2 instance (VM), including all of its volumes.
So, difference is that snapshot is on individual volume level and AMI is the entire instance. Under the hood, AMI takes snapshots of all attached volumes and you can see those snapshots in the corresponding Snapshots page in AWS Management Console.
So to be clear, did you create an AMI from you Ubuntu instance and restored the same AMI? Or did you create a snapshot from one or more of its volumes? In that case, how did you restore it?
If you only take a snapshot of one or more of the volumes, after you restore your snapshots into new volumes attached to an instance, you should also remember to mount those volumes. But if you use an AMI, mounting may not be required, as long as you have added your volumes in the /etc/fstab.

Does new instance from AMI generated from EBS snapshots require initialization

I understand that EBS volumes that are restored from snapshots need to be initialized.
Let's say I create an AMI from an EBS snapshot(s).
When I launch a new instance using that AMI, will I need to initialize each volume before I get consistent latency?
When you create an instance from AMI which has EBS based volumes as storage, your instance creates new volume from this volume and continues with launch.
When you use snapshots as source to launch your instance, AWS need to pull these snapshots and write it to volume and then continue with the launch. In this case, pull storage blocks from S3 and write to volume will be the latency some users want to avoid. This is the case where pre-warming is suggested by AWS.

AWS how to find Snapshot associated with AMI?

I am trying to find a AWS Snapshot associated with AMI.
I am cleaning up my AMI list. After deregistering 2 AMI I have checked snapshot count its not reducing any reason behind this?
How can delete snapshot associated with AMI automatically when I am deleting AMI?
When you deregister an Amazon EBS-backed AMI, it doesn't affect the snapshot that was created for the root volume of the instance during the AMI creation process. You'll continue to incur storage costs for this snapshot. Therefore, if you are finished with the snapshot, you should delete it.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/deregister-ami.html
And for writing some automation script, which can auto delete snapshots when you are deleting the AMI, you can have a look on following:
https://serverfault.com/questions/611831/find-all-snapshots-created-by-ami-where-ami-is-deleted
https://www.yobyot.com/aws/deregister-an-aws-ami-and-remove-associated-s3-snapshots/2014/10/30/
http://www.n2ws.com/how-to-guides/how-to-delete-unutilized-ebs-based-amis-and-corresponding-snapshots.html