Resource blocks are the fundamental building blocks in Terraform that define infrastructure components you want to create, modify, or manage. They tell Terraform what resources to provision in your target infrastructure platform.
The basic syntax of a resource block follows this structure:
resour…Resource blocks are the fundamental building blocks in Terraform that define infrastructure components you want to create, modify, or manage. They tell Terraform what resources to provision in your target infrastructure platform.
The basic syntax of a resource block follows this structure:
resource "<PROVIDER_TYPE>" "<LOCAL_NAME>" {
argument1 = value1
argument2 = value2
}
The resource block consists of several key components:
1. **Resource Keyword**: The block starts with the keyword 'resource' to indicate you are declaring an infrastructure resource.
2. **Resource Type**: This is a two-part identifier combining the provider name and the resource type (e.g., 'aws_instance', 'azurerm_virtual_network'). The prefix before the underscore indicates the provider.
3. **Local Name**: A unique identifier within your Terraform configuration that you assign to reference this specific resource elsewhere in your code. It must be unique among resources of the same type within a module.
4. **Configuration Arguments**: Inside the curly braces, you define arguments specific to that resource type. These can include required and optional parameters that configure the resource's properties.
Example:
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
}
}
Resources can also contain:
- **Meta-arguments**: Special arguments like 'depends_on', 'count', 'for_each', 'provider', and 'lifecycle' that modify resource behavior.
- **Nested blocks**: Some resources require nested configuration blocks for complex settings.
To reference a resource attribute elsewhere in your configuration, use the syntax: <RESOURCE_TYPE>.<LOCAL_NAME>.<ATTRIBUTE>
For example: aws_instance.web_server.public_ip
Understanding resource block syntax is essential for the Terraform Associate exam as it forms the foundation for all infrastructure provisioning with Terraform.
Resource Blocks and Syntax in Terraform
Why Resource Blocks Are Important
Resource blocks are the fundamental building blocks of Terraform configurations. They define the infrastructure components you want to create, modify, or manage. Understanding resource block syntax is essential for the Terraform Associate exam because nearly every Terraform configuration relies on properly structured resource blocks to provision infrastructure.
What Are Resource Blocks?
A resource block declares a resource of a specific type with a specific local name. The combination of resource type and name must be unique within a module. Resources represent infrastructure objects such as virtual networks, compute instances, DNS records, or higher-level components like DNS providers.
1. resource - The keyword that identifies this as a resource block
2. Resource Type - The first string after the resource keyword (e.g., "aws_instance", "azurerm_virtual_network"). This consists of a provider prefix and the resource name separated by an underscore.
3. Local Name - The second string that serves as an identifier for this specific resource within the Terraform module. This name is used to reference the resource elsewhere in the configuration.
4. Configuration Block - The curly braces contain arguments that configure the resource.
How Resource Blocks Work
When Terraform processes a configuration:
- It reads all resource blocks and determines the desired state - It compares the desired state with the current state stored in the state file - It creates an execution plan to reach the desired state - Upon apply, it makes API calls to the provider to create, update, or delete resources
Resource Arguments and Meta-Arguments
Resources support two types of arguments:
Regular Arguments: These are specific to the resource type and define its configuration (e.g., ami, instance_type for aws_instance).
Meta-Arguments: These work with any resource type and include: - depends_on - Specifies explicit dependencies - count - Creates multiple instances of a resource - for_each - Creates multiple instances based on a map or set - provider - Selects a non-default provider configuration - lifecycle - Customizes resource lifecycle behavior
Referencing Resources
Resources can be referenced using the syntax: resource_type.local_name.attribute
For example: aws_instance.web_server.public_ip
Exam Tips: Answering Questions on Resource Blocks and Syntax
1. Memorize the syntax pattern: Know that resource blocks start with the keyword "resource" followed by type in quotes, then name in quotes, then curly braces.
2. Understand the resource type naming convention: Resource types always include a provider prefix (aws_, azurerm_, google_) followed by the resource name.
3. Know the difference between resource type and local name: The type determines what kind of infrastructure is created; the local name is your reference identifier.
4. Be familiar with meta-arguments: Questions often test whether you know which meta-arguments are available and their purposes.
5. Recognize valid vs invalid syntax: Watch for missing quotes around type or name, missing equals signs, or incorrect block structure.
6. Understand resource references: Know how to correctly reference resource attributes in other parts of the configuration.
7. Remember uniqueness requirements: The combination of resource type and local name must be unique within a module.
8. Practice identifying providers from resource types: If you see "aws_" you know it is an AWS provider resource; "azurerm_" indicates Azure Resource Manager.