LibrarySetting up Terraform `init`, `plan`, `apply` in CI/CD

Setting up Terraform `init`, `plan`, `apply` in CI/CD

Learn about Setting up Terraform `init`, `plan`, `apply` in CI/CD as part of Terraform Infrastructure as Code Mastery

Mastering Terraform CI/CD: init, plan, apply

This module dives into the core commands of Terraform –

code
init
,
code
plan
, and
code
apply
– and how to effectively integrate them into your Continuous Integration/Continuous Deployment (CI/CD) pipelines. Automating these steps is crucial for achieving robust Infrastructure as Code (IaC) practices, ensuring consistency, and reducing manual errors.

Understanding the Core Terraform Commands

Before integrating into CI/CD, it's vital to understand what each command does:

`terraform init` prepares your working directory.

This command downloads the necessary provider plugins and initializes the backend configuration. It's the first step in any Terraform workflow.

The terraform init command is essential for preparing a Terraform working directory. It performs several key tasks: initializing the backend (where Terraform state is stored), downloading provider plugins required by your configuration (e.g., AWS, Azure, GCP), and enabling necessary modules. Running init ensures that Terraform has all the components it needs to interact with your cloud provider or other infrastructure resources.

What is the primary purpose of terraform init?

To initialize the working directory by downloading providers, configuring the backend, and enabling modules.

`terraform plan` shows proposed infrastructure changes.

This command creates an execution plan, detailing what Terraform will do to achieve the desired state without making any actual changes.

The terraform plan command analyzes your Terraform configuration and compares it against the current state of your infrastructure. It then generates an execution plan that outlines all the actions Terraform will take – such as creating, updating, or destroying resources – to reach the desired state defined in your .tf files. This plan is crucial for review and approval before any modifications are applied, acting as a safety net.

What is the benefit of running terraform plan before apply?

It allows for a review of proposed changes, preventing unintended infrastructure modifications.

`terraform apply` executes the planned infrastructure changes.

This command applies the execution plan generated by terraform plan, making the actual changes to your infrastructure.

Once you have reviewed and approved the execution plan from terraform plan, the terraform apply command is used to enact those changes. Terraform will provision, update, or delete resources as specified in the plan. It will prompt for confirmation unless the -auto-approve flag is used, which is common in automated CI/CD pipelines after a successful plan stage.

What does terraform apply do?

It executes the actions outlined in the terraform plan to modify the infrastructure.

Integrating into CI/CD Pipelines

Automating these commands in a CI/CD pipeline ensures that your infrastructure changes are consistently tested, reviewed, and deployed. A typical workflow involves:

Loading diagram...

In a CI pipeline,

code
terraform init
and
code
terraform plan
are executed. The output of
code
terraform plan
is often stored as an artifact or displayed for review. If the plan is approved (either manually or through automated checks), a CD pipeline or a subsequent stage in the CI pipeline will execute
code
terraform apply
.

Security Note: Never commit sensitive information like API keys or passwords directly into your Terraform code. Use environment variables or secrets management tools for credentials.

Key Considerations for CI/CD Integration

When setting up Terraform in CI/CD, consider the following:

AspectCI StageCD Stage
Commandinit, planapply
PurposeValidate code, check for drift, preview changesImplement changes, provision/update infrastructure
ApprovalOften automated or manual review of plan outputTriggered by successful plan approval
State ManagementRead access to state (remote backend)Write access to state (remote backend)

Using a remote backend (like S3, Azure Blob Storage, or Terraform Cloud) is crucial for CI/CD to ensure that all pipeline runs can access and update the Terraform state file consistently and safely.

The terraform init command initializes the Terraform environment. It downloads provider plugins, which are the software components that allow Terraform to interact with specific infrastructure platforms (e.g., AWS, Azure, GCP). It also sets up the backend configuration, which determines where Terraform stores its state file. The state file is a crucial record of the infrastructure Terraform manages. Without a properly initialized environment, Terraform cannot perform operations like planning or applying changes.

📚

Text-based content

Library pages focus on text content

Learning Resources

Terraform CLI Documentation: init(documentation)

Official HashiCorp documentation detailing the `terraform init` command, its options, and best practices for initialization.

Terraform CLI Documentation: plan(documentation)

Comprehensive guide to the `terraform plan` command, explaining how to generate and review execution plans for infrastructure changes.

Terraform CLI Documentation: apply(documentation)

Detailed explanation of the `terraform apply` command, including its usage, confirmation prompts, and auto-approval options.

Terraform Documentation: CI/CD Workflows(tutorial)

A practical tutorial from HashiCorp on setting up a basic CI/CD workflow for Terraform, covering the core commands.

Terraform State Management(documentation)

Essential reading on Terraform state management, emphasizing the importance of remote state for CI/CD environments.

GitHub Actions Terraform Workflow Example(documentation)

Example usage and documentation for the `setup-terraform` GitHub Action, useful for integrating Terraform into GitHub Actions CI/CD.

GitLab CI/CD with Terraform(documentation)

Official GitLab documentation providing examples and guidance on integrating Terraform into GitLab CI/CD pipelines.

Understanding Terraform Execution Plan(video)

A video explaining the nuances of Terraform's execution plan and how to interpret its output effectively.

Terraform Best Practices for CI/CD(blog)

A blog post discussing recommended practices for implementing Terraform within CI/CD pipelines to ensure efficiency and security.

Terraform `init` Deep Dive(blog)

A blog post from HashiCorp that provides a more in-depth look at what the `terraform init` command does and why it's critical.