How to use Data Sources in Terraform

In this article, we delve into the power of Terraform Data Sources, showcasing their utility with practical examples to dynamically fetch external data in infrastructure as code.

Create a MySQL RDS Database Instance with Terraform 21

What is Terraform?

1280px Terraform Logo.svg removebg preview 1
  • It is an open-source IaaC (Infrastructure as a code) software tool where you define and create resources using providers in the declarative configuration language example JSON.
  • With Terraform, You can package and reuse the code in the form of modules.
  • It supports a number of cloud infrastructure providers such as AWS, Azure, GCP, IBM Cloud, OCI, etc.

What is Data Sources in Terraform?

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. Data sources serve as a bridge between the current infrastructure and the desired configuration, allowing for more dynamic and context-aware provisioning.

For example, a particular IaC repository may have resource declarations for database and storage, while others may have declarations for compute resources. Thus, multiple infrastructure components are provisioned using separate Terraform projects. 

In such cases as well, data sources play a crucial role in sharing the resource information, which is made available only after provisioning is complete.

Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration.

image 55

Difference between terraform variables and terraform datasource

AspectVariablesDatasources
PurposeInput parameters that customize configurationsRetrieve information about external resources
DeclarationDefined using the variable blockDefined using the data block
ScopeLocal to the Terraform configurationGlobal across the Terraform configuration
UsagePassed as arguments to resources and modulesUtilized to query external systems and APIs
Dynamic ValuesCan be assigned dynamically during executionFetches real-time data during execution
DependenciesCan depend on other variables within the scopeCan depend on other datasources and resources
Mutable During ExecutionNo, values are set before execution beginsYes, values can change during execution
Difference between variables and data in Terraform

Difference between terraform resources and terraform data sources

AspectResourcesDatasources
DeclarationDefined using resource blocksDefined using the data block
PurposeCreates, updates, or deletes infrastructureFetches information about external resources
UsageConfigures and manages infrastructure stateQueries external systems and APIs
ScopeTypically within the same Terraform fileCan be used across multiple Terraform files
Dependency ManagementExplicitly declared using depends_onDependencies are managed implicitly
MutabilityMutable – can be modified during executionImmutable – values are read-only during execution
OutputsCan produce outputs for inter-resource communicationPrimarily used for retrieving information
Examplesaws_instance, aws_s3_bucketaws_ami, aws_subnet, external
Difference between resources and data sources in Terraform

Benefits of Utilizing Terraform Data Sources

Benefits of Data Sources: Enhance code reusability by reducing redundancy and promoting maintainability. Efficiently handle external data, fostering a dynamic approach to infrastructure management.

Common Mistakes: Beware of overlooking error handling; validate outputs, implement conditional logic, and rigorously test configurations for robust data source usage.

Real-world Use Cases: Fetch external data for networking or configuration management scenarios, showcasing data sources’ versatility in diverse infrastructure setups.

Security Measures: Prioritize security by avoiding exposure of sensitive information, employing encryption, and implementing access controls to protect Terraform configurations.

Future Developments: Anticipate Terraform’s evolution with enhanced support for cloud providers, improved filtering, and advanced querying capabilities for cutting-edge infrastructure code.

Practical to understand Terraform DataSource

Step1: Create an aws_instance

provider "aws" {
    region     = "ap-south-1"
    profile = "default"
}

resource "aws_instance" "ec2_example" {

    ami           = "ami-03f4878755434977f"
    instance_type =  "t2.micro"

    tags = {
      Name = "Terraform EC2"
    }
}

Step2: Define a data source

data "aws_instance" "myawsinstance" {
    filter {
        name = "tag:Name"
        values = ["Terraform EC2"]
    }

    depends_on = [
      "aws_instance.ec2_example"
    ]
}

Step3: Create Output variable for data source

output "fetched_info_from_aws" {
  value = data.aws_instance.myawsinstance.public_ip
}

Step4: Apply the final terraform configuration

datasource.tf
provider "aws" {
    region     = "ap-south-1"
    profile = "default"
}

resource "aws_instance" "ec2_example" {

    ami           = "ami-03f4878755434977f"
    instance_type =  "t2.micro"

    tags = {
      Name = "Terraform EC2"
    }
}

data "aws_instance" "myawsinstance" {
    filter {
        name = "tag:Name"
        values = ["Terraform EC2"]
    }

    depends_on = [
      "aws_instance.ec2_example"
    ]
}

output "fetched_info_from_aws" {
  value = data.aws_instance.myawsinstance.public_ip
}

Step5: Let’s Deploy by using Terraform Commands

terraform init
terraform plan
terraform apply
terraform destroy

Conclusion

Terraform data sources enable the retrieval of external information to inform infrastructure configuration. By defining a data source block, querying relevant data, and seamlessly integrating it into resource attributes, Terraform provides a dynamic and flexible approach. In practice, using data sources allows for efficient and automated management of diverse infrastructures, enhancing the power of Terraform configurations.

Reference:-

For reference visit the official website .

Any queries pls contact us @Devopshint.

Related Articles:

Terraform State File Management | Terraform Local State File | Terraform Remote

About Akash Bhujbal

Hey, I am Akash Bhujbal, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link