Browse the Repo

file-type-icon.circleci
file-type-icon_ci
file-type-icon_docs
file-type-iconexamples
file-type-iconconsul-ami
file-type-iconREADME.md
file-type-iconconsul.json
file-type-iconconsul-examples-helper
file-type-iconexample-with-custom-asg-role
file-type-iconexample-with-encryption
file-type-iconroot-example
file-type-iconREADME.md
file-type-iconmodules
file-type-icontest
file-type-icon.gitignore
file-type-iconCODEOWNERS
file-type-iconCONTRIBUTING.md
file-type-iconLICENSE
file-type-iconNOTICE
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf

Browse the Repo

file-type-icon.circleci
file-type-icon_ci
file-type-icon_docs
file-type-iconexamples
file-type-iconconsul-ami
file-type-iconREADME.md
file-type-iconconsul.json
file-type-iconconsul-examples-helper
file-type-iconexample-with-custom-asg-role
file-type-iconexample-with-encryption
file-type-iconroot-example
file-type-iconREADME.md
file-type-iconmodules
file-type-icontest
file-type-icon.gitignore
file-type-iconCODEOWNERS
file-type-iconCONTRIBUTING.md
file-type-iconLICENSE
file-type-iconNOTICE
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
HashiCorp Consul

HashiCorp Consul

Deploy a Consul cluster. Supports automatic bootstrapping, DNS, Consul UI, and auto healing.

Code Preview

Preview the Code

mobile file icon

README.md

down

Consul AMI

This folder shows an example of how to use the install-consul and either install-dnsmasq for Ubuntu 16.04 and Amazon Linux 2 or setup-systemd-resolved for Ubuntu 18.04 modules with Packer to create Amazon Machine Images (AMIs) that have Consul and Dnsmasq installed on top of:

  1. Ubuntu 16.04
  2. Ubuntu 18.04
  3. Amazon Linux 2

These AMIs will have Consul installed and configured to automatically join a cluster during boot-up. They also have Dnsmasq installed and configured to use Consul for DNS lookups of the .consul domain (e.g. foo.service.consul) (see registering services for instructions on how to register your services in Consul). To see how to deploy this AMI, check out the consul-cluster example.

For more info on Consul installation and configuration, check out the install-consul and install-dnsmasq for Ubuntu 16.04 and Amazon Linux 2 or setup-systemd-resolved for Ubuntu 18.04 documentation.

Dependencies

  1. AWSCLI must be installed on the base AMI in order for run-consul to run
  2. Git CAN be installed on the base AMI if you want to use clone commands

Quick start

To build the Consul AMI:

  1. git clone this repo to your computer.
  2. Install Packer.
  3. Configure your AWS credentials using one of the options supported by the AWS SDK. Usually, the easiest option is to set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.
  4. Update the variables section of the consul.json Packer template to configure the AWS region, Consul version, and Dnsmasq version you wish to use. If you want to install Consul Enterprise, skip the version variable and instead set the download_url to the full url that points to the consul enterprise zipped package.
  5. Run packer build consul.json.

When the build finishes, it will output the IDs of the new AMIs. To see how to deploy one of these AMIs, check out the consul-cluster example.

Creating your own Packer template for production usage

When creating your own Packer template for production usage, you can copy the example in this folder more or less exactly, except for one change: we recommend replacing the file provisioner with a call to git clone in the shell provisioner. Instead of:

{
  "provisioners": [{
    "type": "file",
    "source": "{{template_dir}}/../../../terraform-aws-consul",
    "destination": "/tmp"
  },{
    "type": "shell",
    "inline": [
      "/tmp/terraform-aws-consul/modules/install-consul/install-consul --version {{user `consul_version`}}"
    ],
    "pause_before": "30s"
  },{
    "type": "shell",
    "only": ["ubuntu16-ami", "amazon-linux-2-ami"],
    "inline": [
      "/tmp/terraform-aws-consul/modules/install-dnsmasq/install-dnsmasq"
    ],
    "pause_before": "30s"
  },{
    "type": "shell",
    "only": ["ubuntu18-ami"],
    "inline": [
      "/tmp/terraform-aws-consul/modules/setup-systemd-resolved/setup-systemd-resolved"
    ],
    "pause_before": "30s"
  }]
}

Your code should look more like this:

{
  "provisioners": [{
    "type": "shell",
    "inline": [
      "git clone --branch <MODULE_VERSION> https://github.com/hashicorp/terraform-aws-consul.git /tmp/terraform-aws-consul",
      "/tmp/terraform-aws-consul/modules/install-consul/install-consul --version {{user `consul_version`}}"
    ],
    "pause_before": "30s"
  },{
    "type": "shell",
    "only": ["ubuntu16-ami", "amazon-linux-2-ami"],
    "inline": [
      "/tmp/terraform-aws-consul/modules/install-dnsmasq/install-dnsmasq"
    ],
    "pause_before": "30s"
  },{
    "type": "shell",
    "only": ["ubuntu18-ami"],
    "inline": [
      "/tmp/terraform-aws-consul/modules/setup-systemd-resolved/setup-systemd-resolved"
    ],
    "pause_before": "30s"
  }]
}

NOTE: Amazon Linux 2 users will need to install Git first.

You should replace <MODULE_VERSION> in the code above with the version of this module that you want to use (see the Releases Page for all available versions). That's because for production usage, you should always use a fixed, known version of this Module, downloaded from the official Git repo. On the other hand, when you're just experimenting with the Module, it's OK to use a local checkout of the Module, uploaded from your own computer.

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?