[November 20 Webinar] Detecting & Correcting Infrastructure Drift

➡️ Register Now

Terraform

How to Use GitHub Copilot for Terraform Infrastructure

terraform github copilot

In this article, we will explore the exciting world of generative AI tools, specifically GitHub Copilot. We will examine its features and what it can do for us when writing Terraform configurations for infrastructure as code (IaC). We will also run through some examples and give pointers along the way.

What we will cover:

  1. What is GitHub Copilot?
  2. GitHub Copilot features
  3. How to use GitHub Copilot?
  4. Does Copilot work with Terraform?
  5. Examples of using GitHub Copilot to write Terraform configuration

What is GitHub Copilot?

GitHub Copilot is a code completion tool developed by GitHub (owned by Microsoft) and OpenAI (49% owned by Microsoft) that assists users of Visual Studio Code, Visual Studio, Neovim, and JetBrains integrated development environments (IDEs) by autocompleting code. It acts like a pair programmer alongside you, helping you write code faster and with less effort.

As developers write code, Copilot suggests entire lines or blocks of code based on the context of what’s being written. It can generate code for a wide range of programming languages and frameworks, drawing from its vast training dataset of publicly available code repositories. The integration with development environments means you can access its suggestions directly within your workflow without needing to switch between tools.

Copilot is built on OpenAI’s GPT (Generative Pre-trained Transformer) architecture, which is also the foundation of many AI language models, including Chat GPT.

GitHub Copilot features

GitHub Copilot offers several features designed to enhance coding productivity.

1. Code autocompletion

Copilot suggests code completions as you type, including entire lines or blocks of code. By providing relevant suggestions based on context, it can significantly speed up your coding process, especially for repetitive tasks or boilerplate code. Autocompletion suggestions can include everything from filling in function calls or variable names to generating entire code blocks. Over time, as developers use Copilot and provide feedback, it can learn and improve, becoming increasingly adept at understanding and generating code.

2. Integration with development tools

A major benefit of Copilot is its seamless integration with popular developer environments, allowing you to access its features directly within your workflow with no need to switch between different tools. GitHub Copilot also integrates seamlessly with GitHub, allowing you to use it directly from within the GitHub interface.

Some supported environments include:

  • Visual Studio Code
  • Visual Studio
  • JetBrains IDEs (e.g., PyCharm, WebStorm)
  • Neovim

3. Multi-language support

Copilot works with multiple programming languages, allowing developers to work in various environments. Whether you’re writing Python, JavaScript, TypeScript, Java, C++, Go, or any other language, Copilot adapts to your needs. This makes it a valuable tool for developers working on polyglot projects or those exploring new languages.

4. Context-aware suggestions

Copilot goes beyond simple string matching for completions. It understands the context of your code and your intent to provide suggestions relevant to the specific situation. It can answer coding questions, refactor code, fix issues, and generate unit test cases and code documentation. For instance, if you’re working on a function to calculate area, Copilot might recommend relevant geometric formulas or functions from libraries you’re using.

5. Chat interface

Copilot includes a natural language interface that you can use within your integrated development environment (IDE). You can ask Copilot for help, discuss code, describe what you want to achieve, and get assistance while you work.

6. Responsible AI practices

GitHub Copilot is designed with responsible AI practices in mind. It includes filters to block offensive language in prompts and avoids synthesizing suggestions in sensitive contexts. Copilot is also designed to respect licensing constraints and can suggest code snippets that comply with the licenses of the project being worked on.

How to use GitHub Copilot?

Here are the steps required to set up and use GitHub Copilot:

  1. Ensure you’re using a compatible development environment. Popular options include Visual Studio Code, Visual Studio, JetBrains IDEs (PyCharm, WebStorm, etc.), and Neovim.
  2. You’ll need a GitHub account to sign up for GitHub Copilot.
  3. GitHub Copilot generally requires a paid subscription, although it offers a free trial. You can subscribe through your personal account with a GitHub Copilot Individual subscription or through an organization account with a GitHub Copilot Business subscription. To sign up, go to your GitHub profile, click your profile photo, and then select “Your Copilot.” On the GitHub Copilot settings page, click “Start free trial” and follow the steps to set up your subscription.
  4. Install the GitHub Copilot Extension in your IDE.

