Terraform-file-Automation-Using-Python-Script

Terraform-file-Automation-Using-Python-Script

Prerequisites:-

Azure account and subscription

Azure CLI installed

Terraform installed

Python 3.x installed

An editor or IDE for Python, such as Visual Studio Code

✔ Please check out my GitHub for more information and the code related to this project!

▶ GitHub Link
🔗 Romeshdg/-Automated-Infrastructure-Provisioning-with-Python-and-Terraform- (github.com)

Steps:-

  1. Click "Azure Active Directory" in the left navigation pane.

  2. Click on "App registrations" in the left menu.

  3. Click on the "+ New registration" button.

  4. Enter a name for your application in the "Name" field.

  5. This name should be unique within your Azure AD tenant.

  6. Go to the "Certificates & Secrets" section of your Azure AD application in the Azure portal.

  7. Click on "+ New client secret".

image

image

image

Steps:-

  1. I am using VS Code to create a new file called main.tf .

  2. Open main.tf in a text editor and define the resources.

  3. create using the Terraform configuration language.

  4. Save and close main.tf

  5. Install the Azure CLI and authenticate with your Azure account.

Here's an example:

image

  1. To automate the process using a Python script, you can use the python-terraform library. Install it using pip: "pip install python-terraform".

  2. Create a new Python script called azure-resource-group.py in the same directory as main.tf

  3. In azure-resource-group.py import the terraform module.

Here's an example:

image

*The script sets the working directory to "C:/terraform" using os.chdir().

*The name of the Terraform configuration file, "main.tf" is stored in the variable config_file.

*The script sets environment variables for authentication with Azure, using os.environ[].

✅The "terraform init" command is executed using subprocess.run(), which initializes the Terraform workspace in the current directory.

image

✅The "terraform import" command is executed using subprocess.run(). This command imports an existing resource group from Azure into the Terraform state file.

✅The "terraform plan" command is executed using subprocess.run(), which generates a Terraform execution plan based on the configuration file and the imported resource group. The -out flag is used to store the execution plan in a file, and the -var flag is used to pass a variable to the configuration file.

image

image

image

✅The "terraform apply" command is executed using subprocess.run(), which applies the Terraform execution plan to create or modify the resources specified in the configuration file.

The -auto-approve flag is used to automatically approve any prompts during the apply process.

image

Overview of all the resources

#1

image

#2

image

#3

image

#4

image

Logs

image

Overall, this script automates the process of initializing a Terraform workspace, importing an existing resource group into the Terraform state, generating an execution plan, and applying that plan to create or modify resources in Azure.

📍 Thanks for reading, and happy learning! :) ✌

✨Let's continue to learn and grow together.

follow me on LinkedIn for more updates and insights. Also, don't forget to like, share, and comment on this post to spread the word and help others in their Learning journey.

Did you find this article valuable?

Support Romesh Dharamgudi by becoming a sponsor. Any amount is appreciated!