Terraform on Azure with GitHub Copilot – Creating a Kubernetes Cluster and a Container Registry

This post explains how to create a Terraform configuration, which is capable of deploying a Kubernetes cluster and a Container Registry on Azure using the GitHub Copilot

Video – watch the content of the post on YouTube

1. Introduction

This is my first blog post about using an AI tool, the GitHub Copilot, for an Infrastructure as Code (IaC) task. I was curious how GitHub Copilot could support me by writing a Terraform configuration for deploying resources on Azure. The goal was to create a final Terraform configuration by getting suggestions, which are derived from my comments. This includes: the Terraform block, the provider block, and the specific resource blocks. In this blog post, I’d like to describe my approach of using the GitHub Copilot for the very first time to create a Terraform configuration.



2. Prerequisites

The following prerequisites are necessary for this task:

I prefer Visual Studio Code as IDE, so the GitHub Copilot extension needs to be installed before. It can be installed at “Extensions”: you can use the shortcut CTRL+Shift+X and afterwards search for “GitHub Copilot” in the Marketplace. You may need to get a later version of Visual Studio Code. Ensure that the extension is properly installed:

Terraform on Azure with GitHub Copilot - Creating a Kubernetes Cluster and a Container Registry1

3. Creating a Kubernetes Cluster and a Container Registry using GitHub Copilot

I’m going to start from scratch with a blank file, in my example it is named “aks-acr-example.tf”:

GitHub Copilot provides suggestions for you – but of course, to get them – you need somehow to tell the AI pair programmer what to do. So, to get my first suggestion, I’ll add the following comment in the first line (see picture below):

After finishing the comment and hitting the “Enter” key, GitHub Copilot provides me the first suggestion, which appears as greyed text:


By hovering over the text, the GitHub Copilot command palette arises. This shows among others, that the suggestion can be accepted by either pressing the “Tab” key or by simply clicking on the occuring “Accept” button:


By accepting the suggestion, the greyed text turns into “real code”. Afterward, I’ll jump into a new line and add the “provider” block by adding the comment in line 11:


Finishing the comment in line 16 results in the creation of a “resource group” named “patricks-rg” with the location “westeurope” (line 17-20):

Now let’s explore a probably more challenging task: creating the resource block of a Kubernetes cluster by providing a meaningful comment. I’ll try it with the comment in line 22 (see picture below). I got two different suggestions as the GitHub Command Palette (hovering over the greyed text) indicates:


I decided on the first option of the two suggestions. In addition, I have to adapt the suggestion for the first time: all yellow marked lines will be removed:


They would result into following errors:

Terraform on Azure with GitHub Copilot - Creating a Kubernetes Cluster and a Container Registry


Terraform on Azure with GitHub Copilot - Creating a Kubernetes Cluster and a Container Registry

(the Kubernetes version of the AKS will be 1.26.6 after applying the configuration in the end)

Now I’m almost at the end – the next step is to take care about the Container Registry. The specific block for the ACR will be created by providing the comment of see line 42:

Terraform on Azure with GitHub Copilot - Creating a Kubernetes Cluster and a Container Registry

The Container Registry, named “patricksacr” is now done too, no adaptions were necessary.

Finally, I would like to enable the Kubernetes cluster to pull images from the Container Registry – this will be achieved by adding line 51:


The Terraform configuration file is finished, now I want to prove that it is really working. So, the following Terraform commands will be executed:

  • terraform init
  • terraform validate
  • terraform apply

After a few seconds, the Container Registry named “patricksacr” appears:

A little more time is necessary for the Kubernetes cluster, named “patricks-aks”:

The AKS is ready too after some time has passed. All desired resources are now deployed, now it is finished!

4. Conclusion

There are two things, which came immediately to my mind after finishing this task:

  1. No need to use a search engine: it is great, if there is no need to switch to the browser for searching related code snippets as an example – it saves time!
  2. Verification of the suggestions: of course you can’t expect miracles. You don’t get the final (working) code by just adding a few simple comments. Sometimes you need to reject the suggestion (or to switch to another one) and to edit/extend your comment, and sometimes you probably need to adapt the suggestion. But that’s fine, because you should not stop thinking. You should consider the GitHub Copilot as “partner” for the successfull implementation of a task instead of a complete replacement for you.

It was great to use GitHub Copilot, it worked well for my Terraform example. Just tiny adaptions were necessary to get the final Terraform configuration, which allows me to deploy the desired resources on Azure.


GitHub Copilot subscription

Visual Studio Code

GitHub Copilot Extension


Azure subscription

This blog is part of Azure AI Week. Check out more great Azure AI blogs and webinars today.

About the Author:

I’m a “Senior Integration Engineer Software”, Microsoft MVPAzure HeroHashiCorp Ambassador, Speaker, Co-organizer of the Microsoft Developer User Group Graz, and Blogger working in the Automotive Industry. I love to practice DevOps. Find out more about me here.


Koch, P. (2023). Terraform on Azure with GitHub Copilot – Creating a Kubernetes Cluster and a Container Registry. Available at: https://www.patrickkoch.dev/posts/post_31/ [Accessed: 12th January 2024].

Share this on...

Rate this Post: