Reputation: 10487
The Terraform Data Sources documentation tells me what a data source is, but I do not quite understand it. Can somebody give me a use case of data source? What is the difference between it and configuring something using variables?
Upvotes: 115
Views: 144793
Reputation: 1
In simple term, think of data source as a messenger that terraform sends to get information about a pre-existing resource created manually, or managed by another terraform in your cloud provider. Then brings the information so that terraform can reference that in the configuration files. Example, you want create a EC2 instance in AWS cloud, but it has to be in a particular VPC which was created manually on the AWS console. Now terraform will use data source to get information about the network so that it make use of it to deploy the EC2 in that VPC.
Upvotes: 0
Reputation: 23
One really useful way to use a data source is to reference a resource created in another workspace.
This can create a lot of WET code, though! If you change the referenced resource name, you'll have to update all the data sources that reference it.
If you use a tool like Metastructure, you can drive both the original resource AND the derived data source from a common config, which will eliminate the problem & DRY up your code.
Upvotes: 0
Reputation: 916
Data sources are used to fetch the data from the provider end, so that it can be used as configuration in .tf files instead of hardcoding it.
Example: Below code fetches the AWS AMI ID and uses it to launch AWS instance.
data "aws_ami" "std_ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
resource "aws_instance" "myec2" {
ami = data.aws_ami.std_ami.id
instance_type = "t2.micro"
}
Upvotes: 13
Reputation: 259
The main difference between a Terraform data source, resource and variable is:
Resource: Provisioning of resources/infra on our platform. Create, Update and delete!
Variable: Provides predefined values as variables on our IAC. Used by resource for provisioning.
Data Source: Fetch values from our infra/provider and provides data for our resource to provision infra/resource.
Upvotes: 24
Reputation: 12657
Data sources provide information about entities that are not managed by the current Terraform configuration.
This may include:
In other words, data sources are read-only views into the state of pre-existing components external to our configuration.
Once you have defined a data source, you can use the data elsewhere in your Terraform configuration.
For example, let's suppose we want to create a Terraform configuration for a new AWS EC2 instance. We want to use an AMI image which were created and uploaded by a Jenkins job using the AWS CLI, and not managed by Terraform. As part of the configuration for our Jenkins job, this AMI image will always have a name with the prefix app-
.
In this case, we can use the aws_ami
data source to obtain information about the most recent AMI image that has the name prefix app-
.
data "aws_ami" "app_ami" {
most_recent = true
filter {
name = "name"
values = ["app-*"]
}
}
Data sources export attributes, just like resources do. We can interpolate these attributes using the syntax data.TYPE.NAME.ATTR
. In our example, we can interpolate the value of the AMI ID as data.aws_ami.app_ami.id
, and pass it as the ami
argument for our aws_instance
resource.
resource "aws_instance" "app" {
ami = "${data.aws_ami.app_ami.id}"
instance_type = "t2.micro"
}
Data sources are most powerful when retrieving information about dynamic entities - those whose properties change value often. For example, the next time Terraform fetches data for our aws_ami
data source, the value of the exported attributes may be different (we might have built and pushed a new AMI).
Variables are used for static values, those that rarely changes, such as your access and secret keys, or a standard list of sudoers for your servers.
Upvotes: 67
Reputation: 6494
Data sources can be used for a number of reasons; but their goal is to do something and then give you data.
Let's take the example from their documentation:
# Find the latest available AMI that is tagged with Component = web
data "aws_ami" "web" {
filter {
name = "state"
values = ["available"]
}
filter {
name = "tag:Component"
values = ["web"]
}
most_recent = true
}
This uses the aws_ami data source - this is different than a resource! It will instead just give you information, and not create anything. This example in particular will call out to the describe-images
AWS API call, pass in a few --filter
options as specified, and return an object that you can get information from - take a look at these attributes!
... The list goes on. This is really useful if I were, let's say - always wanting to pull the latest AMI matching some tags, and keep a launch configuration up to date with it. I could use this data provider rather than always have to update a variable or hard-code the ID.
Data source can be used for other reasons as well; one of my favorites is the template provider.
Good luck!
Upvotes: 120