Copilot in Action: Infrastructure as Code with Terraform¶
Welcome to the Platform Engineering section using Terraform! In this section, we'll touch on:
- 
Creating Terraform configurations with Copilot: Discover how to use GitHub Copilot to generate Terraform configurations for creating and managing cloud resources.
 - 
Generating Terraform modules with AI: Learn how to leverage GitHub Copilot to generate reusable Terraform modules for your infrastructure.
 - 
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¶
- Create a new file in your workspace named 
main.tf. - Add the following comment to the file:
    
# Create an AWS S3 bucket - 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 - Review the feedback from Copilot. If everything looks good, copy the code to 
main.tf. - Finally, run 
terraform initto initialize the Terraform configuration, and then runterraform validateto 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¶
- Create a new directory in your workspace named 
modules. - Inside the 
modulesdirectory, create a new directory namedec2_instance. - Inside the 
ec2_instancedirectory, create a new file namedmain.tf. - Add the following comment to the file:
    
# Create a reusable module for an AWS EC2 instance - 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 - Review the feedback from Copilot. If everything looks good, copy the code to 
modules/ec2_instance/main.tf. - Finally, run 
terraform initto initialize the Terraform configuration, and then runterraform validateto 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¶
- Ask Copilot the following question in the sidebar chat. Example:
    
Explain the output of terraform plan and terraform apply commands. - 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.