How to use Data Source in Terraform?

data codes through eyeglasses
Reading Time: 3 minutes

In this blog, we will be learning about what is data source and how to use it in terraform.

What is Data Source?

Data source in terraform relates to resources but only it gives the information about an object rather than creating one. It provides dynamic information about the entities we define outside of terraform.
Data Sources allow fetching data about the infrastructure components’ configuration. It allows to fetch data from the cloud provider APIs using terraform scripts.
When we refer to a resource using a data source, it won’t create the resource. Instead, they get information about that resource so that we can use it in further configuration if required.

How to use Data Source?

For example, we will create an ec2 instance using a vpc and subnet, both of which are created on aws console that is external to terraform configuration.

Step 1: Create a terraform directory and create a file named provider.tf in it. Below code represents the details of the aws provider that we’re using, like its region, access key and secret key.

provider "aws"{
  region     = "us-east-1"
  access_key = "your_access_key"
  secret_key = "your_secret_key"
}

Step 2: In that directory, create another file named demo_datasource.tf and use the code given below.

data "aws_vpc" "vpc" {
  filter {
    name = "tag:Name"
    values = ["vpc"]
  }
}

data "aws_subnet" "subnet" {
  filter {
    name = "tag:Name"
    values = ["subnet"]
  }
}

resource "aws_security_group" "sg" {
  name = "sg"
  vpc_id = data.aws_vpc.vpc.id
 ingress                = [
   {
     cidr_blocks      = [ "0.0.0.0/0"]
     description      = ""
     from_port        = 22
     ipv6_cidr_blocks = []
     prefix_list_ids  = []
     protocol         = "tcp"
     security_groups  = []
     self             = false
     to_port          = 22
  }
  ]
  egress = [
    {
      cidr_blocks      = [ "0.0.0.0/0"]
      description      = ""
      from_port        = 0
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      protocol         = "-1"
      security_groups  = []
      self             = false
      to_port          = 0
    }
  ]
}

resource "aws_instance" "proj-instance" {
  ami ="ami-2757f631"
  instance_type = "t2.micro"
  key_name= "aws_key"
  vpc_security_group_ids = [ aws_security_group.sg.id ]
  subnet_id =  data.aws_subnet.subnet.id
  tags = {
    Name = "DataSource- Instance"
  }
}

In the above block of code, we are using a vpc and a subnet that is already created on AWS using its console. Then using data block, which refers to data sources, that is, a vpc and a subnet. By doing this, we are retrieving the information about the vpc and subnet that are created outside of terraform configuration. Then creating a security group that uses vpc_id that was fetched using data block. Further creating the EC2 instance that uses the subnet_id that was also fetched using data block.
So, in this example, data source is being used to get data about the vpc and subnet that were not created using terraform script and using this data further for creating an EC2.

Step 3: After completing the above steps, run the terraform plan and terraform apply commands.

Conclusion

So, we learned about what a data source is and how it can be used in our configuration files. For detailed knowledge, refer to this link.