How to Execute Terraform Actions Without the Interactive Prompt

TLDR
You can bypass Terraform's interactive prompts by using the -auto-approve flag for apply and destroy commands. This is useful for automation and CI/CD pipelines.
Terraform's interactive prompts are designed to prevent accidental changes to your infrastructure. However, in automated workflows, such as CI/CD pipelines, these prompts can be a hindrance. This guide will show you how to execute Terraform actions without the interactive prompt.
Why Automate Terraform Actions?
- Efficiency: Automating Terraform actions saves time and reduces manual effort.
- Consistency: Ensures that infrastructure changes are applied in a predictable manner.
- Integration: Necessary for CI/CD pipelines and other automated workflows.
Using the -auto-approve Flag
The -auto-approve flag skips the interactive approval step for terraform apply and terraform destroy. This is particularly useful in scripts and pipelines.
Example: Applying Changes
To apply changes without confirmation, use the following command:
terraform apply -auto-approve
This command applies the Terraform configuration without asking for confirmation.
Example: Destroying Resources
To destroy resources without confirmation, use:
terraform destroy -auto-approve
This command destroys all resources managed by the Terraform configuration without requiring confirmation.
Automating Variable Inputs
In addition to bypassing prompts, you may need to provide variable values non-interactively. Use a .tfvars file or the -var flag.
Using a .tfvars File
Create a file named terraform.tfvars with your variable values:
aws_region = "us-east-1"
instance_type = "t2.micro"
Terraform automatically loads this file during execution.
Using the -var Flag
You can also pass variables directly in the command:
terraform apply -var="aws_region=us-east-1" -var="instance_type=t2.micro" -auto-approve
Disabling Interactive Prompts Globally
If you want to disable interactive prompts for all Terraform commands, set the TF_INPUT environment variable to false.
Example
Here's how to set the environment variable and run Terraform commands:
export TF_INPUT=false
terraform apply -auto-approve
This approach is useful for environments where you want to enforce non-interactive behavior.
Best Practices
- Use with Caution: Skipping prompts can lead to unintended changes. Always review your plan before applying.
- Combine with
terraform plan: Runterraform planto preview changes before applying them. - Secure Variable Files: If using
.tfvarsfiles, ensure they are not exposed in version control.
By automating Terraform actions and bypassing interactive prompts, you can simplify your workflows and integrate Terraform into your CI/CD pipelines effectively.
Try it hands-on
Run the commands from this article in the browser. Nothing to install.
We earn commissions when you shop through the links below.
Svix
Webhooks as a service
Svix Dispatch sends your webhooks for you: retries with exponential backoff, signed payloads, idempotency keys, and a delivery log your customers can see.
Atomsized
AWS platform engineering and GitOps
Design and automation for reliable AWS and Kubernetes platforms, safer delivery workflows, and preview and UAT environments your engineers can understand and own.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
Can Terraform Watch a Directory for Changes? Working With Dynamic Files
Learn how to handle scenarios where you need Terraform to respond to file changes, and explore alternatives to automatic directory watching.
CI/CD Pipeline Setup Checklist
Step-by-step checklist for a production-ready CI/CD pipeline: source control, builds, tests, security scans, deploy gates, secrets, and rollback paths.
1-2 hours
CI/CD Pipeline Stages
What are the typical stages of a CI/CD pipeline and why is each stage important?
junior