Data sources in Terraform are used to get information about resources external to Terraform, and use them to set up your Terraform resources. For example, a list of IP addresses a cloud provider exposes. Each provider may offer data sources alongside its set of resource types.
Data Resource Behavior
Terraform reads data resources during the planning phase when possible, but announces in the plan when it must defer reading resources until the apply phase to preserve the order of operations. Terraform defers reading data resources in the following situations:
At least one of the given arguments is a managed resource attribute or other value that Terraform cannot predict until the apply step.
The data resource depends directly on a managed resource that itself has planned changes in the current plan.
The data resource has custom conditions and it depends directly or indirectly on a managed resource that itself has planned changes in the current plan.
Local-only Data Sources
While many data sources correspond to an infrastructure object type that is accessed via a remote network API, some specialized data sources operate only within Terraform itself, calculating some results and exposing them for use elsewhere.
For example, local-only data sources exist for rendering templates, reading local files, and rendering AWS IAM policies.
The behavior of local-only data sources is the same as all other data sources, but their result data exists only temporarily during a Terraform operation, and is re-calculated each time a new plan is created.
Refreshing data sources
By default, Terraform will refresh all data sources before creating a plan. You can also explicitly refresh all data sources by running terraform refresh.
Occasionally you’ll have data sources that change very often and would like to keep your resources in sync with those changes. The easiest way to achieve this is to just run Terraform every few minutes or so, do a refresh, and apply all resulting changes.
With this let's conclude the post here.
Thank you for reading!
*** Explore | Share | Grow ***
Comments