env0 Workflows: Simplifying Advanced IaC Setups and Managing Dependencies
Tags: devops, infrastructureascode, tooling, cloud
Published: 2024-07-04 14:35:24

As infrastructure scales, managing its increasing complexity and the dependencies between different components becomes more challenging. This is where the env0 Workflows feature becomes essential, offering a solution to address today's complexities in [Infrastructure as Code](https://www.env0.com/blog/infrastructure-as-code-101) (IaC) workloads.

In this write-up, we’ll discuss the capabilities, use cases, and features of Workflows in detail.

**What are env0 Workflows?**
============================

[env0](https://www.env0.com/) Workflows provide a structured approach to managing groups of related environments by defining their dependencies and orchestrating the entire process. This ensures that all dependencies are satisfied and the workflow runs smoothly from start to finish.

Workflows are particularly useful for managing systems divided into multiple sub-systems, allowing you to oversee the entire system as a cohesive unit while retaining control over individual stacks. They simplify the process of breaking down IaC stacks into smaller, more manageable parts.

With Workflows, you can describe complex deployment relationships and initiate partial or full runs to create and update sub-environments. 

This ensures proper sequencing of resource deployment and allows changes to any sub-environment to auto-update dependent components.

Workflows also help manage environment variables and sensitive credentials across different stacks, ensuring consistency and security.

Whether your IaC is monolithic or split into microservice-based stacks, Workflows are a massive step forward in describing and managing complex deployments.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667ab7bc5081a02205e7992b_image%205.png)

