Skip to content

graph_apply

dariuscox edited this page Jul 28, 2020 · 2 revisions

Terrawrap graph_apply

What is graph_apply?

  • graph_apply is a feature added to terrawrap which allows users to recursively apply Terraform directories while maintaining dependency order to avoid apply conflicts.

Commands

How does it work?

  • graph_apply uses .tf_wrapper files with dependency information to create a graph. The graph is an unweighted directed graph where the nodes are Terraform config directories. Once the graph is created graph_apply will then start at the source node of the graph and apply each node in a breadth first traversal style order. Any nodes on the same level will be run in parallel and levels are run in series. However, to ensure there are no conflicts, at the start of each node's apply a check is done to make sure all predecessors in the graph have completed their runs. Nodes also notify their successors once complete, to avoid the chance of a node not being run because its predecessor was still in progress.
  • graph_apply is "smart" in that it will recursively discover any dependency information by walking through a directory starting at the given directory.
  • Any directory without dependency information will be added to a list to be run in parallel at the end of the graph (similar to nodes on the same level from the root). This can be used to apply massive directories that only depend on a few files. For example, if you have multiple directories which depend on IAM, WAF, S3 and R53, but nothing else, you can add those 4 directories to the graph and run all the remaining directories in parallel without listing those dependencies in every directory. This will also work if no dependency information is present.
  • graph_apply also allows for inherited dependency configurations. For example, if you have a directory which contains all Terraform configs for a specific region, you can put a .tf_wrapper file at the root of that directory. Every sub directory with dependency configuration will inherit the root's dependency information, this eliminates the need to rewrite dependency information for every directory.

What are the requirements?

  • The latest version of Terrawrap
    • All Terrawrap requirements

What are the limitations?

  • graph_apply "doesn't" support single directories. It may work however it is not intended for this use and there already exists a tf command in terrawrap which is used for single directory operations.

How to add dependency information to .tf_wrapper?

  • Users add dependency information by adding a depends_on: option to their wrapper file.
  • Currently there are 3 ways users may add dependency information to .tf_wrapper
  1. Relative from where the command will be run.

    • If you are running graph_apply from the root of your Terraform directory where all of your Terraform configs are, ex. ~tf_configs/ , you can add dependency information relative to the root of that directory. ex:
    depends_on: aws/global/iam  # This means that it depends on ~tf_configs/aws/global/iam
  2. Relative from the config directory itself using common relative path notation ex.

    depends_on: ../../../region2/dynamodb 
    # This means that it depends on region2/dynamodb, 
    # located 3 directories back from the current dir
  3. Empty list

    • This can be used to either list an item as a source node (no dependency information) or when users want to take advantage of inherited dependency information listed further up towards the root in the current path. This will make sure the directory is added to the graph and run before the no dependency list.
    depends_on: []

Useful Tips and troubleshooting