Creating a custom Dev Container for your GitHub Codespace to start with Terraform on Azure

azure terraform github codespace infrastructure-as-code devcontainer configuration-as-code devops cloud

This post explains how to create a custom dev container (including the Azure CLI and Terraform) for a GitHub Codespace, which makes you ready to develop with Terraform on Azure

1. Introduction

There were three things that motivated me to write this post:

  1. My first usage of the GitHub Codespaces feature (a few months ago for work)
  2. A presentation from my good friend April Edwards at PSConfEU, with the title “Every Repository Needs a DevContainer”
  3. April’s talk at my company in February 2024, which was about GitHub and cloud native development

One of my focal points in my work and in my contributions to the tech community is Infrastructure as Code, especially using Terraform. Therefore, I wanted to create a dev container that can be used to start developing with Terraform on Azure.

2. Why you should use Dev Containers and Codespaces

In my company, we are currently getting ready for GitHub Enterprise, so for most of my colleagues, it is new to them, as we are using other platform tools to realize aspects of our DevOps approach. In addition, we do not use the concept of dev containers (consistently) - no matter which platform tool. Being in a lead position to define some principles and rules for our way of how to develop in a “cloud native way” with GitHub, it would now be the chance to change this “mindset”. I would go for having a dev container mandatory in each Git repository. You should be ready to work immediately when you finish cloning a Git repository. Having a dev container in your repository would also ensure that everyone uses the same versions of the tools, which also avoids some possible pain points. My colleagues would just need to use a GitHub Codespace, which provides a running dev container for the specific development of a Git repository. Those are just a few benefits, to which I’m referring.

See Benefits of GitHub Codespaces for a more detailed list of benefits.

3. Dev Container vs. Codespace

What is the difference between those two concepts? When do we refer to a dev container? When do we refer to a Codespace?

A dev container is the containerized environment that you, as a developer, can use to work. It can include the desired tools, like the Azure CLI, the Terraform CLI, kubectl, etc. Whatever you need for your (cloud native) development.

GitHub Codespace can be defined as a service that is capable of providing you with the included dev container while running inside the cloud.

This means you can, of course, use the concept of a dev container without having a GitHub Codespace. Create your own Dockerfile, put the tools you need in it, build it, run it, attach to it using, e.g., Visual Studio Code and start working on your code. If you are working in a team, it would be great to share the dev container with your colleagues. So, add the Dockerfile to your version control system, e.g., Git. A colleague of yours, who wants to contribute to the code located in this specific repository and who also wants to use the same environment for development, has now also got to do the same things: build the container, run it, and attach it to it.

It would be great to make that more comfortable: therefore, the concept of the GitHub Codespace is ideal IMHO. When creating a Codespace, the included dev container will be used and hosted in the cloud. So you don’t have to take care of setting up and hosting the dev container on your own.

4. Create a dev container in your GitHub Codespace including the Azure CLI and Terraform

Almost from scratch, I would like to describe the necessary steps for creating a dev container, which includes the Azure CLI and Terraform.

4.1 The initial situation: starting with an empty Git repository

I’m going to start with an empty Git repository named “azure_terraform_codespace”:


The repository will be cloned and opened with Visual Studio Code:


4.2 Creating the configuration files

Those files will be created in the “.devcontainer” directory. Inside this directory, I will add two files:

  1. Dockerfile
  2. devcontainer.json

The Dockerfile defines your containerized environment, which includes all the tools you need. The devcontainer.json file allows you to set which environment of the Codespace should be used. So you can configure your Codespace in the devcontainer.json, as you can set, among other things:

If you do not provide a devcontainer.json file, then a default configuration for the dev container will be used.

4.3 Set up the Dockerfile, which defines your environment

The purpose of the Git repository is to deploy resources on Azure, using Terraform. Therefore, I’ll set up the Dockerfile as seen below:

FROM ubuntu:22.04

# update and install sudo
RUN apt-get update && \
    apt-get -y install sudo
# install curl
RUN sudo apt-get install curl -y
# install azure cli
RUN curl -sL | sudo bash
# install terraform
RUN apt-get update && apt-get install -y gnupg software-properties-common curl wget git && \
    curl -fsSL | apt-key add - && \
    apt-add-repository "deb [arch=amd64] $(lsb_release -cs) main" && \
    apt-get update && apt-get install terraform -y
# update and install wget and git
RUN apt-get update && apt-get install -y \
    wget \

4.4 Set up the devcontainer.json file

I’ll keep the configuration simple for the devcontainer.json file and just define the “name” and “build”:

    "name": "azure_terraform",
    "build": { "dockerfile": "Dockerfile" }

The “name” defines the name for the dev container, to get an idea of the purpose. The other configuration,“build”, specifies how to build the Dockerfile. I choose to just point to the included Dockerfile, by providing the string “Dockerfile” without any additional arguments.

Finally, I’ll provide the configuration files to my “main” branch by doing a commit and a push:


5. Create a Codespace using the provided configuration files

To create a new Codespace, I’ll click on the “Remote Explorer” icon in Visual Studio Code, then I’ll switch to “GitHub Codespace”. Next I have to click on the “+” button:


Now I’ll select the repository that I want to use for the Codespace:


As the last step, I’ll choose which instance I would like to use. I’ll choose the 2 cores option:


After that, the Codespace will be created:


After the successful creation, I can recognize that an active Codespace is available for my repository:


That is the view in Visual Studio Code:


I’m going to verify that the desired tools are installed. Therefore, I’ll enter “az” in the terminal:


In addition, I also prove that Terraform was provided properly by typing “terraform”:


So my Codespace named “obscure funicular” is ready, which includes my customized dev container, and I would be ready to start my development using Terraform on Azure.

6. Stop your Codespace

Don’t forget to stop the Codespace, if you don’t need it:


Save your available core hours, respectively, money::


7. Conclusion

A GitHub Codespace including a customized dev container is created within a few minutes. It is a service that provides you with a development environment that is hosted in the cloud, so you don’t need any prerequisites on your working machine. You can start working inside the browser, but I feel more comfortable using my installed Visual Studio Code. It is a great approach to sharing the same environment with your colleagues, and they don’t need to take care of setting up on their own. As it is hosted in the cloud, it will incur costs, and as a private user, you have a limited number of hours to use it. Take care when using it, and stop or delete not-used Codespaces.

References - azure_terraform_codespace

GitHub Codespaces

Benefits of GitHub Codespaces

Every Repository Needs a DevContainer


Terraform - Install CLI