> **_Disclaimer_**
> Workflows is an Enterprise plan feature. Visit here to learn more about [env0 pricing and plans](https://www.env0.com/pricing).

**How Workflows Work**
======================

A workflow is based on a [workflow template](https://docs.env0.com/docs/create-a-new-workflow#creating-a-workflow-in-the-ui) along with the IaC stack templates that you intend to deploy in your workflow environment.

The Workflow YAML file, **env0.workflow.yml**, is used to define the hierarchy of the dependencies and configuration of IaC sub-environments within a workflow declaratively.

To better understand how Workflows work, let us take an example.

Below is the definition of the **env0.workflow.yml** consisting of two environments that deploy VPC, and an EC2 instance that depends on that VPC:

    environments:
      vpc:
        name: 'Virtual Network'
        templateName: 'VPC'
      vm:
        name: 'VM'
        templateName: 'EC2'
        needs:
          - vpc

This method works when we have the respective IaC templates created in place for Virtual Network and VM stacks. 

It is also important to note that each Workflow sub-environment can also reference the directories for individual IaC components, not only from different repositories but even from different VCS providers.

Let’s take the same example above to make it clearer. 

Here we are referencing the VPC directory from our GitHub VCS repo and the EC2 directory from our Gitlab VCS repo to define our sub-environments in the Workflow YAML:

    environments:
      vpc:
        name: 'Virtual Network'
        vcs:
          type: 'terraform'
          terraformVersion: '1.5.7'
          repository: 'https://github.com/adityamurali155/env0-workflow-example'
          path: 'vpc'
      vm:
        name: 'VM'
        vcs:
          type: 'terraform'
          terraformVersion: '1.5.7'
          repository: 'https://gitlab.com/adityamurali155/env0-workflow-example'
          path: 'ec2'
        needs:
          - vpc

Notice the **needs** block makes one environment dependent on another (EC2 depends on VPC, in this case). You can add more [parameters](https://docs.env0.com/docs/create-a-new-workflow) as you see fit to define your custom Workflow YAML file.

Following on the template creation approach, with our VPC and EC2 templates now in place, we are set to develop our workflow template, which references the **env0.workflow.yml** file located in the root directory of our repository.

The next step is to deploy a workflow environment using our workflow template. This workflow environment shows us a vivid dependency graph that illustrates the relationship between our Virtual Network and VM sub-environments.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667a9ec7ed3407ad8a957385_AD_4nXeqUlSqjLrGVgmRUDws5O0EqFNbqTJNn-y-FLaj1VZVudETBunaVX3qXB3PF12d8YTgqs8XSN3saDfTxvZYk7eaMGnPih9t8Z11WFCgDPIc69xX0uiDw9V9A-LilSgn_gSLoKwKR1E4HHpnBPGgwylXrmg.png)

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667a9ec7408022ab46b86a93_AD_4nXcJA6JWCy8kCV5KNtDUbwcbMAxcg_4pc5F3mHwdV8dfrURXJw7V1ShX3d8OdGzou3AU-_9hxKWPh6D3-C7X0xnAwrdTybpGMgGpxwlPZ9y9LbEWiPTIiYiBTF935JaRrc9puUoqlwINnEuIml_TELPMWwU.png)

In this example, no variables or information are shared between the sub-environments. This presents an opportunity to leverage the Workflows' core capability of [Environment Outputs](https://www.env0.com/blog/environment-output-variables-easy-and-secure-output-piping), which allows one IaC sub-environment's output to serve as another's input.

For instance, you can output the VPC subnet ID from the Virtual Network environment and use it as input for the VM environment, ensuring the EC2 instance is deployed into the correct VPC.

This method facilitates the secure transfer of sensitive credentials or data between sub-environments dynamically via the env0 platform abd eliminates the need for local storage of sensitive information or hard-coded variables in your IaC -enhancing the security of your sub-environments.

We’ll go into the detailed mechanics of Workflows with Environment Outputs in a moment.

**Advantages of env0 Workflows**
================================

The capabilities of Workflows prove to be invaluable, particularly in scenarios like those discussed below:

*   **Dependency Management:** As emphasized earlier, Workflows excels at managing dependencies between different environments. The Workflow YAML file allows you to declaratively define all the IaC stacks you plan to deploy and specifies any dependencies between them. Additionally, Workflows visually represent these dependencies, making it easier to understand and manage complex infrastructure setups. This visual aid simplifies troubleshooting and planning, enhancing overall deployment efficiency.

*   **Targeted Deploy/Destroy:** With env0 Workflows, you can deploy or destroy specific parts of your infrastructure without affecting the entire workflow. This partial or selective deployment feature is handy in scenarios where you might only need to update or roll back certain IaC components. You could also destroy a specific IaC environment without affecting others, reducing the risk of unintended changes to other parts of your infrastructure.

*   **Multiple IaC Tools Support:** Workflows enable teams to use different IaC tools within the same workflow. For instance, one team can use Terraform to provision infrastructure, while another team uses CloudFormation. Both teams can still share environment variables or sensitive credentials through Environment Outputs for their infrastructure, ensuring seamless collaboration and integration.

*   **Custom integration:** Workflows can seamlessly integrate with other env0 features, such as [custom flows](https://docs.env0.com/docs/custom-flows), [drift detection](https://docs.env0.com/docs/drift-detection), and [approval policies](https://docs.env0.com/docs/approval-policies). For example, you can configure custom flows to run hooks (like installing a web server) after deploying an EC2 IaC stack in a workflow. 

You can also configure the workflow to run policies using an [OPA plugin](https://docs.env0.com/docs/opa-plugin) after every environment is deployed to ensure meeting security and compliance requirements.

**Practical Example**
=====================

Now, let us delve into a practical working example of how Workflows operate.

Here are three environments where we need to deploy a Virtual Network, an AKS cluster that depends on the virtual network, and a Helm chart that installs FluxCD, which depends on the AKS cluster.

The respective **env0.workflow.yml** looks like this:

    environments:
      vnet: 
        name: 'Virtual Network'
        templateName: 'Virtual Network'
      aks:
        name: 'AKS Cluster'
        templateName: 'AKS Cluster'
        needs:
          - vnet
      flux:
        name: 'Flux Installation'
        templateName: 'FluxCD'
        needs:
          - aks

First, make sure that you have created your IaC stack templates: 

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667ab0ef61c202b450fdb693_aks%201.png)

Create your [workflow template](https://docs.env0.com/docs/create-a-new-workflow#creating-a-workflow-in-the-ui) and select **Run Now** to create a workflow environment.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667ab11742fec4c49bac086d_aks%202.png)

Make sure that your environment name exactly matches that of the name defined in the Workflow YAML file.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667a9f1c0e0b9c063535e7c4_AD_4nXcxjFk1Whtu710wldrzGfdRQABnhZAO9IoftF7fhVoSJt_nEBcJcqYtffmvgTiwgxY-eH98wRuYUgxWZw7WkdF7BDMpb33RzocvvAhlrX3uw4PSwwNTNbyDwJ87wKyQilo_qe9Ev__dde9IRPgh9LCrNY_t.png)

‍

We have defined the subnet ID as an output value to be used by the AKS cluster in the VNet config, and the subnet ID as an input value in the AKS config.

    #vnet config
    output “aks_subnet_id” {
      value       = azurerm_subnet.az-subnet.id
      description = "Output the subnet ID to AKS cluster"
    }
    
    # aks config
    variable "vnet_subnet_id" {
      type        = string
      description = "Take the subnet ID as input."
    }
    

Before deploying the VNet environment, we’ll switch to the AKS environment and set the input value for the AKS environment (**vnet\_subnet\_id**) as the environment output value of the VNet environment (**aks\_subnet\_id**): 

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667a9f52ae6e3499353696ba_AD_4nXdtFjpejHFbQzTtfW6F11GMM2qGRM6vT-eqwPZDHmBWAmxGRr191tR8HKFD8hrJFebtGUj_-J0PPGGehNM-lPIijoXld6f-C3Jz0wixQ0OUY-iV-Jm33etzBin5h8Hr2xtggxJuRnHWcozK24-7H7SpViIV.png)

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667a9f52e58a85226c4dc150_AD_4nXeUaw-ELnNydEF2m0HbupZoRl_bbLdUwwnpByjlgjiZ65-RCXbwplFIjBxTZVEM2Oyb1MOSjqID92oco9SkqYIMsFqzyxSAbE40uOcjsjMngL21XDc8bJ-52UKif9ExrK-BB3G38f8TFG7nW8yvUNH8mP5z.png)

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667a9f5279a6c2f9aafc1590_AD_4nXeEKKh1g8A4OnULCzuRnUcGLYOon98RJI2wUu-CkMU33DTA7UgEi3MNWuXjg_l15oKf18BxVoT-mh26zsnNSd4Z39vF-WdBmWvrcPkxqWdL4VPZIHXfAtul14Fm-6R97Do4fkbqo45R9wuVHeVm8AeBwqA.png)

