Skip to content

Copilot in Action: Infrastructure as Code with Terraform

Welcome to the Platform Engineering section using Terraform! In this section, we'll touch on:

  1. Creating Terraform configurations with Copilot: Discover how to use GitHub Copilot to generate Terraform configurations for creating and managing cloud resources.

  2. Generating Terraform modules with AI: Learn how to leverage GitHub Copilot to generate reusable Terraform modules for your infrastructure.

  3. Understanding Terraform plan and apply with Copilot: Learn how to leverage GitHub Copilot to understand the output of terraform plan and terraform apply commands.

Let's get started!

Creating Terraform Configurations with Copilot

Objective

In this exercise, we will use Copilot to generate a Terraform configuration for creating an AWS S3 bucket.

Steps

  1. Create a new file in your workspace named main.tf.
  2. Add the following comment to the file:
    # Create an AWS S3 bucket
    
  3. Use either the Copilot inline prompt or the Copilot sidebar chat to ask Copilot to generate the Terraform configuration from the comment. Example (using the sidebar chat):
    @workspace Generate Terraform configuration for the pseudo code comment in main.tf
    
  4. Review the feedback from Copilot. If everything looks good, copy the code to main.tf.
  5. Finally, run terraform init to initialize the Terraform configuration, and then run terraform validate to ensure that the configuration is valid.

Generating Terraform Modules with AI

Objective

In this exercise, we will use Copilot to generate a reusable Terraform module for creating an AWS EC2 instance.

Steps

  1. Create a new directory in your workspace named modules.
  2. Inside the modules directory, create a new directory named ec2_instance.
  3. Inside the ec2_instance directory, create a new file named main.tf.
  4. Add the following comment to the file:
    # Create a reusable module for an AWS EC2 instance
    
  5. Use either the Copilot inline prompt or the Copilot sidebar chat to ask Copilot to generate the Terraform module from the comment. Example (using the sidebar chat):
    @workspace Generate a Terraform module for the pseudo code comment in modules/ec2_instance/main.tf
    
  6. Review the feedback from Copilot. If everything looks good, copy the code to modules/ec2_instance/main.tf.
  7. Finally, run terraform init to initialize the Terraform configuration, and then run terraform validate to ensure that the configuration is valid.

Understanding Terraform Plan and Apply with Copilot

Objective

In this exercise, we will use Copilot to understand the output of terraform plan and terraform apply commands.

Steps

  1. Ask Copilot the following question in the sidebar chat. Example:
    Explain the output of terraform plan and terraform apply commands.
    
  2. These are two of the most commonly used Terraform commands. Refine your question based on your specific use case or scenario.

Conclusion

You have now seen how we can utilize Copilot to generate Terraform configurations, modules, and understand the output of Terraform commands. Copilot can be a valuable tool for platform engineering teams, helping to automate repetitive tasks, generate boilerplate infrastructure code, and provide insights into best practices.