Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconattach-eni
file-type-iconbastion-host
file-type-iconec2-backup
file-type-iconpersistent-ebs-volume
file-type-iconpacker
file-type-iconuser-data
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-iconroute53-helpers
file-type-iconmodules
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
file-type-iconcore-concepts.md
file-type-iconterraform-cloud-enterprise-private-module-...

Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconattach-eni
file-type-iconbastion-host
file-type-iconec2-backup
file-type-iconpersistent-ebs-volume
file-type-iconpacker
file-type-iconuser-data
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-iconroute53-helpers
file-type-iconmodules
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
file-type-iconcore-concepts.md
file-type-iconterraform-cloud-enterprise-private-module-...
Single EC2 Instance

Single EC2 Instance

Run a single EC2 instance for stateless or stateful apps. Supports IAM roles, EBS volumes, ENIs, and EIPs.

Code Preview

Preview the Code

mobile file icon

README.md

down

Persistent EBS Volume Example

This folder contains an example of how to create an EC2 instance with two EBS volumes that are persisted between redeploys. This is useful for servers that persist data to the local hard disk and need that data to still be available after a redeploy, such as a database like MySQL, CI server like Jenkins, or CMS like WordPress. Although it should be possible to do this with native Terraform using the aws_ebs_volume and aws_volume_attachment resources, due to a bug, the aws_volume_attachment resource does not work correctly and cannot be used. Therefore, this example includes a Packer template that installs the persistent-ebs-volume scripts and uses those scripts to attach and mount the volume when the instance is booting.

Note, an EBS volume can only be associated with a single EC2 Instance, so if you need the data on the disk to be shared amongst multiple servers, check out the Amazon Elastic File System, which provides a service built on top of NFS.

How do you run this example?

To run this example, you need to do the following:

  1. Build an AMI using Packer
  2. Deploy the AMI using Terraform

These steps are described in detail next.

Build an AMI using Packer

The code that runs the EC2 instance in this example is an Amazon Machine Image (AMI) that has been defined in a Packer template under packer/build.json. To build an AMI from this template:

  1. Install Packer.
  2. Set up your AWS credentials as environment variables.
  3. Run packer build build.json to create the AMI in your AWS account. Note down the ID of this new AMI.

NOTE: If you wish to try the example on a Nitro based EC2 instance, use nvme_build.json instead of build.json.

Deploy the AMI using Terraform

Now that you have an AMI, use Terraform to deploy it:

  1. Install Terraform.
  2. Open up vars.tf and set secrets at the top of the file as environment variables and fill in any other variables in the file that don't have defaults. This includes the ami variable which you should fill in with the ID of the AMI you just built with Packer.
  3. terraform get.
  4. terraform plan.
  5. If the plan looks good, run terraform apply.

When the templates are applied, Terraform will output the IP address of the EC2 instance. If you SSH to the instance, it should have a /data folder. This is the EBS volume. It should persist whatever data you put into it in between restarts of this EC2 instance.

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?