Introduction

While it’s possible to experiment and learn parts of Terraform in isolation sometimes it’s handy to have a larger, more complete, environment to run your tests in. For me unixdaemon_terraform_experiments
this is that repo. It will contain a number of different terraform based projects that can be consistently deployed together. You can see some of my thinking behind this in the Naive first steps with Terraform
post.

Terraform is a very powerful, but quite young, piece of software so I’m making this repo open to encourage sharing and invite feedback on better way to do things. There is no guarantee that anything in this repo is the best or most current way to do anything.

Bootstrap

The bootstrap phase requires you to have AWS account credentials. For this repo it’s recommended that you store them in .aws/credentials
under distinct profile names and leave [default]
empty.

We’ll do the initial terraform configuration out of bounds to avoid making bootstrapping difficult. First we create the S3 bucket, which must have a globally unique name, used to store the terraform state files. Then we enable bucket versioning in case of anything going hideously wrong.

The AWS_REGION
and DEPLOY_ENV
variables will help us when we later need to have AWS resources in multiple regions or if you decide to have separate test, staging and production environments for example.

You will also need to make a change to the projects Rakefile
and tell it your BUCKET_NAME and
BUCKET_REGION`. These are (currently, and awkwardly) set as constants at the top of the file and should match the values you exported above.

You should now install Terraform. This can be done by downloading the file from the Terraform website
, or possibly installing it using your package manager.

Once this is done we’ll enable our rake
terraform wrapper by installing its dependencies.

$ bundle install

You can then see the possible rake
tasks with

$ bundle exec rake -T
...
rake plan # Show the terraform plan
...

Setting up an environment

Before we add our first Terraform project we’ll configure an environment. I’ve decided to structure this repo and code to have three environments, test
, staging
and production
. Each of those will be implemented as a distinct Amazon AWS Account and will have their own S3 distinct bucket for state. If you want to have your own environment names then you’ll need to change ALLOWED_ENVIRONMENTS
in the Rakefile
.

We then create our environment specific variable file.

mkdir variables
echo 'environment = "test"' > variables/test.tfvars

Running an initial terraform project

Now we’re past all the basic configuration we’ll create a very simple Terraform project and apply it to confirm everything is working. For our initial project we’ll create a security group and then delete it to show the entire end to end process.

Our initial step is to create a directory under projects
to hold our new resources. Once this is done we’ll add a single security group resource.

You can now check for the security group using either the AWS console or the aws
command line tool. If you now check the S3 bucket we configured and created earlier to store our Terraform statefiles you’ll also see an object named terraform-simple-sg.tfstate
. This is where Terraform stores its remote state.

If you re-run the terraform apply
nothing will will change as Terraform has no more work to do.

Future Plans

In general most of the things under projects
will start out as tf
files full of resources and will be extracted as modules when they’re generic enough to be useful on their own. I’m sure there is third party code I could use to build most of this but as this repo is mostly for learning it’d be a little counter productive to import too much functionality.

I’ll be expanding the repo with working examples as I hit new use cases and hopefully having a larger, related, chunk of terraform code will be useful to people new to Terraform.