In this lesson, we explore how Terraform utilizes data sources to read information from resources that are managed externally. Terraform leverages configuration files and a state file to provision infrastructure resources, but it can also interact with resources that are created manually, by other tools (such as Puppet, CloudFormation, SaltStack, Ansible, etc.), ad-hoc scripts, or even resources provisioned by another Terraform configuration. For instance, consider a database instance that was manually provisioned in the AWS cloud. Even though Terraform does not manage this resource, it can still read attributes—such as the database name, host address, or DB user details—and use that information to provision an application resource managed by Terraform.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.

/root/pets.txt and stores its information in the state file. Meanwhile, another file—created by an external shell script—is located at /root/dog.txt and contains “Dogs are awesome!” Because dog.txt is not managed by Terraform, we can use it as a data source to supply content for our managed resource pets.txt.
Data sources in Terraform enable you to use attributes from external resources, integrating them into your Terraform-managed infrastructure.
data block in your configuration. Although these blocks resemble resource blocks, they start with the keyword data instead of resource. Below is an example configuration that demonstrates this process:
- The resource block creates a file called
/root/pets.txtwith its content dynamically populated using data fromdog.txt. - The data block reads from the local file located at
/root/dog.txtand makes its content available via the expressiondata.local_file.dog.content.
- The raw content of the file.
- The base64-encoded version of the file’s content.

| Type | Purpose | Management by Terraform |
|---|---|---|
| Resource | Create, update, and destroy infrastructure elements | Managed; stored in the state file |
| Data Source | Read and reference information from unmanaged resources | Not managed; used for reference within Terraform |
