...
/Create and Differentiate Resource and Data Configurations
Create and Differentiate Resource and Data Configurations
Learn about data sources and resources and their data configurations.
Most providers in Terraform are composed of resources and data sources. Data sources are existing resources or information available from the provider for use within the configuration.
Data sources
Data sources are simply sources of information that we can retrieve from a provider. For example, maybe we want to retrieve an AMI ID for a specific version of Ubuntu in the AWS us-east-1
region. Or, perhaps we need to get the VNet ID where we’ll deploy some Azure virtual machines. Those are two examples of a data source.
Configuration of data sources
Most data sources require that we provide some configuration data to get the relevant information we want. They also assume that we’ve correctly configured the provider for our data source. Let’s take a look at the AWS AMI example:
data "aws_ami" "ubuntu" {most_recent = truefilter {name = "name"values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]}filter {name = "virtualization-type"values = ["hvm"]}owners = ["099720109477"] # Canonical}
The object type label for a data
source is data, followed by the data source type label (aws_ami
) and a name label for the data source (ubuntu
). We’re supplying arguments to get the Ubuntu image for 16.04
. Each data source has arguments required to retrieve the data and a set of attributes that are available once the data is retrieved. The aws_ami
data source has an attribute called image_id
, which can be used to create an EC2 instance using that AMI ID. We’ll go over data source and resource addressing in the next ...