Similarly, the Flux environment expects input values from the AKS environment, and the outputs are defined in the AKS config.

    #aks config
    output "host" {
      value     = module.cluster.cluster_fqdn
      sensitive = true
    }
    output "client_certificate" {
      value     = module.cluster.client_certificate
      sensitive = true
    }
    output "client_key" {
      value     = module.cluster.client_key
      sensitive = true
    }
    output "cluster_ca_certificate" {
      value     = module.cluster.cluster_ca_certificate
      sensitive = true
    }
    
    #flux config
    variable "aks_cluster_fqdn" {
      type        = string
    }
    variable "aks_client_certificate" {
      type        = string
      sensitive   = true
    }
    variable "aks_client_key" {
      type        = string
      sensitive   = true
    }
    variable "aks_cluster_ca_certificate" {
      type        = string
      sensitive   = true
    }
    

We’ll switch to the Flux environment and set our input variables. 

Here, we are able to pass sensitive Kubernetes connection credentials as environment output to Helm for configuring and installing flux in our AKS cluster.

> Note: Environment Outputs cannot be marked as sensitive in UI on the fly. Therefore, it is necessary to make the sensitive variables marked as “sensitive=true” in your IaC config itself.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667a9f85566e8aaff765b6de_AD_4nXdZ2AjLncU-HItzQsx9zlGcbZvht4dZVsfyhoHODLlVnpgW3nF8hBNvaIF6nAp0YddHa0cRepzMK6N8rWTNGdgPERWN5kK8AA-PsBI0oijLe30tfZYZcNxKmL2qIT9OSVDG5y0Nm6XpyL0oP5lEgoSaDZI.png)

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667ab3bad9f92ba25e04dd4e_variables.png)

