Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconattach-eni
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-iconbastion-host
file-type-iconec2-backup
file-type-iconpersistent-ebs-volume
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-iconpacker
file-type-iconuser-data
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-iconbastion-host
file-type-iconec2-backup
file-type-iconpersistent-ebs-volume
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

Attach ENI Example

This folder contains an example of how to create EC2 instances with Elastic Network Interfaces (ENIs) attached. This is useful for servers that need IP addresses that remain static even if the underlying EC2 Instances change (e.g., ZooKeeper). Ideally, you would attach these ENIs using Terraform's aws_network_interface_attachment resource, but it does not work with dynamic use cases, such as Auto Scaling Groups.

Therefore, this example includes a Packer template that installs the attach-eni script and uses it to attach an ENI when the Instances are booting. One of the examples shows how to attach an ENI by ID and the other by tag.

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.

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.

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?