TFModule | Automate deployable IaC in azure
Purpose
Following the steps of azexport and given that it isn't built for production/reading code,
TFModule is a tool that let's you define a module structure and creates the necessary files to deploy your existing infrastructure.
How to use it
- Make sure
aztfexport is already installed in your system and terraform's version is later than 1.5
(if not, the import blocks won't work)
- Make a list of desired Resource Groups to import into terraform.
- Create an empty folder where
aztfexport will create all the files. If there are multiple Resource
Groups, create one folder per each Resource Group and place them inside a ResourceGroups folder.
- Run
aztfexport (once per each Resource Group):
aztfexport rg <rg-name> -n -k -f
ResourceGroups
├── RG1
│ ├── ...
│ ├── aztfexportResourceMapping.json
│ ├── aztfexportSkippedResources.txt
│ ├── main.tf
│ ├── provider.tf
│ ├── terraform.tf
│ └── terraform.tfstate
└── RG2
├── ...
├── aztfexportResourceMapping.json
├── aztfexportSkippedResources.txt
├── main.tf
├── provider.tf
├── terraform.tf
└── terraform.tfstate
- If there are more than one Resource Groups, run
tfmodule rg to join all the Resource Groups into one main terraform file:
tfmodule rg
- Create a tfmodule.yaml file where the module structure is defined (example file
can be seen here. To generate this file, you can run
tfmodule check --src <generated-tf-files-path> --conf <config-folder-path> to create a csv file with all the resources that
exist in the source directory, and tfmodule module --src <generated-tf-files-path> --conf <config-folder-path> to generate a new yaml file from the csv file (if a resource's Module column is
empty it won't be present in the yaml file):
CSV file generated by tfmodule check
Resource,Module,Quantity
azurerm_resource_group,ResourceGroups,2
azurerm_storage_account,Storage,3
azurerm_storage_container,Storage,4
azurerm_virtual_network,,2
- Run the following command specifying the folder where the source code is located (
src by default):
tfmodule appy --src <source-tf-files-path> --conf <config-folder-path
output
├── Modules
│ ├── Network
│ │ ├── main.tf
│ │ ├── output.tf
│ │ └── variables.tf
│ ├── ResourceGroup
│ │ ├── main.tf
│ │ ├── output.tf
│ │ └── variables.tf
│ └── StorageAccount
│ ├── main.tf
│ ├── output.tf
│ └── variables.tf
├── imports.tf
├── logs.txt
├── main.tf
├── terraform.tfvars
└── variables.tf
5 directories, 13 files
Dev Commands
go build -o ./main ./main.go && ./main apply -conf ./example/ -src ./de-pr-08-30/
go build -o .\main.exe .\main.go && .\main.exe apply -conf .\example\ -src .\defender-prueba\
Cross Compile
Windows compilation
GOOS=windows GOARCH=amd64 go build -o ./bin/win/tfmodule.exe ./main.go
MacOS compilation
GOOS=darwin GOARCH=amd64 go build -o ./bin/mac/tfmodule ./main.go
Roadmap
- Implement good practices at the same time
apply is run
- Give an option to split the resources into execution steps