The Terraform Workflow?

3 minute read

Welcome to this lesson on the Terraform workflow.

When thinking of the Terraform workflow, there are three things.

  1. Write - First, you write the code.
  2. Plan - You review the changes that the code will make.
  3. Apply - Finally, you execute or deploy the actual code to provision the Infrastructure.

In this lesson, you will learn how each of these three steps works in the context of working as an individual practitioner, how a team collaborate on Infrastructure, and how Terraform Cloud enables this workflow to run smoothly.

Working as an Individual Practitioner

Let’s first understand how the individual work on Infrastructure as code.

  • Write: You write Terraform code just like you write code in anyother language in your editor of choice. It’s a common practice to store your work in a version controlled repository even when you’re working individually.

    # Create repository
    $ git init my-infra && cd my-infra
    Initialized empty Git repository in /.../my-infra/.git/
    # Write initial config
    $ vim
    # Initialize Terraform
    $ terraform init
    Initializing provider plugins...
    # ...
    Terraform has been successfully initialized!

    As a best practice, repeatedly running plans can help flush out syntax errors and ensure that your config is coming together as you expect.

    # Make edits to config
    $ vim
    # Review plan
    $ terraform plan
    # Make additional edits, and repeat
    $ vim

    This parallels working on code, where a feedback loop between editing code and running test commands is useful.

  • Plan: When the feedback loop of the Write step has shown a change that looks good, it’s time to commit your code and review the final plan.

    $ git add
    $ git commit -m 'Managing infrastructure as code!'
    [main (root-commit) f735520] Managing infrastructure as code!
    1 file changed, 1 insertion(+)

    The terraform apply will display a plan for confirmation before proceeding to change any infrastructure, that’s the command you run for final review.

    terraform apply
    An execution plan has been generated and is shown below.
    # ...
  • Apply: After one last check, you are ready to tell Terraform to provision the infrastructure.

    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
      Enter a value: yes
    # ...
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

    At this point, it’s common to push your local repository code to a remote location for safekeeping.

    $ git remote add origin*user*/*repo*.git
    $ git push origin main

Working as a Team

The Core Workflow Enhanced by Terraform Cloud

In the writing phase, you’re generally starting off either with a version control system as a best practice or even a flat-file if you’re working individually.

Version control is recommended as a best practice so that you and a team can collaborate and enter the issues within your code. We then move on to planning or reviewing the changes that your code will make. This is an important step in the Terraform workflow because at this point you’re not yet deploying any infrastructure, but you can go ahead and see in detail the changes that the code will make within your actual environment. And looking at the review of the changes that the code will make, you can go back and modify your code. And with this cycle, you can perfect your code and get it to work exactly as you need it to.

Finally, you move on to deploying or executing the code and making actual changes to an environment. This step deploys the infrastructure, and creates the resources in the cloud.

Whether you’re working as an individual or a team, this workflow will yield the best results and the best efficiency.

Later in the course, we’ll be going through a lab and also a lesson which maps actual Terraform commands to the workflow.

Thank you for going through this lesson.