What is a Data Source in Terraform?

Tarun PandeyTarun Pandey
2 min read

In Terraform, a data source allows you to fetch or reference information from existing resources without creating or modifying them.

This is useful when you need to retrieve details about resources that were not provisioned by Terraform or were created in a different module or workspace.

For eg. If we want to attach an internet gateway to an existing VPC that we have created manually in AWS account ,we can do this with the help of data source in terraform.Following are the steps for the example :

Steps:

1.) look for the datasource in the aws terraform datasource documentation :

2.) Create a vpc in aws account give it a name eg. datasource .

3.) Also look for the IGW in the aws terraform internet gateway documentation :

4.) Create a terraform file eg. datasource.tf and provide for the code .Remember to add data. in from of the vac_id in resource IGW block.See the highlighted section in the image below .Also change the name of the resouces as per you wish.

5.) Run the terraform plan and apply commands :

6.) Look for the IGW in you aws account ,an IGW with the name datasource has been created and attached with the same vpc_id.

In this way using data source in terraform you can attach various resources.

Hope you find something valuable.

Keep learning and connect me on LinkedIn

0
Subscribe to my newsletter

Read articles from Tarun Pandey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tarun Pandey
Tarun Pandey

Hey there! Great to see you here. I’m actively exploring new technologies in DevOps and sharing my insights through articles. If you find something valuable, feel free to check out my other profiles and show some love! Take care, and see you on the other side! 🚀