diff --git a/content/blog/Dockerizing-Terraform.mdx b/content/blog/Dockerizing-Terraform.mdx new file mode 100644 index 0000000..2be59d5 --- /dev/null +++ b/content/blog/Dockerizing-Terraform.mdx @@ -0,0 +1,93 @@ +--- +title: Dockerizing Terraform +description: Terraform is an amazing IaC tool. You can use Docker to run Terraform in a containerized environment. I'll share what I leared about Dockerizing Terraform. +date: 2024-11-21 +published: true +tags: ["docker", "azure", "terraform", "iac"] +--- + +
+
+ Terraform3001 +
+
+ Docker1001  +
+
+ +# Why run Terraform in a Container? + +I like to keep my development environment as clean as possible. Containers are +a great way to do that. You can run Terraform in a containerized environment to +keep your host machine clean. You can also use containers to run Terraform in a +CI/CD pipeline. + +# Prerequisites + +- Docker +- A working directory with Terraform files +- An environment which supports Terraform (e.g. Azure, AWS, GCP) +- ARM_ACCESS_KEY environment variable (for Azure) + +# Docker Image + +We will be using the official HashiCorp Terraform Docker image. You can find +the image on Docker Hub at [https://hub.docker.com/r/hashicorp/terraform](https://hub.docker.com/r/hashicorp/terraform). + +# Running Terraform in a Container + +You can run Terraform in a container using the following commands. The commands mount the current working directory to the container using `-v` and `-w` parameters, and set the `ARM_ACCESS_KEY` environment variable using the `-e` parameter. After running the commands, the containers are removed using the `-rm` parameter. + +## terraform init + +```bash +docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest init +``` + +## terraform plan -out=main.tfplan + +```bash +docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest plan -out=main.tfplan +``` + +## terraform apply main.tfplan + +```bash +docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest apply main.tfplan +``` + +## terraform destroy + +```bash +docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest destroy +``` + +# Using Aliases + +As you probably noticed, the commands to run Terraform in a container are quite +long and cumbersome. I created aliases in my `.zshrc` file to make it easier to +run Terraform in a container. Here are the aliases I use: + +```bash +# Dockerized Terraform +alias dtfinit="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest init" +alias dtfinitrc="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest init -reconfigure" +alias dtfplan="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest plan -out main.tfplan" +alias dtfapply="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest apply main.tfplan" +alias dtfdestroy="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest destroy" +alias dtfrefresh="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest refresh" +``` + +# Conclusion + +Running Terraform in a container is a great way to keep your development +environment clean. It is a good thing that Hashicorp provides an official +Terraform Docker image. I try to use containers as much as possible in my +workflow to learn more about containers and to get ready to move on to +CI/CD pipelines and kubernetes soon. I hope you found this article helpful. +Let me know if you have any questions or suggestions. diff --git a/public/terraform3001.png b/public/terraform3001.png new file mode 100644 index 0000000..e604c32 Binary files /dev/null and b/public/terraform3001.png differ