Accelerate your Terraform development with Amazon CodeWhisperer

Improve the developer experience writing Terraform code with the help of generative AI powered by Amazon CodeWhisperer.

In April 2023, AWS announced the general availability of Amazon CodeWhisperer, an AI-based coding companion that generates real-time single-line or full-function code suggestions. Now, at AWS re:Invent 2023, HashiCorp and AWS have announced the support for Terraform in Amazon CodeWhisperer. CodeWhisperer helps accelerate Terraform development by providing code suggestions that reduce total development effort, allowing Terraform practitioners to focus on end-to-end Terraform workflows. Customers can now take advantage of real-time generative AI Terraform suggestions, an open source reference tracker, and built-in security scans in Amazon CodeWhisperer.

»HashiCorp Terraform and Amazon CodeWhisperer

Amazon CodeWhisperer provides code suggestions based on large language models (LLMs) trained on billions of lines of code, including Amazon's internal code and IaC config files as well as open source code. To generate high-quality Terraform suggestions, HashiCorp and Amazon CodeWhisperer teams worked together to source sample Terraform modules and configurations written in HashiCorp Configuration Language (HCL). The teams collaborated on providing model validations, working to ensure the output generated by the CodeWhisperer meets the requirements of Terraform practitioners.

»The rise of HashiCorp Configuration Language (HCL)

CodeWhisperer and Terraform is a powerful combination, as HCL has once again been confirmed as a high-growth programming language by Octoverse, indicating that operations and IaC work are gaining prominence among developers. Specifically, HCL adoption has grown 36% year-over-year, demonstrating that developers are increasingly using declarative languages to leverage infrastructure deployments.

Top growing languages

Source: GitHub’s The State of the Octoverse 2023

»How CodeWhisperer and Terraform work together

To use CodeWhisperer with Terraform, you simply install the latest AWS Toolkit plugin in your integrated development environment (IDE) of choice. CodeWhisperer automatically detects when customers write a new Terraform configuration file (*.tf file) and generates code suggestions using comments.

Here are a few examples of what CodeWhisperer can do:

Let’s start with a simple example, suppose you want to create multiple Amazon EC2 instances using the latest Amazon Linux 2 machine image. You would start with a simple prompt to configure Terraform Cloud, followed by instructions to create the instance by looking up the Amazon Machine Image (AMI) for Amazon Linux 2. CodeWhisperer will provide suggestions for each resource block. You can select the alternative suggestions and use the tab key to accept the suggestion:

Terraform Cloud EC2 and CodeWhisperer

CodeWhisperer is also trained in understanding advanced HCL syntax and expressions. For example, you could ask it to do variable validations for bucket names with 10-20 characters without special characters. CodeWhisperer can generate suggestions as shown here:

Generating examples

Another example is to create an EC2 Security Group and populate the ingress rules using dynamic blocks expression and the existing locals:

Creating an EC2 Security Group and populating the ingress rules

»Better with Terraform enhanced editor validation

When writing Terraform code, either by hand or by leveraging an AI-based coding companion such as CodeWhisperer, errors are a fact of life. If the generated Terraform has missing artifacts or has validation errors, developers often find themselves context-switching between their editor and the CLI to validate code, leading to frustration and reducing productivity.

Enhanced editor validation in the Terraform extension for Visual Studio Code automatically validates Terraform code as early as possible, creating an enhanced, integrated authoring experience by highlighting errors and providing guidance to help resolve issues quickly.

Examples of these new validations include:

  • Identifying missing variable declarations or required attributes
  • Highlighting unexpected attributes or blocks
  • Issuing warnings for deprecated attributes.
Terraform enhanced editor validation

Validation errors are immediately identified within the Terraform extension for the Visual Studio Code editor, no context switching is needed.

»Start generating code

You can start using Terraform code generation in Amazon CodeWhisperer using AWS’ getting started resources. To complement your generative workflows you can install the Terraform extension for Visual Studio Code and learn about enhancements recently added to the extension.

Special thanks to Kevon Mayers, Kalen Arndt, and Sean Doyle whose work behind the scenes made all of this possible.

Sign up for the latest HashiCorp news

By submitting this form, you acknowledge and agree that HashiCorp will process your personal information in accordance with the Privacy Policy.