Automating `plan` and `apply`
Set up automated `terraform plan` for review and `terraform apply` for deployment within your CI/CD system, ensuring consistent deployments.
Automating `plan` and `apply` is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Automating Terraform Deployments
Manually running terraform plan and terraform apply can be slow and error-prone, especially in team environments.
Automating these steps within a Continuous Integration/Continuous Deployment (CI/CD) pipeline brings significant benefits:
- Speed: Faster infrastructure changes.
- Consistency: Eliminates human error.
- Reliability: Ensures predictable deployments.
- Collaboration: Facilitates team review and approval.
Previewing Changes Automatically
The terraform plan command is your safety net. It shows exactly what changes Terraform will make to your infrastructure without actually executing them.
In a CI/CD pipeline, plan should run automatically on every code change (e.g., a pull request). This provides an immediate preview for review.
Setting Up Your Automated Plan
Integrating terraform plan into your CI pipeline typically involves a few steps:
- Initialize: Run
terraform initto prepare your working directory. - Plan: Execute
terraform planto generate the proposed changes. - Output: Capture the plan output for review.
Here's a simplified example of how this might look in a CI script:
# ci_plan_stage.sh
terraform init
terraform plan -out=tfplanUnderstanding Proposed Changes
After terraform plan runs, its output is crucial. It details every resource that will be created (+), updated (~), or destroyed (-).
Reviewing this output (often in a pull request comment) helps catch unintended changes, ensuring infrastructure integrity. It acts as a critical checkpoint.
Human Approval for Deployment
A key aspect of safe automation is gating the terraform apply step. You typically don't want apply to run immediately after every plan.
Instead, the plan output should be reviewed and explicitly approved by a team member. This approval (e.g., merging a pull request) then triggers the actual deployment.
Automated Infrastructure Deployment
Once the terraform plan has been reviewed and approved, terraform apply takes over. This command executes the changes defined in your configuration files.
In a CI/CD pipeline, apply is often triggered after a successful merge to a designated deployment branch (like main or production), or via a manual trigger after approval.
Your Automated Deployment Step
When automating terraform apply, you'll often use the -auto-approve flag in CI/CD environments. Be aware that this skips the interactive confirmation prompt.
Always ensure this step is protected by strong access controls and only runs after explicit human or automated approval based on a successful plan.
# cd_apply_stage.sh
terraform init
terraform apply -auto-approve tfplanConsistent Deployments, Every Time
Terraform is designed to be idempotent. This means if you run terraform apply multiple times with the same configuration, it will only make changes if the real-world infrastructure doesn't match the desired state.
If your infrastructure already matches the configuration, apply will do nothing, ensuring consistent and reliable deployments without unintended side effects.
Securing Your Automated Workflow
When automating plan and apply:
- Least Privilege: Ensure your CI/CD agent has only the necessary permissions.
- Remote State: Use remote state backends with locking for team collaboration.
- Version Control: Keep all Terraform configurations under version control.
- Monitoring: Implement monitoring and alerts for deployment status.
Check Your Understanding
Let's check your knowledge of automating Terraform in CI/CD!
Key Takeaways
In this lesson, we learned how to automate terraform plan and terraform apply within a CI/CD pipeline.
terraform planprovides a safe preview of changes, ideal for automated checks and human review.- Gating
applywith approval ensures critical changes are vetted. terraform applyautomates the deployment, often with-auto-approvein controlled environments.- Terraform's idempotency makes automated deployments reliable and predictable.
This automation is key for efficient, consistent, and collaborative infrastructure management!
Frequently asked questions
Is the “Automating `plan` and `apply`” lesson free?
Yes — the full text of “Automating `plan` and `apply`” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Automating `plan` and `apply`”?
Set up automated `terraform plan` for review and `terraform apply` for deployment within your CI/CD system, ensuring consistent deployments. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Automating `plan` and `apply`” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Terraform in CI/CD Pipelines
- Automating `plan` and `apply`
- Secure Credential Management
- GitOps and Pull Request Automation