-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Jihillestad/feature/Dockerizing-Terraform
Blog post complete
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
--- | ||
|
||
<div className="flex gap-4"> | ||
<div> | ||
<Image | ||
alt="Terraform3001" | ||
src="/terraform3001.png" | ||
width={400} | ||
height={400} | ||
/> | ||
</div> | ||
<div> | ||
<Image alt="Docker1001 " src="/docker1001.png" width={400} height={400} /> | ||
</div> | ||
</div> | ||
|
||
# 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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.