08 - AWS Terraform Resources
The building blocks of Terraform configuration start with defining your provider block to specify the cloud platform, such as AWS. The next step is to define the resources you want to create.
What Are Terraform Resources?
Terraform resources represent the infrastructure objects you want to create, such as:
EC2 Instances
VPCs
Databases
DNS Records
Each resource block in Terraform describes a single infrastructure object. For example, to launch an EC2 instance, you define a resource block just for that. You can declare multiple resource blocks to create various resources within your infrastructure.
Resource Block Structure
A resource block in Terraform has the following structure:
resource "<RESOURCE_TYPE>" "<LOCAL_NAME>" {
# Arguments specific to the resource type
argument_name = "value"
}
<RESOURCE_TYPE>
: Specifies the type of resource to create (e.g.,aws_instance
for an EC2 instance).<LOCAL_NAME>
: A user-defined name to uniquely identify the resource within the module.Arguments: Define the resource's configuration, such as AMI, instance type, and tags.
Example: Creating an EC2 Instance
Hereโs an example of a resource block to create an EC2 instance:
resource "aws_instance" "my_ec2_instance" {
ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
tags = {
Name = "MyFirstInstance"
}
}
Explanation:
resource "aws_instance" "my_ec2_instance"
:The resource type is
aws_instance
(used to create EC2 instances).The local name is
my_ec2_instance
(user-defined and unique within the module).
Arguments:
ami
: Specifies the Amazon Machine Image (AMI) ID for the instance.instance_type
: Defines the instance size (e.g.,t2.micro
).tags
: Adds metadata for the resource.
Understanding Resource Types and Arguments
Each resource block is linked to a specific resource type, which:
Determines the type of infrastructure object it manages.
Defines the available arguments and attributes.
For example:
aws_instance
supports arguments likeami
,instance_type
,availability_zone
, and more.aws_vpc
supports arguments likecidr_block
,enable_dns_support
, and others.
You can find a complete list of arguments for each resource type in the Terraform Registry.
Best Practices for Resource Blocks
Uniqueness:
Each resource block must have a unique local name within the module.
For example, you cannot have two
aws_instance
blocks with the same local name (my_ec2_instance
).
Referencing Resources:
You can reference resources in other parts of your configuration using the combination of resource type and local name.
Example: Access the public IP of the instance:
aws_instance.my_ec2_instance.public_ip
Documentation:
Always refer to the Terraform Registry for detailed documentation on resource types and arguments.
Example: AWS Instance Documentation
Using Resource Arguments
Arguments within the resource block define the resource's configuration. For instance:
ami
: The ID of the Amazon Machine Image to use.instance_type
: Specifies the instance size.tags
: Adds metadata to the resource for better identification.
Each resource type has its specific set of arguments, which cannot be used interchangeably. For example:
ami
is valid foraws_instance
but not foraws_vpc
.Arguments for a database resource are different from those for a compute instance.
Finding Resource Types and Arguments
To find the correct resource type and arguments:
Visit the Terraform Registry.
Search for the resource type (e.g.,
aws_instance
for EC2).Review the detailed documentation, which includes all available arguments and attributes.
Conclusion
Terraform resources are the foundation of your infrastructure as code (IaC). They:
Define the specific infrastructure components you want to create.
Allow for detailed control through arguments specific to each resource type.
Provide flexibility to manage complex infrastructure efficiently.
Understanding the structure and purpose of resource blocks is essential for working effectively with Terraform. In the next section, weโll explore how to write complete Terraform scripts using providers and resources to build a robust infrastructure.
Stay tuned! ๐