Terraform Export
Highly experimental!
Disclaimer
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Basically, if you damage an existing production system by applying terraform configuration generated by this tool, that is your problem. This tool serves as an aid in migrating a CloudFormation stack to Terraform, not a de-facto solution.
You are strongly advised to deploy a copy of a production stack elsewhere, export that, then manually edit the configuration generated by this tool and apply to your test stack until you obtain the desired results, then follow your internal change control process before turning loose on a live production stack (you do have a change control process, right?).
Prerequisites
To use the Export-PSCFNTerraform cmdlet, you must have the Terraform CLI installed and able to be found in the path. See the Terraform downloads page. If the CLI binary cannot be located, the cmdlet exits with an error.
Supported Terraform Versions
As of December 2021, this has been tested with
- Terraform v1.0.0 and v1.1.0, but should work with 0.12 and later when the HCL syntax was updated.
- AWS Provider >= 3.63
About
Given a scenario where your organisation has dictated that all native CloudFormation stacks should be migrated to Terraform, this cmdlet goes some way to easing the pain of that operation. Currently terraform import does not generate HCL code for imported resources, only state information leaving the user to work out and add in all the attributes for every imported resource.
The Export-PSCFNTerraform cmdlet will read a deployed CloudFormation Stack via the AWS CloudFormation Service APIs and generate HCL for the resources the stack contains. Additionally it will fix up dependencies between these resources, input variables (stack parameters) and output values. Thus the amount of work to be done in getting the HCL for the stack correct is greatly reduced.
The cmdlet is also capable of exporting nested stacks as Terraform modules when run with the -ExportNestedStacks
argument, and will resolve the dependencies between module inputs and outputs against other objects in the configuration. If this argument is omitted, then only the root stack is exported with aws_cloudformation_stack
resources for each nested stack.
More Details
- Read about various caveats here.
- How to migrate a stack.
- See the export workflow.
- Examples
This tool makes heavy use of my CloudFormation Parser module, and is the main reason for that module's inception.
Future Improvements
- Increase the number of mapped resources.
- Learn GoLang and become a contributor to the Terraform AWS provider 😁