Skip to content

Instantly share code, notes, and snippets.

@RajChowdhury240
Created March 12, 2025 14:25
Show Gist options
  • Save RajChowdhury240/8f6baac853c9d6e9536880c770c67d48 to your computer and use it in GitHub Desktop.
Save RajChowdhury240/8f6baac853c9d6e9536880c770c67d48 to your computer and use it in GitHub Desktop.

Terraform Refresh vs Terraform Import

Below is a comparison of terraform refresh and terraform import, presented in both bullet-point and tabular form.


Bullet Point Comparison

terraform refresh

  • Purpose: Updates the Terraform state file with the current state of existing resources in the configured providers.
  • Usage: Typically used to ensure that your local state file accurately reflects what’s actually deployed.
  • No New Resource: Does not import new resources that are missing from the state; it only refreshes existing entries in the state.
  • Impact on Configuration: Does not change or generate configuration; it merely refreshes the state.
  • Common Use Cases:
    • After manual changes were made to cloud infrastructure
    • To verify that the Terraform state is still in sync with the real-world environment

terraform import

  • Purpose: Brings an existing resource under Terraform’s management by adding it to the state file.
  • Usage: Requires specifying the resource name and identifier (ID) to import into Terraform.
  • State Update: Creates a new entry in the Terraform state for that resource.
  • Impact on Configuration: Terraform does not generate configuration files for the resource automatically—you must still write or adapt the resource definition in code.
  • Common Use Cases:
    • Migrating resources created outside of Terraform into Terraform management
    • Aligning already-deployed infrastructure with newly written Terraform configurations

Tabular Comparison

Aspect terraform refresh terraform import
Primary Objective Update the existing Terraform state from actual resource states Bring an existing (outside Terraform) resource into Terraform’s state
Effect on State Refreshes the state of already-known resources Creates or adds new entries for previously untracked resources
Effect on Configuration No direct effect on resource blocks in .tf files No automatic code generation; you must still have or create the relevant .tf definitions
Command Arguments Generally runs without arguments (just terraform refresh) Requires resource addresses and IDs, e.g. terraform import aws_instance.my_ec2 i-12345678
Usage Frequency Often used during normal workflow to keep state in sync Used when adopting existing resources into Terraform
Dependencies Requires existing resources in the state to refresh Requires correct resource identifier and a matching resource block in .tf (or to be created)
Typical Outcome Updated state file to match real infrastructure Resource state file entry created for a previously unmanaged resource

Key Takeaways

  • Use terraform refresh to keep your known resources’ state accurate.
  • Use terraform import to start managing existing resources that Terraform doesn’t currently track in its state.
  • Neither command automatically generates or updates your Terraform configuration (.tf files) beyond state changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment