Working with Resources
1. Creating Resources
Resources in Terraform are the fundamental building blocks of infrastructure. Each resource is defined using a resource block.
| Resource Type | Identifier | Attributes | Description |
|---|---|---|---|
| resource | aws_instance.my_instance |
ami, instance_type |
Defines an EC2 instance in AWS |
Example: Creating an AWS Instance
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
}
2. Referencing Resource Attributes
Resource attributes can be referenced using the dot syntax resource_type.resource_name.attribute.
| Reference Format | Example | Description |
|---|---|---|
| resource_type.resource_name.attribute | aws_instance.my_instance.id |
References the ID of an EC2 instance |
Example: Using Resource Attributes in Variables
variable "instance_id" {
description = "ID of the EC2 instance"
default = aws_instance.my_instance.id
}
3. Using Resource Dependencies
Terraform automatically handles dependencies between resources based on references. Explicit dependencies can also be defined using the depends_on argument.
| Attribute | Values | Description |
|---|---|---|
| depends_on | [ aws_security_group.main ] |
Ensures this resource is created after the security group |
Example: Defining Explicit Dependencies
resource "aws_instance" "web_server" {
depends_on = [ aws_security_group.main ]
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
}
4. Setting Resource Timeouts
The timeouts block allows you to configure how long Terraform waits for operations to complete.
| Timeout Type | Default Value | Description |
|---|---|---|
| create | 10m |
Time to wait for resource creation |
| update | 10m |
Time to wait for resource update |
| delete | 10m |
Time to wait for resource deletion |
Example: Setting Custom Timeouts
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
timeouts {
create = "20m"
delete = "15m"
}
}
5. Configuring Lifecycle Rules
The lifecycle block allows you to control how Terraform handles resource creation, updates, and destruction.
| Rule | Values | Description |
|---|---|---|
| ignore_changes | [ tags ] |
Prevents changes to specific attributes from causing updates |
| create_before_destroy | true |
Ensures a new resource is created before destroying the old one |
| prevent_destroy | true |
Prevents destruction of the resource |
Example: Using Lifecycle Rules
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
lifecycle {
ignore_changes = [ tags ]
create_before_destroy = true
}
}
6. Preventing Resource Destruction
The prevent_destroy lifecycle rule prevents Terraform from destroying a resource.
| Attribute | Values | Description |
|---|---|---|
| prevent_destroy | true |
Prevents destruction of the resource |
Example: Preventing Resource Destruction
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
lifecycle {
prevent_destroy = true
}
}
7. Ignoring Attribute Changes
The ignore_changes lifecycle rule allows you to ignore changes to specific attributes.
| Attribute | Values | Description |
|---|---|---|
| ignore_changes | [ tags, size ] |
Prevents changes to these attributes from causing updates |
Example: Ignoring Attribute Changes
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
lifecycle {
ignore_changes = [ tags, size ]
}
}
8. Creating Resources Before Destroying
The create_before_destroy lifecycle rule ensures a new resource is created before the old one is destroyed.
| Attribute | Values | Description |
|---|---|---|
| create_before_destroy | true |
Ensures a new resource is created before destroying the old one |
Example: Creating Resources Before Destroying
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
lifecycle {
create_before_destroy = true
}
}
9. Replacing Matched Resources
The replace_triggered_by lifecycle rule allows you to trigger resource replacement when a specific condition is met.
| Attribute | Values | Description |
|---|---|---|
| replace_triggered_by | [ aws_s3_bucket.my_bucket ] |
Triggers replacement when the specified resource changes |
Example: Replacing Matched Resources
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
lifecycle {
replace_triggered_by = [ aws_s3_bucket.my_bucket ]
}
}
10. Using Resource Preconditions
Preconditions allow you to validate resource inputs before creation using precondition.
| Attribute | Values | Description |
|---|---|---|
| precondition | condition = var.instance_type != "" |
Ensures the instance type is not empty |
Example: Using Resource Preconditions
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
lifecycle {
precondition {
condition = var.instance_type != ""
error_message = "Instance type must not be empty"
}
}
}
11. Using Resource Postconditions
Postconditions allow you to validate resource outputs after creation using postcondition.
| Attribute | Values | Description |
|---|---|---|
| postcondition | condition = self.id != "" |
Ensures the instance ID is not empty |
Example: Using Resource Postconditions
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
lifecycle {
postcondition {
condition = self.id != ""
error_message = "Instance ID must not be empty"
}
}
}
12. Replacing Resources
The replace_triggered_by lifecycle rule allows you to trigger resource replacement when a specific condition is met.
| Attribute | Values | Description |
|---|---|---|
| replace_triggered_by | [ aws_s3_bucket.my_bucket ] |
Triggers replacement when the specified resource changes |