Infrastructure-as-Code Series: Project Structure

Wednesday, Sep 26, 2018 10:50 · 358 words · 2 minutes read

Infrastructure-as-Code Series

  1. Getting Started
  2. Project Structure
  3. DNS (Part 1)
  4. DNS (Part 2)

In my previous blog post “Infrastructure-as-Code Series: Getting Started” I have described how to setup Terraform on your machine and how to apply in different infrastructure configuration scenarios.

Project Tree

Create your project structure using the following directory hierarchy:

▾ dc/
    ▾ fra1/
        ▾ production/
            terraform.tfvars
        ▾ staging/
            terraform.tfvars
    ▾ tor1/
        ▾ production/
            terraform.tfvars
        ▾ staging/
            terraform.tfvars
▾ dns/
    ▾ templates/
        user_data
    droplets.tf
    firewalls.tf
    outputs.tf
    provider.tf
    tags.tf
    variables.tf
▾ lb/
    lb.tf
    outputs.tf
    provider.tf
    variables.tf
▾ nodes/
    ▾ templates/
        bastion_user_data
        compute_node_user_data
    dns.tf
    droplets.tf
    firewalls.tf
    outputs.tf
    provider.tf
    tags.tf
    variables.tf
Makefile

Brief Description of the Project Structure

  • dc – data centers (fra1, tor1) specific configuration settings per environments (staging, production)
  • dns – dns definitions
  • lb – public load-balancers definitions
  • nodes – compute nodes definitions
  • Makefile – a makefile with all commands for validating, debugging, deploying and destroying cloud infrastructure

Test the Project

Initialize (if needed) your project:

terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "digitalocean" (0.1.3)...
- Downloading plugin for provider "template" (1.0.0)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.digitalocean: version = "~> 0.1"
* provider.template: version = "~> 1.0"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Run the debug command under your project:

export TF_VAR_digitalocean_token=<DO_TOKEN>

make debug dc=tor1 env=production op=nodes

In the next blog post I will describe how to provision DNS setup for your infrastructure.

The complete demo application, described in this blog post, can be found here.
comments powered by Disqus