Mocking Terraform AWS using Docker

Terraform is a good tool for infrastructure provisioning.  However to test terraform it could be pretty difficult. So you will create some terraform scripts and upload to the cloud a run some slow Jenkins job? and if your syntax is wrong? Well, this process can be very painful. So I want to share some simple sandbox I built in order to speed up terraform + aws development in your local machine. I might be wondering how is that possible─? Well, my secret sauce is Localstack. So we are limited to all endpoints that localstack mocks. As Localsttack adds more endpoints we benefit from that. The main idea behind this simple project is to show how easy is to docker-ize somDevOpsps tools and make engineering easy.  Currently is very often to spend 40mim or more doing baking and that's is wrong. So that's kind of mainframe era so the idea is to save time and run things local - as much as possible. Docker helps a lot with that. I run software in production using AWS Amazon Linux. Now there is Amazon Linux docker image.  This is great because you can have some OS local as you will have it in PROD.



Getting Started

First of all, you need to have Docker and git installed. Them you can clone Terraform-Docker. Once you clone docker-terraform you can run bake command. That's needed just 1 time.  After baking the docker images we can run localstack(this will need to be in another terminal). After running Localstack we can run terraform-docker.

The Terraform Project

Under the src directory you will see:

  • main.tf:        Which is our terraform "code"
  • outputs.tf:     Which are all the things Terraform will output when it finishes.
  • variables.tf:  Which are custom variables and parameters we use for terraform.
For this sample, I will create a bucket on S3 using terraform. There are some special changes that need to be made in order to this work locally. For instance, we need to point to Localstack endpoints instead of AWS ones. 


So this file is where you can see a specific IP for the S3 endpoint. I can do this because I created a Docker Network which allows me to control and define IP address for docker networks. You can see how I create a docker network and attach IPs here.

The Dockerfile

Dockerfile is pretty simple. We are using the latest Amazon Linux as base Docker image and we are installing terraform 0.11.7 and we are copying local terraform project. There is a run.sh which pretty much does terraform init and terraform apply in order to run terraform as soon as you start this container.



That's it! Now we mocked Terraform and are running all in the local machine. You can get a full project with all source code and scripts here.

Cheers,
Diego Pacheco

Popular posts from this blog

Cool Retro Terminal

Running Multi-Nodes Akka Cluster on Docker