The GCP Terraform Generator is a powerful tool designed to simplify and streamline the process of creating Terraform configurations for GCP infrastructure. With this tool, you can quickly generate Terraform code to provision GCP resources such as IoT Core, BQ tables, App engines, Cloud functions, Pub Sub, and much more.
Table of contents
$ go install github.com/joselitofilho/gcp-terraform-generator@latest
- graphviz: Graphviz is open source graph visualization software. Graph visualization is a way of representing structural information as diagrams of abstract graphs and networks.
- terraform: Terraform is an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently.
- Generate initial stack infrastructure folders.
- Generate GoLang code and Terraform files.
- Diagrams integration: Generate everything based on the exported XML diagram.
- Customization Options: Tailor generated code to your specific requirements using customizable templates and configuration parameters.
- Best Practices: Adhere to GCP and Terraform best practices with automatically generated code that follows industry standards.
- Supported resources:
- App Engine
- Big Query
- Big Table
- DataFlow
- IoT Core
- Pub Sub
- Storage
- Everything is customizable.
$ gcp-terraform-generator --workdir ./examples/diagram-as-code
The code generator already comes with some pre-configured templates for generating Terraform and GoLang files. All generator configuration is based on YAML files, making it easy to customize the available resources and templates.
The first step is to write the configuration file specified here. You can also use this example as a reference.
There you go! Now you can generate the structure of your project or the files based on the configured resources. You can execute the commands in any order.
If you're using Diagrams, you can also generate the initial configuration file based on the XML generated by the tool.
If you have any questions or suggestions, go to the Contributing section. Your contribution is much appreciated.
Step 1: Create a folder to organize the diagram and configuration files, ideally named after your stack.
$ mkdir mystack
Step 2: Create your diagram using Diagrams. If you have already created one, proceed to the next step.
Step 3: Export and download your diagram as an XML file (file name suggestion: diagram.xml
).
You can find instructions on how to do that at this link: https://www.drawio.com/doc/faq/export-to-xml.
Move the file to the folder created in the Step 1.
$ mv ~/Downloads/diagram.xml mystack/diagram.xml
Step 4: Create the diagram configuration file.
Suggestion diagram.config.yaml:
$ cp ./examples/diagram-as-code/diagram.config.yaml mystack/diagram.config.yaml
Change the values according to your project.
Step 5: Create the structure configuration file.
Suggestion structure.config.yaml:
$ cp ./examples/diagram-as-code/structure.config.yaml mystack/structure.config.yaml
Step 6: Run the generator guide to assist you.
$ gcp-terraform-generator --workdir mystack
Output:
██████╗ ██████╗ ██████╗ ███████╗ ██████╗ ███████╗███╗ ██╗
██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██╔════╝ ██╔════╝████╗ ██║
██║ ██║ ██║██║ ██║█████╗ ██║ ███╗█████╗ ██╔██╗ ██║
██║ ██║ ██║██║ ██║██╔══╝ ██║ ██║██╔══╝ ██║╚██╗██║
╚██████╗╚██████╔╝██████╔╝███████╗ ╚██████╔╝███████╗██║ ╚████║
╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚══════╝╚═╝ ╚═══╝
GCP
? What would you like to do? [Use arrows to move, type to filter]
> Generate a diagram config file
Generate the initial structure
Generate code
Exit
To use these configurations:
- Navigate to the desired stack/environment folder.
- Customize the Terraform files (
main.tf
,vars.tf
, etc.) according to your requirements. - Run commands to manage the infrastructure.
User guide:
$ gcp-terraform-generator --workdir ./example
Or use commands:
$ gcp-terraform-generator diagram -c ./example/diagram.config.yaml -d ./example/diagram.xml -o ./example/diagram.yaml
$ gcp-terraform-generator structure -c ./example/structure.config.yaml -o ./output
$ gcp-terraform-generator apigateway -c ./example/diagram.yaml -o ./output
$ gcp-terraform-generator lambda -c ./example/diagram.yaml -o ./output/mystack
$ gcp-terraform-generator kinesis -c ./example/diagram.yaml -o ./output/mystack
$ gcp-terraform-generator sqs -c ./example/diagram.yaml -o ./output/mystack
$ gcp-terraform-generator s3 -c ./example/diagram.yaml -o ./output/mystack
All you need know regarding configuration you can find in the configuration section.
For code generation, we are using the standard Golang library text/template. Further details about the available variables and the definition of some added utility functions can be found in the template section.
Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to create an issue or submit a pull request. Your contribution is much appreciated. See Contributing.
This project is licensed under the MIT License.