In this article, we’re going to dive into GitHub Copilot, which is a generative AI tool. We’ll talk about the features, and how it can help us when writing Terraform configurations.
What is GitHub Copilot?
GitHub Copilot is a generative AI tool developed by GitHub, and is built on OpenAI’s GPT (Generative Pre-trained Transformer) architecture.
Using machine learning models trained on public code repositories, GitHub Copilot provides real-time suggestions for entire lines or blocks of code based on the context of what you're typing.
This can speed up the coding process, reduce errors, and help users explore different coding methods or languages with ease.
GitHub Copilot Features
GitHub Copilot is packed with features that make it ideal for Terraform users and developers in general:
- Intelligent code suggestions : Copilot suggests code as you type, offering options that fit your coding style.
- Code completion : It can complete lines, statements, or complex code snippets.
- Context-Aware : Copilot understands what you're working on and adapts to the structure and syntax of the file.
- Multi-Language support : Besides Terraform, Copilot supports many languages, including Python, JavaScript, and YAML.
- Comment-based commands : By writing a comment with clear instructions, you can direct Copilot to generate specific code for you.
- Integrated with development tools : It is seamlessly integrated with popular development environments, allowing you to access its features directly within your workflow.
How to use GitHub Copilot
Getting started with GitHub Copilot in Visual Studio Code (VS Code) takes just a few steps. Here’s how you can install the GitHub Copilot extension and start using it:
Step 1: Install Visual Studio Code
If you haven't already, download and install Visual Studio Code (VSCode) from theofficial website.
Step 2: Install the GitHub Copilot extension
- Open VSCode.
- Go to the Extensions view by clicking on the Extensions icon in the sidebar or pressing Ctrl+Shift+X.
- In the search bar, type “GitHub Copilot”.
GitHub Copilot extension in VSCode
- Click Install next to the GitHub Copilot extension by GitHub.
Step 3: Log into GitHub
After installing the extension, you’ll be prompted to sign in to GitHub:
- Click Sign in to GitHub.
- Follow the prompts to complete the sign-in process.
- Grant the necessary permissions for GitHub Copilot to work in VS Code.
Step 4: Start Using Copilot
Once you’re signed in, GitHub Copilot is ready to go. As you start typing code or comments, GitHub Copilot will offer code suggestions automatically. You can accept these suggestions by pressing Tab.
Does GitHub Copilot work with Terraform?
Yes, GitHub Copilot is compatible with Terraform and can assist in writing Terraform configuration files (.tf).
As you type out Terraform commands, GitHub generates suggestions tailored to the syntax and structure of Terraform, which is useful for tasks like setting up resources, defining providers, and specifying variables.
GitHub Copilot's suggestions can accelerate Terraform development and help you ensure that configurations are set up correctly, following best practices.
However, always double-check the suggested code to ensure that it aligns with your infrastructure’s requirements and standards.
Examples of using GitHub Copilot to write Terraform configuration
In this section, we’ll explore how GitHub Copilot can help create an Azure resource using Terraform. Let’s say you want to provision an Azure App Service plan.
If we create a new file within VSCode and save it with the name main.tf.
Enter the following code to start:
resource “azurerm_app_service_plan” “my_appserviceplan” {
GitHub Copilot will automatically suggest the name, location, resource group, and SKU code that you would need to complete the deployment of the service plan.
You can accept the suggestion by pressing Tab.
But you don’t have to start writing code to get GitHub Copilot to suggest code for you. You can start with a comment.
Open a new file in VSCode and save it called storage.tf.
Type the comment
# Terraform configuration to create an Azure storage account
GitHub Copilot will suggest the Azure provider code automatically for you. Accept it by pressing Tab.
It will then start to define the resource block to define the storage account for you, again select the suggestion using the Tab key.
Conclusion
GitHub Copilot is an incredibly powerful tool for engineers working with Terraform.
Its AI-powered suggestions can improve productivity, making it easier to write Infrastructure as Code (IaC) for platforms like Azure.
With Copilot’s ability to suggest syntax, complete code blocks, and follow Terraform best practices, it’s a great assistant for those looking to manage their infrastructure more efficiently.
Just remember, GitHub Copilot’s suggestions are based on existing patterns, so always review the generated code to ensure it fits your requirements before deployment.
Top comments (0)