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

azure terraform githubcopilot kubernetes containerregistry iac ai microsoft hashicorp devops cloud

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.

Reference

https://github.com/features/copilot

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:

github_copilot_aks_terraform_01

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_aks_terraform_02

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):

github_copilot_aks_terraform_03

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

github_copilot_aks_terraform_04

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:

github_copilot_aks_terraform_05

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:

github_copilot_aks_terraform_06

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

github_copilot_aks_terraform_07

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:

github_copilot_aks_terraform_14

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:

github_copilot_aks_terraform_15

They would result into following errors:

github_copilot_aks_terraform_15

and

github_copilot_aks_terraform_15

(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:

github_copilot_aks_terraform_09

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:

github_copilot_aks_terraform_10

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

github_copilot_aks_terraform_11

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

github_copilot_aks_terraform_13

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

github_copilot_aks_terraform_12

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.

References

GitHub Copilot subscription

Visual Studio Code

GitHub Copilot Extension

Terraform

Azure subscription