DEV Community

Nandan K
Nandan K

Posted on

Terraform Data Sources

🧠 Hardcoding the values in code is breach of security and reusing hardcoded values again and again is not a good practice. So today I come across an interesting topic i.e. "Data Sources".

🎯 Data Sources in terraform allows us to read existing information from your cloud provider without creating anything and also minimises the use of hardcoded values in terraform code.

📌 Why Do We Use Data Sources?

  1. To get IDs of existing VPCs, subnets, AMIs, security groups
  2. To fetch latest AMI dynamically
  3. To use existing infrastructure in new Terraform code
  4. To avoid hardcoding values

💡 Let's take an example, we have shared VPC with multiple teams using it and has 2 subnet. We want to provision 2 EC2 instances in each subnet by giving reference of existing VPC and subnet and also use pre-existing Linux image while creating EC2 instances by using data sources.

My hands-on demo's:

Top comments (0)