The HashiCorp Terraform Visual Studio Code (VS Code) extension adds syntax highlighting and other editing features for Terraform files using the Terraform Language Server. Features Manages installation and updates of the Terraform Language Server (terraform-ls), exposing its features.
- Terraform Fmt Visual Studio Code
- Visualize Terraform Plan
- Setup Terraform Visual Studio Code
- Visual Studio Code Terraform 12
The HashiCorp Terraform Visual Studio Code (VS Code) extension adds syntax highlighting and other editing features for Terraform files using the Terraform Language Server.
Features
- Manages installation and updates of the Terraform Language Server (terraform-ls), exposing its features:
- Completion of initialized providers: resource names, data source names, attribute names
- Diagnostics to indicate HCL errors as you type
- Initialize the configuration using 'Terraform: init' from the command palette
- Run
terraform plan
andterraform apply
from the command palette - Validation diagnostics using 'Terraform: validate' from the command palette or a
validateOnSave
setting
- Includes syntax highlighting for
.tf
and.tfvars
files -- including all syntax changes new to Terraform 0.12 - Closes braces and quotes
- Includes
for_each
andvariable
syntax shortcuts (fore
,vare
,varm
)
- Terraform allows infrastructure to be expressed as code in a simple, human readable language called HCL (HashiCorp Configuration Language). It reads configuration files and provides an execution plan of changes, which can be reviewed for safety and then applied and provisioned.
- Visual-studio-code terraform. Improve this question. Follow asked Jun 28 '20 at 3:11. 491 7 7 silver badges 17 17 bronze badges. Add a comment 1 Answer Active Oldest Votes. The Terraform plugin for Visual Studio Code no longer supports tflint since v2. You can see the difference in supported features in the README for.
'Infrastructure as code' is the top reason why over 81 developers like Terraform, while over 270 developers mention 'Intellisense, ui' as the leading cause for choosing Visual Studio. Terraform is an open source tool with 17.7K GitHub stars and 4.83K GitHub forks. Terraform This is a Visual Studio Code extension. Adds syntax support for the Terraform and Terragrunt configuration language.
Getting Started
IMPORTANT: After installing, you must perform a
terraform init
to provide terraform-ls
with an up-to-date provider schemas. The language server will not work correctly without first completing this step!- Install the extension from the Marketplace
- Reload VS Code after the installation (click the reload button next to the extension)
- Perform a
terraform init
to provideterraform-ls
with an up-to-date provider schema - Open your desired workspace and/or the root folder containing your Terraform files. Note: see Known Issues below about multi-folder workspaces
- Depending on your settings in VS Code, completion will start automatically (if not inside quotes/string literal, on certain trigger characters), or you can explicitly trigger completion via keyboard combination (Ctrl+Space on Windows, control+space on Mac).
Configuration
This extension offers several configuration options. To modify these, navigate to the extension view within VS Code, select the settings cog and choose Extension settings, or alternatively, modify the
.vscode/settings.json
file in the root of your working directory.Telemetry
We use telemetry to send error reports to our team, so we can respond more effectively. If you want to disable this setting, add
'telemetry.enableTelemetry': false
to your settings.json and that will turn off all telemetry in VSCode. You can also monitor what's being sent in your logs.Multiple Workspaces
If you have multiple root modules in your workspace, you can configure the language server settings to identify them. Edit this through the VSCode Settings UI or add a
.vscode/settings.json
file using the following template:If you want to automatically search root modules in your workspace and exclude some folders, you can configure the language server settings to identify them.
Formatting
To enable formatting, it is recommended that the following be added to the extension settings for the Terraform extension:
Validation
An experimental validate-on-save option can be enabled with the following setting:
This will create diagnostics for any elements that fail validation.
terraform validate
can also be run using the setting in the command palette.Release History
v2.0.0 is the first official release from HashiCorp, prior releases were by Mikael Olenfalk.
The 2.0.0 release integrates a new Language Server package from HashiCorp. The extension will install and upgrade terraform-ls to continue to add new functionality around code completion and formatting. See the terraform-ls CHANGELOG for details.
In addition, this new version brings the syntax highlighting up to date with all HCL2 features, as needed for Terraform 0.12 and above.
Configuration Changes Please note that in 2.x, the configuration differs from 1.4.0, if you are having issues with the Language Server starting, you can reset the configuration to the following:
See the CHANGELOG for more information.
Terraform 0.11
If you are using a Terraform version prior to 0.12.0, you can install the pre-transfer version of this extension manually by following the instructions in the wiki.
Known Issues
- Multi-folder workspaces are not yet supported. (info)
- A number of different folder configurations (specifically when your root module is not a parent to any submodules) are not yet supported. (info)
User Group
We're starting a user group for the Terraform VS Code extension. Is citrix compatible with mac. This group offers opportunities to provide feedback, access pre-release versions, and help influence the direction of this project. Collaboration and updates will be via HashiCorp Discuss (discuss.hashicorp.com), along with occasional live events.
If you are interested, please complete and submit our participation form, and we will get back to you with an invite!
Credits
Terraform Fmt Visual Studio Code
- Mikael Olenfalk - creating and supporting the vscode-terraform extension, which was used as a starting point and inspiration for this extension.
About Terraform
Terraform is an open-source tool created by HashiCorp for developing, changing and versioning infrastructure safely and efficiently. It provides a service known as 'Infrastructure as Code' which enables users to define and provision infrastructure using a high-level configuration language.
About the Terraform extension
This extension provides the following components:
- A service connection for connecting to an Amazon Web Services(AWS) account
- A service connection for connecting to a Google Cloud Platform(GCP) account
- A task for installing a specific version of Terraform, if not already installed, on the agent
- A task for executing the core Terraform commands
The Terraform tool installer task acquires a specified version of Terraform from the Internet or the tools cache and prepends it to the PATH of the Azure Pipelines Agent (hosted or private). This task can be used to change the version of Terraform used in subsequent tasks. Adding this task before the Teraform task in a build definition ensures you are using that task with the right Terraform version.
The Terraform task enables running Terraform commands as part of Azure Build and Release Pipelines providing support for the following Terraform commands
- init
- validate
- plan
- apply
- destroy
This extension is intended to run on Windows, Linux and MacOS agents.
Create a new service connection for connecting to an AWS account
Visualize Terraform Plan
The Terraform task requires a AWS service connection for setting up the credentials to connect to an AWS account. For setting up a new AWS service connection:
- On the project page, go to Project settings and choose Service connections.
- In the New service connection list, choose AWS for Terraform.
- Enter the following details to set up the service connection:
- Connection name*: Enter a unique name of the service connection to identify it within the project
- Access key id*: Enter the access key id for your AWS account
- Secret access key*: Enter the secret access key associated with the access key id
- Region*: Enter the region of the Amazon Simple Storage Service(S3) bucket in which you want to store the Terraform remote state file e.g. 'us-east-1'
Create a new service connection for connecting to a GCP account
Setup Terraform Visual Studio Code
The Terraform task requires a GCP service connection for setting up the credentials to connect to a GCP service account. For setting up a new GCP service connection:
- Download the JSON key file containing the required credentials
- In the GCP Console, go to the Create service account key page.
- From the Service account list, select the existing service account or New service account to create a new one.
- If New service account was selected in the previous step, in the Service account name field, enter a name.
- From the Role list, select Project > Owner.
- Click Create. A JSON file that contains your key downloads to your computer.
- On the project page, go to Project settings and choose Service connections.
- In the New service connection list, choose GCP for Terraform.
- Enter the following details to set up the service connection:
- Connection name*: Enter a unique name of the service connection to identify it within the project
- Project id*: Enter the project id of the GCP project in which the resources will be managed
- Client email*: Enter the value of the client_email field in the JSON key file
- Token uri*: Enter the value of the token_uri field in the JSON key file
- Scope*: Enter the scope of access to GCP resources e.g. https://www.googleapis.com/auth/cloud-platform. For more information, see granting roles to service accounts
- Private key*: Enter the value of the private_key field in the JSON key file
Terraform tool installer task
- Search for Terraform tool installer and click on Add
- In the Version input, select the exact version of terraform you want to install on the build agent. e.g. if you want to install version 0.10.3, enter
0.10.3
Terraform task
- Search for Terraform and click on Add
Visual Studio Code Terraform 12
- Select the required provider from the Provider list. The available options denote the following:
- azurerm - Azure Resource Manager
- aws - Amazon Web Services
- gcp - Google Cloud Platform
- From the Command list, select the terraform command to execute.
- In the Configuration directory input, select the path to the directory that contains all the relevant terraform config (.tf) files. The task intends to use Terraform to build infrastructure on one provider at a time. So, all the config files in the configuration directory together should not specify more than one provider.
- In the Additional command arguments input, provide any additional arguments for the selected command either as key-value pairs(-key=value) or as command line flags(-flag). Multiple options can also be provided delimited by spaces(-key1=value1 -key2=value2 -flag1 -flag2).Examples:
- -out=tfplan (for terraform plan)
- tfplan -auto-approve (for terraform apply)
- For plan, apply and destroy commands:
- Azure subscription (only if 'azurerm' provider is selected)*: Select the AzureRM subscription to use for managing the resources used by the plan, apply and destroy commands.
- Amazon Web Services connection (only if 'aws' provider is selected)*: Select the AWS connection to use for managing the resources used by the plan, apply and destroy commands.
- Google Cloud Platform connection (only if 'gcp' provider is selected)*: Select the GCP connection to use for managing the resources used by the plan, apply and destroy commands.
Setting up AzureRM backend configuration
- Azure subscription*: Select the Azure subscription to use for AzureRM backend configuration
- Resource group*: Select the name of the resource group in which you want to store the terraform remote state file
- Storage account*: Select the name of the storage account belonging to the selected resource group in which you want to store the terrafor remote state file
- Container*: Select the name of the Azure Blob container belonging to the storage account in which you want to store the terrafor remote state file
- Key*: Specify the relative path to the state file inside the selected container. For example, if you want to store the state file, named terraform.tfstate, inside a folder, named tf, then give the input 'tf/terraform.tfstate'
Setting up AWS backend configuration
- Amazon Web Services connection*: Select the AWS connection to use for AWS backend configuration
- Bucket*: Select the name of the Amazon S3 bucket in which you want to store the terraform remote state file
- Key*: Specify the relative path to the state file inside the selected S3 bucket. For example, if you want to store the state file, named terraform.tfstate, inside a folder, named tf, then give the input 'tf/terraform.tfstate'
Setting up GCP backend configuration
- Google Cloud Platform connection*: Select the GCP connection to use for GCP backend configuration
- Bucket*: Select the name of the GCP storage bucket in which you want to store the terraform remote state file
- Prefix of state file: Specify the relative path to the state file inside the GCP bucket. For example, if you give the input as 'terraform', then the state file, named default.tfstate, will be stored inside an object called terraform.
NOTE: If your connection is not listed or if you want to use an existing connection, you can setup a service connection, using the 'Add' or 'Manage' button.