Terraform in GitHub Codespaces

Sunday evening, and I’m trying to work on some lab projects to refresh my knowledge and learn something new. I have a corporate laptop with all the job-related development software and a personal one for hobbies like photography or blogging. I decided to play with my lab on my personal laptop. Since I don’t have Visual Studio Code or Terraform installed and didn’t want to spend time installing them, I decided to start using GitHub Codespaces.

Recently, I passed a GitHub Administration certification, and during my learning journey, I explored GitHub Codespaces but not as deeply as I would have liked. Today, I have the opportunity to dive into it further.

Prerequisites:

  • A GitHub account

Once the repository is created, Codespaces can be started immediately. I added my code and was ready to initialize Terraform and connect the repository to Terraform Cloud.

But then I hit a roadblock: the Codespaces bash doesn’t have Terraform by default.

Here is how to fix it 🙂

1. Find the Code dropdown button and click on it, then choose Codespaces. Next, find and press the Configure dev container.

    2. On the Configure dev container page, see the devcontainer.json file that is opened and on the right side – Marketplace. Search for Terraform and select the Terraform extension created by devcontainer. By the way, all the available Dev Container Features can be found here.

    3. After adding Terraform features to the devcontainer.json file, save, and commit the changes.

    {
    "image": "mcr.microsoft.com/devcontainers/universal:2",
    "features": {
    "ghcr.io/devcontainers/features/terraform:1": {}
    }
    }

    4. Return to the Codespaces section and recreate Codespaces: Delete -> Create.

    If the method above didn’t work, and you see the error from the first screen, there is an alternative way!

    1. We need to access the command palette.  You can do that in the Codespaces Visual Studio Code in 3 different ways:

    • Pressing keyboard shortcut combination Shift+Command+P (Mac) OR Ctrl+Shift+P (Windows/Linux)
    • From the Application Menu, click View > Command Palette
    • Pressing F1

    2. Once in the command palette, choose the option Add dev container configuration files

    3. Create or modify existing config

    4. Find Terraform

    5. Choose all the needed option

    6. And press OK. The configuration file now looks

    7. After that, recreate a Codespaces container

    Voila. Terraform works!

    Using GitHub Codespaces was an easy way to get a development environment running without installing anything on my laptop. I ran into an issue with Terraform not being recognized, but it gave me a chance to figure out how to set it up properly. Overall, it was a good way to start exploring Codespaces for future projects.

    vovando Avatar

    Leave a Reply

    Your email address will not be published. Required fields are marked *