WARNING: I AM IN NO WAY RESPONSIBLE IF YOU RUN THIS WITHOUT VERSION CONTROL AND THEN BREAK YOUR INFRASTRUCTURE.
Anyone who needs to upgrade a large amount of Terraform <= 0.11 into 0.12-compliant HCL, but doesn't want to manually find and rename variables, specifically within modules. If you haven't used count
as a variable name for modules, you can just use standard search-and-replace within your editor.
- Replaces reserved word[s] in Terraform files in preparation for 0.12.
- Pulls variables out of any .tf file not named variables.tf, and places them in variables.tf
- Actually perform the 0.12 upgrade. Hashicorp's own 0.12upgrade tool does a wonderful job at that.
- Work in all edge cases. If you find one, please submit a PR.
- Pull variables out if they are not in a contiguous block.
- Alpha-sort the variables.tf file. It could be done with a clever regex.
- Use a regex to find variables. There is a lengthy comment block around line 100 detailing a wonderfully complex regex that does in fact work for many cases, but not all. As such, I opted to not use it, and instead use readlines() and look for strings starting with "variable." If you can improve the regex, again, please submit a PR.
- Pretty up the resultant .tf files. There may be newlines or lack thereof. 0.12upgrade may handle some of those; I know it splits out one-line variables with definitions into multi-line.
- Version - Python 3.x. Tested with 3.7.7, but it should probably work with anything 3.5+, as I'm using type hints. If you want to strip those out, it will probably work with 3.x, maybe 2.7. I have no interest in backporting it to 2.7 if it doesn't work.
- OS - It should work on *nix and Windows. UNC paths on Windows would probably cause issues with os.path, but if you're accessing local files it should be fine. Developed on a Mac.
From within whatever directory you wish to make changes, run this file. It will recurse into every subdirectory, so start small. It takes no args, but that is a potential feature to be added.
- Modules referencing
count
but specifying a different variable name, such asconsul_count
(i.e. if your .tf file has multiple modules, which are using different variables to keep track of resource count), this will not make changes. - This does not alert you to all issues, only some. It is expected that you will carefully check your diff prior to usage, although heavy reliance is also being made on the fact that if something gets garbled, Terraform will be unable to parse the file, and will alert you. A potential optional feature is to add all-or-nothing writes, i.e. if one function fails, roll back all changes.