The CI/CD and DevOps Blog

Learn about various tried-and-tested strategies that will help you ship code faster

Provisioning AWS Instances Using Terraform Modules

In the previous post, we walked through the steps to provision an AWS Network using Terraform Modules. If you missed the post, here's the link to it: Provisioning AWS Network using Terraform Modules and the full code is available here: https://github.com/ric03uec/prov_aws_vpc_terraform.

In this post, we'll provision a few more components using Terraform modules. Additionally, we'll use the generated state file from the previous post as an input data source in this workflow. So it is highly recommended that you run that code first.

Since Terraform makes it super easy to create and destroy infrastructure, you should be able to spin up all components for testing and destroy them when done. I've probably done this at least ten times to test the code for this blog !!!

Provisioning AWS Network using Terraform Modules

At Shippable, we love using Terraform. From using it sparsely just a few years back, we've now reached a stage where every single component of all our environments is managed using Terraform. Hard to believe? Here's all our infrastructure code to prove it ! We've also published a few posts earlier which outline our process for managing infrastructure.Some of these are,

- Provisioning AWS Infrastructure Using Terraform

- Provisioniong AWS VPC With Terraform

- Provision AWS EC2 Virtual Machines Using Terraform

So why a new post? Terraform now supports Modules that provide an easy way to break down different parts of the infrastructure into reusable components. They also provide a Registry where users can publish their modules. Users can download "verified" modules from the registry and use them directly as building blocks for their infrastructure. We decided to give this a try by creating a complete, production-ready infrastructure(similar to what we use). The objectives of the tutorial are to

- Logically break down infrastructure components into modules

- Reuse and chain modules to create component decoupling

- Drive all configuration from one file

Provision AWS EC2 Virtual Machines With Terraform

This tutorial explains how to manually provision an AWS EC2 virtual machine using Terraform. Before you start, you should be familiar with the following concepts:

The best way to get started is to install Terraform and run scripts manually on your local machine to provision a VM. Once you understand the mechanics of it, you should consider automating your workflow by following our documentation on Automated provisioning of AWS EC2 using Terraform.

Provisioning AWS infrastructure with Terraform

Provisioning and updating infrastructure is a the first step in setting up your development, beta, or production environments. Hasicorp's Terraform format is fast becoming very popular for this use case.  We love Terraform at Shippable due to its easy declarative syntax, similar to our pipelines syntax. Other advantages are: 

  • Infrastructure as Code: Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and treated as you would any other code. Additionally, infrastructure can be shared and re-used.

  • Execution Plans: Terraform has a "planning" step where it generates anexecution plan. The execution plan shows what Terraform will do when you call apply. This lets you avoid any surprises when Terraform manipulates infrastructure.

  • Resource Graph: Terraform builds a graph of all your resources, and parallelizes the creation and modification of any non-dependent resources. Because of this, Terraform builds infrastructure as efficiently as possible, and operators get insight into dependencies in their infrastructure.

  • Change Automation: Complex changesets can be applied to your infrastructure with minimal human interaction. With the previously mentioned execution plan and resource graph, you know exactly what Terraform will change and in what order, avoiding many possible human errors.

At Shippable, we use Terraform to provision all our environments and automate the provisioning using our Pipelines feature. If you're interested in taking a look at our terraform scripts and pipelines config, we have made our repositories public so you can check them out:  

Interested in trying it yourself? The following example walks you through a sample project that provisions two t2.micro instances on AWS. We've kept it simple for easy understanding, but you can also automate provisioning of complex environments as seen in our beta infra scripts above.

Infrastructure provisioning using Terraform

You can now provision infrastructure using Terraform through Shippable builds without having to worry about  merge conflicts in state files. If you aren't familiar with it, Terraform is a tool for building, changing and versioning infrastructure safely and efficiently. It can manage existing and popular service providers as well as  a part of a custom in-house solution. You can now use Terraform to provision your infrastructure in Shippable. Here's how to get started: