How to Configure Terraform with TFenv on Mac M1 Using Docker in 3 Easy Steps
top of page

How to Configure Terraform with TFenv on Mac M1 Using Docker in 3 Easy Steps


a-puzzle-piece-fitting-into-its space


Intro


Find the solution to Terraform compatibility conflicts on M1 architecture. This Teratip helps you to bypass the difficulties related to legacy Terraform vendor incompatibility on M1 architecture using Docker with Ubuntu Linux, so you can make your plans and apply them without relying on an external Linux environment.



Step 1: Create your Dockerfile


Use the following Dockerfile to create a Docker image that includes Ubuntu 20.04 and tfenv:

FROM ubuntu:20.04


ENV DEBIAN_FRONTEND=noninteractive


RUN apt-get update && \\

    apt-get install -y --no-install-recommends git curl ca-certificates unzip && \\

    apt-get clean && \\

    rm -rf /var/lib/apt/lists/*


RUN git clone <https://github.com/tfutils/tfenv.git> ~/.tfenv && \\

    echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bashrc


ENV PATH="/root/.tfenv/bin:${PATH}"


ARG TF_VERSION=0.15.4


RUN tfenv install $TF_VERSION && \\

    tfenv use $TF_VERSION


RUN tfenv --version && \\

    terraform --version


CMD ["tail", "-f", "/dev/null"]



Step 2: Build the Docker Image


With the Dockerfile ready, proceed to build your Docker image, setting the version of Terraform that you need with the following command:


docker build --build-arg TF_VERSION=0.15.4 -t maosella/tfenv:0.25 .



Step 3: Run and Work in your Container


To run the Docker container, first go to the root of your Terraform project and run it:


docker run -it -v ${PWD}:/workspace -w /workspace maosella/tfenv:0.25 /bin/bash


This command executes the container and mounts the current directory (${PWD}) in /workspace inside the container keeping the changes synchronized between them.


the-docker-container-running-on-terraform


The container by having a volume configured to the work repository, allows to work directly with our files in VSCode synchronizing with the files that we have in the directory workspace of our container. From the shell of the container we will be able to apply plan, apply and see our synchronized files without problems in the shell.

With the -it flag you have an interactive shell to work with Terraform commands. Remember that you can change the version of Terraform with the command:


tfenv install 1.0.0


tfenv use 1.0.0


Before executing Terraform commands, we will have to configure our environment variables with the Access Key and Secret Keys of our AWS user so that Terraform is authorized to enter our account.


export AWS_ACCESS_KEY_ID="ASIAQATREYEPYOHALTB"

export AWS_SECRET_ACCESS_KEY="l6YigMubZUu4fZdDFTQR/Xo4+Y9veTREFl17B/bA3"


Security Considerations: It's essential that you handle your AWS keys with caution. Be sure not to expose your keys in scripts or Dockerfiles.

Now we have everything in order to use Terraform normally.


finished-product



Integration with Visual Studio Code (VSCode) (optional)


VSCode extension: "Remote - Containers".


In VSCode, you can install the Remote - Containers extension to manage the container filesystem as if you were working locally and all from VSCode. To do this, install the VSCode "Remote - Containers" extension to work with Docker containers directly from VSCode. You can find the extension here: Remote - Containers.

Conclusion: With these three simple steps, you can have a fully functional Terraform environment on your Mac with M1, giving you the freedom and flexibility to work on your projects without restrictions, transforming the challenge of incompatibility into a productivity win with Docker🐳.




martin-osella




Martin Osella

Cloud Engineer

Teracloud

Entradas recientes
Buscar por tags
Síguenos
  • Twitter Basic Square
bottom of page