Lastly, after configuring all the values, we switch to the VNet environment (as it should deploy first) and run the workflow:

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667a9f86d17fba0592f51505_AD_4nXeSOM7g2Fn_19__edSf0eGkfSgvISeCLiFcqJDT0vULca_20Kku-_dU1jk7LnUrHs33rVNvjuSfboNc1kar5WUSYP3YJfvI--M9PmtiIiyDpVChOaWMvtoiVqV0iSDGY82UFuvTcypKE65qvZMYQbVpYXL1.png)

After a while, we can see our entire workflow functioning properly:

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667ab249c1d3169108d90205_workflows%201.png)

### **Less is More: Partial Workflow Deployment** 

Often, we might forget to set up values correctly between different environments. Redeploying all environments to fix these issues is not ideal and can be time-consuming.

With [Partial Workflow Deployment](https://docs.env0.com/docs/workflow-partial-deployment), you can redeploy or destroy a single sub-environment without affecting others.

Let's consider the previous example and take a scenario where we forgot to configure the environment output for the AKS environment. 

In this case, the workflow would fail, as it relies on these outputs to function correctly.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667ab49ad826f853016d58cf_workflows%202.png)

To solve this issue, navigate to the AKS environment, configure the environment output, then click on the three dots in the AKS cluster block and select the **Run from here** option.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667ab50fe69ff9c851c1551e_workflows%203.png)

We will select the AKS cluster and re-run the environment. After this, we can confirm that our workflow is operating correctly again.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667aa0ada007d9e020e0afad_AD_4nXfzz5saqda82Z_Q5nCIi4RkAz2cn_ynf5oiABS_q-sQcepsN4_z06G0Ry8w5RDLFE0cpvDhml5FXQ1b0IlNB89g4nwBiplAC-OpmKTmRYbdK7847iKGsuQvtXEWE-YLiQuf3N3VKGGHqBWFM5_9DVddgwzM.png)

**Additional Use Cases**
========================

There are more possibilities for utilizing Workflows than what we have explored so far. Here are key use cases to consider:

### **1\. Multi-tool Setup**

As infrastructure grows more complex with the business expansion, different teams may handle multiple environments. 

For example, one team can manage the DB stack and its dependencies (Billing and Configuration Services), while another team handles the EKS stack and its dependencies (Configuration and Notification Services). 

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/664e11b82a0972c00c8f317f_rl_hguSbDnBLPSAJd082D6zvvm4b6rTu0R7YIYIWJ8FUMTzLq1g_Yrb-PneaT28DUWt0L2ghVJ_7zRZ6wWYDpHCfKTspx69l-0Chkfgz4ZsE_6a1vI4eCov4DIFzrL8CIc-ijbfkLLn87WdmRdtbtAo.png)

Over time, as organizations scale, IaC deployments can grow even more intricate than the example we saw above. However, by using Workflows to define your IaC resources in a declarative approach, you can efficiently manage and track your entire IaC stack. Due to a clear presentation of their IaC stack statuses, the teams can collaborate and ensure smoother operations. 

### **2\. Bulk Operations**

Bulk operations refer to performing the same operation across multiple environments or resources simultaneously. 

In this example, the EC2 and S3 environments depend on an IAM role, both configured with a **Deny** effect for any operation on SNS topics. Later, the team decides to change the IAM role policy to **Allow** operations on the SNS topics, enabling the necessary permissions for EC2 and S3 environments to interact with those topics.

![](https://cdn.prod.website-files.com/63eb9bf7fa9e2724829607c1/667ab9ef8d370e8b30d3b115_workflows%204.png)

Utilizing Workflows simplifies making changes to one environment, automatically applying them to all dependent environments. 

This streamlines tasks such as updating software versions, applying security patches, and modifying configurations across multiple IaC environments, thereby reducing the time these tasks take.

**Wrap Up**
===========

In conclusion, env0 Workflows stand out as a powerful and efficient way to manage complex infrastructure setups and dependencies in an Infrastructure-as-Code (IaC) environment.

Interested to learn more? [Schedule a technical demo](https://www.env0.com/demo-request) to see env0 in action!