Terraform: Reverse Terraforming

19min

What is Terraform ?

Terraform is an open-source infrastructure-as-code (IaC) tool that allows developers and operators to define and provision infrastructure resources in a declarative manner. It provides a way to automate the creation, modification, and deletion of infrastructure components, such as virtual machines, networks, and storage, across multiple cloud providers and on-premises environments. Snowflake, on the other hand, is a cloud-based data warehousing platform that offers a highly scalable and flexible solution for storing, analyzing, and processing large volumes of structured and semi-structured data. By combining the power of Terraform with Snowflake, organizations can efficiently manage their infrastructure resources and data warehousing capabilities, enabling them to rapidly deploy and scale their data-driven applications and analytics workflows. In this article, we will explore how Terraform can be used to provision and configure Snowflake resources, providing a comprehensive introduction to the integration between these two technologies.

Why Terraform your Snowflake Infrastructure?

Terraform is an infrastructure-as-code (IaC) tool that allows you to define, manage, and version your Snowflake infrastructure in a declarative manner. While Snowflake is a cloud-based data warehousing platform, using Terraform with Snowflake can provide several benefits:

  1. Infrastructure as Code: Terraform enables you to define your Snowflake resources, such as databases, schemas, warehouses, users, roles, and more, as code. This approach brings benefits like version control, collaboration, and the ability to track changes over time.
  2. Automation and Consistency: With Terraform, you can automate the provisioning and configuration of Snowflake resources, ensuring consistency across environments. This helps to reduce manual errors and guarantees that your Snowflake infrastructure is deployed consistently every time.
  3. Reproducibility: By using Terraform, you can easily reproduce your Snowflake environment across multiple stages (e.g., development, staging, production) or even across different accounts or regions. This reproducibility promotes reliability and makes it easier to maintain and update your Snowflake infrastructure.
  4. Scalability and Elasticity: Terraform allows you to define and scale Snowflake resources, such as warehouses, to handle varying workloads. You can automatically provision or resize warehouses based on demand, ensuring optimal performance and cost efficiency.
  5. Integration with Infrastructure Ecosystem: Terraform integrates with other infrastructure providers and tools, allowing you to manage your entire cloud infrastructure ecosystem with a single configuration. You can combine Snowflake resources with other infrastructure resources, such as virtual networks, security groups, storage, and compute, all in a unified Terraform workflow.
  6. Change Management and Auditability: Terraform provides a structured approach to managing changes to your Snowflake infrastructure. You can review and approve changes before applying them, track the history of changes, and maintain an audit trail of infrastructure modifications.
  7. Collaboration and Teamwork: Terraform's code-based approach enables better collaboration among team members. Infrastructure configurations can be shared, reviewed, and updated using version control systems, facilitating teamwork and enabling knowledge sharing.

By leveraging Terraform with Snowflake, you can apply infrastructure-as-code principles to your Snowflake environment, resulting in improved automation, scalability, consistency, and reproducibility.

Terraform Process Overview

Before we discuss importing Infrastructure resources(Snowflake for this guide) into Terraform, quick introduction to key terraform concepts.

Terraform State File

The Terraform state file is a crucial component of Terraform's functionality. It is an important artifact that stores the current state of your infrastructure as managed by Terraform. The state file contains information about the resources that Terraform has created, their configuration, and the relationships between them.

Terraform Resource File

In Terraform, resource files are used to define the desired infrastructure resources that you want to manage. These resource files typically have a .tf extension and contain the Terraform configuration code to create and manage resources in a particular provider.

Here are some key points about Terraform resource files: *Resource Block: A resource block is the primary construct within a Terraform resource file. It defines a specific resource type and its configuration settings. * Resource Syntax: For example, resource syntax for Snowflake database is as follows :

Shell


Hence for each snowflake object, there is a resource syntax detailed out in the link [Snowflake_terraform_resources]

Importing Resources into Terraform

Importing Snowflake resources into Terraform can be done either through the Terraform Cloud platform or by using the Terraform command-line interface (CLI).

With Terraform Cloud, the process involves connecting your Terraform configuration to your Snowflake account. You can create a workspace in Terraform Cloud and configure the necessary variables and authentication details and Terraform Cloud will then fetch the current state of the resource and bring it under Terraform's management.Alternatively,you can use the Terraform CLI to import existing Snowflake resources into your Terraform state file. You need to specify the resource type, resource identifier, and the corresponding Terraform resource address. When executed, Terraform will query the Snowflake provider to fetch the existing resource state and create an association between the resource in Snowflake and your Terraform configuration.

This guide will walkthrough on detailed steps to import resources using Terraform CLI.

Snowflake Provider

As part of the walkthrough, we have used the Snowflake-Labs provider. Further details : [Snowflake_Registry].

Step 1 - Setting up the Terraform main.py file

The first step is to setup a main.py file which has your configuration details . While writing this blogpost, the version v0.64.0 is the latest version.

Python


The account details are the details of your Snowflake instance that has to be brought under terraform. Terraform recommends that the user that you are using to connect with should atleast have SYSADMIN user role.

Step 2 - Initialize the Terraform setup

Run the following command in your shell

Shell



Step 3 - Generate Terraform resource scripts and import files with DLH.io

We have developed the world's first reverse Terraform for your Snowflake Cloud Data Warehouse and will continue to add additional abilities that enable you to take your existing Cloud DW footprint and align it with your existing Terraform investments.Our customers tell us the main use case for this feature is when they have completed a proof-of-value (POV) for Snowflake and as the solution starts moving into the next phase of development, when considering leveraging existing of future use of Infrastructure-as-a-Service (IaaS) and Terraform is the IaaS of choice, conducting a blueprint extraction from the PoV is a tremendous time-saver.

How does it work?

In DLH.io there is an option to articulate the currently deployed Snowflake Cloud Data Warehouse as a set Terraform scripts (.ts files) and state. This is perhaps a snapshot but the most important thing is that it is a complete Terraform configuration for Snowflake that can be now used to deploy the same structure of objects to another Snowflake account.

DLH.io Reverse Terraform of Snowflake service will:

  • Communicate with your Snowflake account
  • Extract all core objects you select and convert them into Terraform syntax and files

Once the files are extracted you can see the format of the files. You may need to unzip the deployed files for larger accounts that are reversed terraformed.





Step 4 - Import the Terraform .tf files into your terraform state

Once you've completed the process and downloaded your DLH.io Reverse Terraform Snowflake TF files:

  • Navigate to your Terraform repository folder in your terminal where your Terraform project exists and you have just copied the output files from DLH.io Terraform Reverse Snowflake
  • Run the terraform import commands by copy pasting them into the terminal or run them as a shell script file
  • The above step will generate a tfstate file
  • Run terraform plan . if all the resources are correctly imported this will indicate that there are no changes between the existing snowflake infrastructure and the terraform state (tfstate file). If this step indicates changes then that means that there is a difference between the resources imported and the snowflake existing production infrastructure. Any changes will have to be managed on a case by case basis.
  • After the tfstate file is in sync with the snowflake production state, any changes made in terraform will involve the below steps -
    • Make the change in .tf state file of the resource.
    • Run terraform plan to confirm the change



DLH.io Reverse Terraform
Reverse Terraform






Use Case - Replicating Objects across Instances