Infrastructure as code

Leverage Infrastructure as Code to manage your ZenML stacks and components.

Infrastructure as Code (IaC) is the practice of managing and provisioning infrastructure through code instead of through manual processes.

In this section, we will show you how to integrate ZenML with popular IaC tools such as Terraform.

Screenshot of ZenML stack on Terraform Registry

Terraform is a powerful tool for managing infrastructure as code, and is by far the most popular IaC tool. Many companies already have existing Terraform setups, and it is often desirable to integrate ZenML with this setup.

We already got a glimpse on how to deploy a cloud stack with Terraform using existing Terraform modules that are maintained by the ZenML team. While this is a great solution for quickly getting started, it might not always be suitable for your use case.

This guide is for advanced users who want to manage their own custom Terraform code but want to use ZenML to manage their stacks. For this, the ZenML provider is a better choice.

Understanding the Two-Phase Approach

When working with ZenML stacks, there are two distinct phases:

  1. Infrastructure Deployment: Creating cloud resources (typically handled by platform teams)

  2. ZenML Registration: Registering these resources as ZenML stack components

While our official modules (zenml-stack/aws, zenml-stack/gcp, zenml-stack/azure) handle both phases, you might already have infrastructure deployed. Let's explore how to register existing infrastructure with ZenML.

Phase 1: Infrastructure Deployment

You likely already have this handled in your existing Terraform configurations:

Phase 2: ZenML Registration

Setup the ZenML Provider

First, configure the ZenML provider to communicate with your ZenML server:

To generate an API key for an OSS server, use the command:

This will create a service account and generate an API key that you can use to authenticate with the ZenML server.

The API key is shown only once during creation. Make sure to save it securely, as you cannot retrieve it later. If you lose it, you'll need to create a new key.

You can learn more about how to generate a ZENML_API_KEY via service accounts here.

If you're using a ZenML Pro server, you will need to create a Personal Access Token or an organization-level service account and an API key for it. You can find more about Personal Access Tokens here and organization-level service accounts and API keys here.

Create the service connectors

The key to successful registration is proper authentication between the components. Service connectors are ZenML's way of managing this:

Register the stack components

Register different types of components:

Assemble the stack

Finally, assemble the components into a stack:

Practical Walkthrough: Registering Existing GCP Infrastructure

Let's see a complete example of registering an existing GCP infrastructure stack with ZenML.

Prerequisites

  • A GCS bucket for artifacts

  • An Artifact Registry repository

  • A service account for ML operations

  • Vertex AI enabled for orchestration

Step 1: Variables Configuration

Step 2: Main Configuration

Step 3: Outputs Configuration

Step 4: terraform.tfvars Configuration

Create a terraform.tfvars file (remember to never commit this to version control):

Store sensitive variables in environment variables:

Usage Instructions

  1. Install required providers and initializing Terraform:

  1. Install required ZenML integrations:

  1. Review the planned changes:

  1. Apply the configuration:

  1. Set the newly created stack as active:

  1. Verify the configuration:

This complete example demonstrates:

  • Setting up necessary GCP infrastructure

  • Creating a service connector with proper authentication

  • Registering stack components with the infrastructure

  • Creating a complete ZenML stack

  • Proper variable management and output configuration

  • Best practices for sensitive information handling

The same pattern can be adapted for AWS and Azure infrastructure by adjusting the provider configurations and resource types accordingly.

Remember to:

  • Use appropriate IAM roles and permissions

  • Follow your organization's security practices for handling credentials

  • Consider using Terraform workspaces for managing multiple environments

  • Regular backup of your Terraform state files

  • Version control your Terraform configurations (excluding sensitive files)

To learn more about the ZenML terraform provider, visit the ZenML provider.

Last updated

Was this helpful?