Amazon EC2 – Persist Data for PostgreSQL Instance Using Terraform

amazon ec2postgresqlterraform

I am provisioning a cheap postgres instance for myself by installing directly on amazon ec2.
Everything will be deployed using terraform.
I would like the database data to persist between restarts / teardown / redeploy of infra.

What is the conventional way to achieve this?

  • (a) define an amazon EBS resource separately, so that terraform teardown does not destroy it, and always provide its arn as a mounted storage attachment to the ec2 instance?
  • (b) define amazon EBS resource with terraform, but set a final_snapshot = true and also set a snapshot_id so that on terraform destroy, the data will be backed up somewhere. If so, how do I reincorporate this data in the next ec2 instance?
  • (c) something else I don't know about / haven't thought of?

Any tips appreciated.

Best Answer

You can provision the EBS resource out of band, fetch (or store) the volume id and associate with it. Or you can also use EFS the same way, using user-data to mount EFS on boot

Related Question