How are Data Sources Useful?

In this lesson, we'll discuss Terraform data source advantages.

Terraform data source advantages#

As your Terraform project grows, it can be sensible to break it up into smaller projects to make for easier maintenance. When this happens, you can use data sources to reference and utilize resources across Terraform projects. In this case, it would always be better to use a data source rather than to compute the arn yourself, as with something like an S3 bucket. This is because you want Terraform to fail if, for some reason, the bucket no longer exists. By using a data source, you maintain this behavior.

As your Terraform project grows it can be sensible to break it up into smaller projects to make for easie maintainance

Example#

Created with Fabric.js 3.6.6 Imagine you want to create a new AWS EC2 instance using an AMI image from a private repository.
1 of 5

Another advantage#

Another case when you may want to use a data source is if you are migrating existing infrastructure to Terraform and want to reference a resource that is not part of your Terraform project yet. As previously stated, it is always better to use a data source rather than compute the value yourself. You want Terraform to know that there is a dependency on the resource so that your terraform apply will fail if the resource cannot be found or if the attribute it returns changes. Terraform will realise this when you run “apply” and update your project with the new value.

Another use for data sources is when you are migrating existing infrastructure to Terraform and want to reference a resource that is not part of your Terraform project yet.
Data Sources in Detail
🎉Quiz
Mark as Completed
Report an Issue