For Visual Studio Code, go to the Extensions view (you can do this by clicking the square icon on the sidebar or pressing Ctrl+Shift+X).

Search for “GitHub Copilot” in the search bar and install the extension.

github copilot for terraform

GitHub CoPilot VSCode Extension.

If you haven’t authorized Visual Studio Code with your GitHub account, you’ll be prompted to sign in. Follow the authentication process to authorize GitHub Copilot2. 

If you aren’t prompted to sign in, click on the bell icon at the bottom right and sign in from there.

github copilot terraform

Once signed in, you’ll see the GitHub icon next to the bell.

github copilot icon
  1. Once installed, licensed, and signed in, start coding, and you will see suggestions!

Does Copilot work with Terraform?

GitHub Copilot can assist in writing Terraform code by providing code suggestions, autocompletion, and even snippets based on the context of what you are typing. 

Here are some ways Copilot can be useful with Terraform:

  • For those new to Terraform, Copilot can provide examples and suggestions that explain how to use various Terraform features.
  • Copilot helps in detecting syntax errors and potential issues in Terraform scripts.
  • Copilot can generate comments and documentation for Terraform code, making it easier to understand and maintain.

To use Copilot with Terraform, ensure that the GitHub Copilot extension is installed and enabled in your code editor (like VS Code). Copilot will automatically start providing suggestions as you write your Terraform code.

Examples of using GitHub Copilot to write Terraform configuration

Let’s look at some examples.

Example: Attribute suggestions

Open a new file in VSCode and name it main.tf.

Enter the following code to start the coding of an AWS bucket:

resource "aws_s3_bucket" "my_bucket" {
  bucket = 

Github CoPilot will automatically suggest a value of “my-tf-test-bucket” in gray text. You can accept the suggestion by pressing Tab.

copilot terraform example

Example: Resource declarations

Try entering the following code to create an Azure resource group.

resource "azurerm_resource_group" "my_rg"
github copilot

Notice the rest of the resource variables are filled in. Press the tab to accept the suggestion.

Let’s try a more complex Azure VM resource:

resource "azurerm_virtual_machine" "vm1" {
terraform github copilot azure

Notice how much time code autocompletion saves when it comes to boilerplating your code.

Why use Spacelift with Terraform?

Terraform is really powerful, but to achieve an end-to-end secure GitOps approach, you need to use a product that can run your Terraform workflows. Spacelift takes managing Terraform to the next level by giving you access to a powerful CI/CD workflow and unlocking features such as:

  • Policies (based on Open Policy Agent) — You can control how many approvals you need for runs, the kind of resources you can create, and the kind of parameters these resources can have, and you can also control the behavior when a pull request is open or merged.
  • Multi-IaC workflows — Combine Terraform with Kubernetes, Ansible, and other IaC tools such as OpenTofu, Pulumi, and CloudFormation, create dependencies among them, and share outputs
  • Build self-service infrastructure — You can use Blueprints to build self-service infrastructure; simply complete a form to provision infrastructure based on Terraform and other supported tools.
  • Integrations with any third-party tools — You can integrate with your favorite third-party tools and even build policies for them. For example, you can Integrate security tools in your workflows using Custom Inputs.

Spacelift enables you to create private workers inside your infrastructure, which helps you execute Spacelift-related workflows on your end. The documentation provides more information on configuring private workers.

To learn more about Spacelift, create a free account today or book a demo with one of our engineers.

Key points

Copilot is powerful, but it’s important to remember it’s an assistant, not a replacement for your coding skills. It can suggest infrastructure code, but it’s up to you to review, understand, and integrate it effectively into your project.

Here are some additional resources to learn more about GitHub Copilot:

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Manage Terraform Better with Spacelift

Spacelift helps manage Terraform state, build more complex workflows, supports policy as code, programmatic configuration, context sharing, drift detection, resource visualization and many more.

Learn more

The Practitioner’s Guide to Scaling Infrastructure as Code

Transform your IaC management to scale

securely, efficiently, and productively

into the future.

ebook global banner
Share your data and download the guide