Skip to content

Commit 2bb2893

Browse files
feat: add terraform refresh command (#33)
## what - Add `terraform refresh` in our task commands so it's similar to `terraform [plan | apply]` ## why - There are situations where we need `terraform refresh` while migrating state from local to a remote backend. ## references - masterpointio/mp-infra#211 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Added a Terraform task to refresh state for a selected environment. - Uses environment-specific variables file and supports additional Terraform arguments. - Includes a pre-run check to ensure the variables file exists. - Behavior aligns with existing Terraform tasks for a consistent workflow. - Runs quietly in the current working directory to reduce noise. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent f11cee7 commit 2bb2893

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/terraform/Taskfile.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,20 @@ tasks:
7474
msg: "Variables file does not exist: {{.TFVARS_FILE}}"
7575
cmds:
7676
- terraform apply -var-file {{.TFVARS_FILE}} {{.TF_ARGS}}
77+
78+
refresh:
79+
desc: Refresh the Terraform state to match the real-world infrastructure (safer via apply -refresh-only).
80+
summary: |
81+
Refreshes the Terraform state for a specified environment, using a file to load the variables.
82+
Note: Upstream deprecates `terraform refresh`; use `terraform apply -refresh-only` to review changes before writing state.
83+
Requires a variables file specific to the environment to be present.
84+
Usage: task terraform:refresh -- ENVIRONMENT [terraform apply -refresh-only arguments]
85+
Example: task terraform:refresh -- automation
86+
dir: "{{.USER_WORKING_DIR}}"
87+
silent: true
88+
vars: *vars
89+
preconditions:
90+
- sh: test -f {{.TFVARS_FILE}}
91+
msg: "Variables file does not exist: {{.TFVARS_FILE}}"
92+
cmds:
93+
- terraform apply -refresh-only -var-file {{.TFVARS_FILE}} {{.TF_ARGS}}

0 commit comments

Comments
 (0)