2022-10-09

Terraform Resource and Data

Introduction

Terraform is a popular infrastructure-as-code tool that allows you to define, provision, and manage your infrastructure resources on various platforms. The core component of Terraform is the resource block, which is used to define the desired state of a specific resource in a particular provider.

Additionally, Terraform Data is used to retrieve information from existing infrastructure resources.

This article explains about Terraform Resource and Data in detail.

Terraform Resources

The core component of Terraform is the resource block, which is used to define the desired state of a specific resource in a particular provider.

Understanding Resource Blocks

A resource block in Terraform is a configuration block that defines a specific infrastructure object, such as a virtual machine, a database, or a load balancer. It specifies the desired state of the object and the provider-specific details required to create and manage it. The resource block is defined using the following syntax:

resource "provider_type" "resource_name" {
  argument1 = value1
  argument2 = value2
  ...
}

The provider_type parameter specifies the name of the provider, such as AWS or Google, while the resource_name parameter specifies a name for the resource. The argument parameters are used to define the desired state of the resource.

Resource Configuration Syntax

The configuration syntax for resource blocks is provider-specific and depends on the type of resource being created. For example, let's consider a simple resource block for creating an EC2 instance in AWS:

resource "aws_instance" "example" {
  ami = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "example-instance"
  }
}

In this example, aws_instance is the provider type, while example is the resource name. The ami and instance_type parameters are used to specify the desired state of the EC2 instance. The tags parameter is used to add metadata to the instance.

Resource Dependencies and Order of Execution

Resources in Terraform can depend on each other, meaning that the creation of one resource may require the existence of another resource. Terraform automatically determines the order of resource creation based on their dependencies. If a resource depends on another resource that has not yet been created, Terraform will wait until the dependency is created before creating the dependent resource.

For example, let's consider the resource block for creating a security group in AWS:

resource "aws_security_group" "example" {
  name_prefix = "example-"
  description = "Example security group"
  vpc_id = aws_vpc.example.id

  ingress {
    from_port = 0
    to_port = 65535
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

In this example, aws_vpc.example.id is used to define a dependency on a previously created VPC resource. Terraform ensures that the VPC resource is created before the security group is created.

Terraform Data Sources

Terraform Data is used to retrieve information from existing infrastructure resources.

Understanding Data Sources

A data source in Terraform is a configuration block that allows you to retrieve information from an existing infrastructure resource, such as an EC2 instance or a security group. Data sources are defined using the following syntax:

data "provider_type_resource_type" "data_source_name" {
  argument1 = value1
  argument2 = value2
  ...
}

The provider_type parameter specifies the name of the provider, such as AWS or Google, while the resource_type parameter specifies the type of resource to retrieve data from. The data_source_name parameter specifies a name for the data source. The argument parameters are used to define the data to retrieve.

Data Configuration Syntax

The configuration syntax for data sources is provider-specific and depends on the type of resource being retrieved. For example, let's consider a simple data source for retrieving information about an EC2 instance in AWS:

data "aws_instance" "example" {
  instance_id = "i-0123456789abcdef0"
}

In this example, aws_instance is the provider type, while example is the data source name. The instance_id parameter is used to specify the ID of the EC2 instance to retrieve information about.

Data Dependencies and Order of Execution

Data sources in Terraform can depend on other resources, meaning that the creation of a data source may require the existence of another resource. Terraform automatically determines the order of data source creation based on their dependencies. If a data source depends on another resource that has not yet been created, Terraform will wait until the dependency is created before creating the data source.

For example, let's consider a data source that retrieves information about a VPC in AWS:

data "aws_vpc" "example" {
  id = aws_subnet.example.vpc_id
}

In this example, aws_subnet.example.vpc_id is used to define a dependency on a previously created subnet resource. Terraform ensures that the subnet resource is created before the data source is created.

Using Data Outputs in Resource Blocks

Terraform allows you to use data outputs in resource blocks to reference the data retrieved by a data source. For example, let's consider a resource block that creates an EC2 instance in AWS and references the VPC ID retrieved by a data source:

resource "aws_instance" "example" {
  ami = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id = aws_subnet.example.id

  tags = {
    Name = "example-instance"
  }
}

data "aws_vpc" "example" {
  id = aws_subnet.example.vpc_id
}

output "vpc_id" {
  value = data.aws_vpc.example.id
}

In this example, aws_subnet.example.id is used to specify the subnet ID for the EC2 instance. The data.aws_vpc.example.id is used to reference the VPC ID retrieved by the data source. The output block is used to define a named output for the VPC ID.

Summary

Terraform is a popular infrastructure as code tool that enables developers to define and manage cloud infrastructure resources using a declarative approach. The tool is built on two key components: resources and data sources.

Resources are used to define the desired state of a specific resource in a particular provider, such as a virtual machine or a load balancer. Resource blocks are defined using the provider-specific syntax and can depend on each other. Terraform automatically determines the order of resource creation based on their dependencies, ensuring that the creation of one resource does not require the existence of another resource.

Data sources, on the other hand, are used to retrieve information from existing infrastructure resources, such as an EC2 instance or a security group. Data sources are defined using a provider-specific syntax and can depend on other resources. Terraform determines the order of data source creation based on their dependencies, ensuring that a data source creation does not require the existence of another resource.

Data outputs can be used in resource blocks to reference the data retrieved by a data source, enabling developers to retrieve and use information from existing infrastructure resources in resource creation.

By understanding Terraform resources and data sources, developers can define and manage infrastructure resources efficiently and reliably, automating the provisioning, configuration, and management of cloud infrastructure resources.

References

https://developer.hashicorp.com/terraform/language/resources
https://developer.hashicorp.com/terraform/language/data-sources

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!