
In this blog we are directly move on the terraform data source that what exactly data source in terraform . If you have no idea about terraform you can refer this blog . So let’s get started
What is data source ?
Lets take an example , suppose you are a developer and you are working on below terraform script
resource "aws_instance" "ec2_eaxmple" {
ami = "ami-005e54dee72cc1d00"
instance_type = "t2.micro"
tags {
Name = "Terraform EC2"
}
This is your terraform script for starting your ec2 instance on aws. In which we specified ami , instance_type and tags “Terraform EC2” You can see we have specified all input instruction. Now what if you want to get back any information from your aws environments.
Solution
So if you want to get the public_ip , instance_id and tags of your instance you have use the terraform data-source. so you can simply say , just go back to your browser , go to or login to your aws console anf drom there you can fetch you instance-id, and tags. Thats the one way of fetching the information. But what if i want fetch this information only using the terraform script. So here you can say this is the terraform script for creating a resorce of type ec2, this is just an input instaruction for aws environment for creating ec2-instance.But this input instruction doesn’t return back you anything.
So if you want to get back any information from your aws consolethen you need to create a data sources. If you create a data souce in terraform then you can get the info back in the form of public-ip, instance-id and tags. A simple resource of aws can contain a lot of attributes. As our scripts only containes three attribute we can get them back, but you can also get many more attribute as per your requirement with the help of data source in terraform.
Now lets see the How to create a Data Source
data "aws_instance" "myawsinstance" {
filter {
name = "tag:Name"
values = ["Terraform EC2"]
}
depends_on = [
"aws_instance.ec2_eaxmple"
]
}
output "fetched_from_aws" {
values = data.aws_instance.myawsinstance.public_ip
}
So data block refers to data sources and output block refers to oututs which you will get from aws. Lets start with our data block . So here you can see i have used the resource aws_instance . What dies it mean, since we are working on aws so the the data source which i want to create is of type aws_instance type. Second we need to provide the name of my dta source i.e “myawsinstance” you can chage it as well as your requirement. Second thing we need to define is filter, assume that you have multiple instances in your aws enviromnet , and you some info back from aws then which one to choose for that reasion we defined filter i.e we choose the instance which has tag “Terraform EC2”. And if you see the the first code of resource “ec2-instance” the tag we gave is “Terraform EC2”
Thirdly we need to define the dependency , but why do we need dependency, so in a single terraform file you will have a resource block where you ahve created a ec2-instance, so we need to find the info return back of this ec2-instance , so we need to define the dependency of this resource block. So just remember the name of resource block i.e aws_instance and ec2_example. We defined this dependency as shown above. because of the dependency the data souce is going to know that from which instanch we need to fetch the data.
Now we have defined the data source. Now see the output block. The name of the output block would be any as per your choice. Second we ned to define the value that what kind of info you want to get back. so i want a get back public_ip. So i get that with the help of “values = data.aws_instance.myawsinstance.public_ip”.
So this how we create data source to get some information back from aws with the help of terraform.
Demo
Now we have a below file i.e main.tf. Alright now I am assuming you have gone through all the above content so here is our final terraform configuration including aws_instance, data source, and output values
provider "aws" {
region = "eu-central-1"
access_key = "AKIATQ37NXB2JMXVGYPG"
secret_key = "ockvEN1DzYynDuKIh56BVQv/tMqmzvKnYB8FttSp"
}
resource "aws_instance" "ec2_example" {
ami = "ami-005e54dee72cc1d00"
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_from_aws" {
value = data.aws_instance.myawsinstance
}
You can simply run the following terraform command to create your aws_instance –
- terraform init
- terraform plan
- terraform apply
Here is the output –
Outputs:
fetched_from_aws = {
"ami" = "ami-ami-005e54dee72cc1d00"
"arn" = "arn:aws:ec2:eu-central-1:242396018804:instance/i-0eda1c6a59790eb7d"
"associate_public_ip_address" = true
"availability_zone" = "eu-central-1c"
"credit_specification" = tolist([
{
"cpu_credits" = "standard"
},
])
"disable_api_termination" = false
"ebs_block_device" = toset([])
"ebs_optimized" = false
"enclave_options" = tolist([
{
"enabled" = false
},
])
"ephemeral_block_device" = tolist([])
"filter" = toset([
{
"name" = "tag:Name"
"values" = tolist([
"Terraform EC2",
])
},
])
"get_password_data" = false
"get_user_data" = false
"host_id" = tostring(null)
"iam_instance_profile" = ""
"id" = "i-0eda1c6a59790eb7d"
"instance_id" = tostring(null)
"instance_state" = "running"
"instance_tags" = tomap(null) /* of string */
"instance_type" = "t2.micro"
"key_name" = ""
"metadata_options" = tolist([
{
"http_endpoint" = "enabled"
"http_put_response_hop_limit" = 1
"http_tokens" = "optional"
},
])
"monitoring" = false
"network_interface_id" = "eni-0ffc9d62eafcafcbc"
"outpost_arn" = ""
"password_data" = tostring(null)
Here is the Screenshot

Fetching only specific attribute using data source
output "fetched_info_from_aws" {
value = data.aws_instance.myawsinstance.public_ip
}
Here is the public_ip
information fetched by the data source –

Proudly powered by WordPress