Achieving terraform DRYness with terraform workspace and Ansible part 3

francis
francis
Nov 1 · 2 min read

read part one and two before reading this part.

So in the previous part, we were able to deploy to multiple environments.

The challenges that we will try solve in this part are

  • One single source of truth for variables for both the app and terraform.
  • Variable inheritance, i.e those variables that were shared in staging and production. We had to copy them in each file
  • An ephemeral environment without creating new files.

So let's start by checking out this branch terraform_with_ansible
Use this command


git checkout terraform_with_ansible

So in this, you will notice we don’t have a specific configuration for development, staging and production. We are using the same terraform for development, staging and production.
Also, notice we don’t have to terraform variables for each environment.

In this case, we are using terraform workspace to create a different environment with the same terraform file and providing variables from ansible group variables. We are also using ansible to dynamically get those variables from group vars and calling to terraform with the same environment.

Now we can create an ephemeral environment by calling this command

make deploy config=staging env=my-custom-env

And with that, you will have another replica of your environment.

Also, notice that the same environment that we are passing to terraform are the same values we are passing to the app via the env file. So we have solved the issue of having two different sources of variable values. And also we have tackled the issue of creating the ephemeral environment.

The theory will make sense if you get hands-on with part one, two and three.

In case you get issue while running the application please raise an issue so that we can improve it for the next person who reads this article https://github.com/mwaaas/ansible_terraform_demo/issues

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade