Revolutionize Your Terraform Experience: Harnessing Python to Generate README.md Files effortlessly

Introduction :-

Terraform is a tool that helps you build, change, and version infrastructure safely and efficiently. It can manage existing service providers as well as custom in-house solutions.A README.md file is like a guidebook for your Terraform modules. It tells other people how to use your code, what it does, and any other important info they need to know.

Creating a README using Python Script --

2.1 Setting Up Your Environment

First things first, you need to make sure your computer is ready to go. You need to have Python installed. If you do not have Python installed, you can download it from the official Python website.

2.2 Writing the Python Script

  • Open a Text Editor like VS Code and create a file with name readme.py. Next copy the below example code into the .py file and specify the path of your terraform modules. i.e.,The Path in which the terraform files exists.

Here is an example of what part of your script might look like:

import subprocess
import os

modules_dir = "Path To Your Terraform Modules" #example: cd Desktop/terraform

def run_terraform_docs(directory):
    for module_name in os.listdir(directory):
        module_dir = os.path.join(directory, module_name)
        if os.path.isdir(module_dir) and os.path.exists(os.path.join(module_dir, "main.tf")):
            cmd = f"terraform-docs markdown {module_dir} > {os.path.join(module_dir, 'README.md')}"
            subprocess.run(cmd, shell=True)
        elif os.path.isdir(module_dir):
            run_terraform_docs(module_dir)

run_terraform_docs(modules_dir)

2.3 Running the Script

After writing your script, you need to run it. Running it means telling your computer to follow the instructions in your script.

  • Open Your Command Line Tool:- This could be Command Prompt on Windows, Terminal on macOS, or whatever tool you use to type in commands.

  • Navigate to Your Script:- Use the cd command to get to the folder or directory where your script is located.

  • Run Your Script:- Type python3 readme.py, where “readme.py” is the name of your file name, then press Enter.

You can see that a readme.md file is being created in your terraform modules same as like below.

Conclusion :-

By now, you should have a basic understanding of how to use Python to create a README.md file for your Terraform modules. This can save you time and help you keep your documentation neat and tidy. Remember, the content of the README.md is up to you. Make it helpful for anyone who might use your Terraform module. With a bit of practice, you’ll get better and faster at it. Happy coding!

0
Subscribe to my newsletter

Read articles from Mahira Technology Private Limited directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mahira Technology Private Limited
Mahira Technology Private Limited

A leading tech consulting firm specializing in innovative solutions. Experts in cloud, DevOps, automation, data analytics & more. Trusted technology partner.