Running terraform apply on a Schedule with Cron
Run with `-auto-approve` because cron has no TTY to confirm, `cd` into the config directory, use terraform's absolute path, and export the `TF_VAR_*` and cloud credentials cron won't inherit.
The crontab line
0 5 * * * cd /opt/infra && /usr/local/bin/terraform apply -auto-approve >> /var/log/tf.log 2>&1
Without -auto-approve terraform waits for an interactive "yes" that never comes, and the job hangs or aborts.
Credentials and variables
Cron won't load your shell exports. Set cloud creds and variables inline: AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... TF_VAR_region=us-east-1, or source a secrets file before the command.
Lock the state remotely
Scheduled applies and manual applies can collide. Use a remote backend with state locking (S3+DynamoDB, Terraform Cloud) so two runs never corrupt state.
FAQ
How do I run terraform apply from cron?
Use -auto-approve so it doesn't wait for input: 0 5 * * * cd /opt/infra && /usr/local/bin/terraform apply -auto-approve.
Why does my terraform cron job hang?
Without -auto-approve, apply prompts for confirmation and cron has no TTY to answer. Add -auto-approve.
How do I give terraform cloud credentials in cron?
Cron doesn't inherit your shell exports. Set AWS_*/TF_VAR_* inline in the crontab line or source a secrets file first.