Automate Terraform Commands Using PyCharm:-
Tired of running repetitive Terraform commands while testing the code ? With a few simple configurations, you can automate the execution of terraform init, plan, apply, from within PyCharm IDE. This blogpost walks you through setting up your IDE to streamline Terraform workflows, therby saving time.
This is recommended for Dev & Test Environment where a Devops Engineer is fully invested in writing & testing Terraform Code.
Install PyCharm on Windows.
choco install pycharm-community
Note:-
The above command will install PyCharm Community Edition . We need PyCharm Professional Edition . PyCharm Community Edition DOES NOT have File Watcher Feature required for automating Terraform Commands.
Spot the Difference as stated above - PyCharm Community Edition Vs PyCharm Professional Edition:-
Community Edition (No "File Watchers" Option Available):-
Professional Edition (Licensed):-
Install the Terraform Plugin in PyCharm.
Install Terraform CLI on Windows.
winget install --id=Hashicorp.Terraform -e --accept-source-agreements
Note:-
Below follows the command to install Chocolatey and Winget Package Manager , if incase your system finds it missing.
Set-ExecutionPolicy Bypass -Scope Process -Force; `
[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.ServicePointManager]::SecurityProtocol -bor 3072; `
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Install-PackageProvider -Name NuGet -Force
Install-Module -Name Microsoft.WinGet.Client -Force -Repository PSGallery
Repair-WinGetPackageManager -AllUsers
II. Terraform Code for Resource Group Deployment:-
Note:-
The below code is self explanatory.
provider.tf
provider "azurerm" {
features {}
client_id = var.client_id
client_secret = var.client_secret
tenant_id = var.tenant_id
subscription_id = var.subscription_id
}
versions.tf
terraform {
required_version = ">= 1.4.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.70"
}
}
backend "azurerm" {
resource_group_name = "AM-Admin-RG"
storage_account_name = "amtfstatesa"
container_name = "terraform"
key = "am-rg.tfstate"
}
}
main.tf
resource "azurerm_resource_group" "am_rg" {
name = var.resource_group_name
location = var.location
tags = var.tags
}
variables.tf
variable "client_id" {
description = "Azure client ID"
}
variable "client_secret" {
description = "Azure client secret"
sensitive = true
}
variable "tenant_id" {
description = "Azure tenant ID"
}
variable "subscription_id" {
description = "Azure subscription ID"
}
variable "resource_group_name" {
description = "The name of the production resource group"
}
variable "location" {
description = "Azure region"
default = "West Europe"
}
variable "tags" {
description = "Common tags to apply to resources"
type = map(string)
default = {
environment = "test"
owner = "am"
}
}
terraform.tfvars
client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
tenant_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
resource_group_name = "AM-RG-02"
III. Settings in PyCharm for running Terraform Commands Automatically:-
Details with Reference Screesnhot:-
PyCharm IDE > Settings > Tools > File Watchers
Name of the File Watcher: Terraform Init + Plan + Apply
Files to watch - File Type: Terraform Config Files
Scope: Project Files
Tool to Run on Changes - Program: powershell.exe
Arguments: -Command "terraform init; terraform plan; terraform apply -auto-approve"
Working directory: C:\Users\amadmin\Desktop\Code
Advanced Options:
Check the option: Trigger the watcher on external changes
Check the option: Create output file from stdout
Show console: Always
IV. Automated Terraform Commands Run and Deploy:-
Details with Reference Screesnhot:-
As soon as Terraform files are saved, the File Watcher oberves the devaition/changes. This triggers the automatic execution.
Execution and Deployment is successful.
Hope You Enjoyed the Session!!!
Stay Safe | Keep Learning | Spread Knowledge