All Products
Search
Document Center

Terraform:Resource

Last Updated:Oct 15, 2025

What is a resource

In Terraform, a resource is an infrastructure element, such as an ECS instance or an OSS bucket, that you can configure using Terraform code. Real-world infrastructure contains various resources and resource types. Terraform uses the API of each Alibaba Cloud service to deploy your resources, such as ECS instances, ECS instance deployment templates, security groups, VPC networks, firewall rules, VPNs, route tables, databases, and Server Load Balancers.

Syntax for resource declaration

Resources are defined in .tf files. We recommend that you place resources of similar types in one folder and define them in the main.tf file.

image

In this example, main.tf is the root configuration file:

  • resource

    The resource block declares a single infrastructure object.

  • resource_type

    The resource_type parameter identifies the type of resource to create. This resource type depends on the provider declared in the Terraform provider block. A provider is a plugin that provides a set of resource types.

  • resource_name

    The resource_name parameter is the identity of the resource within the current configuration folder. It is not the name of the actual infrastructure object. The resource_name must be unique among all resources of the same type within the current folder.

  • Resource parameters

    Resource parameters use expressions to declare the properties of a resource. Some parameters are required when the resource is created, while others are optional. You can use properties to define advanced features for the resource.

You can include multiple resources of the same or different types in the same Terraform configuration file. These resources can even span multiple providers.

image

In this example, the first resource block declares a VPC network and the second block declares a vSwitch. Both resources are in the same main.tf file. This example shows a VPC resource of the alicloud_vpc type named main-vpc and a vSwitch resource of the alicloud_vswitch type named main-vswitch.

All parameters in the alicloud_vpc block are optional. The cidr_block parameter is optional because it has a default value. If you do not explicitly define this parameter in the configuration file, the provider or the VPC service backend generates a default value for it.

For the alicloud_vswitch block, vpc_id, cidr_block, and zone_id are required parameters. The other parameters are optional.

Note that resource parameters are specific to the resource type. This means that parameters vary greatly among different resource types. For example, alicloud_vpc includes parameters such as vpc_name and cidr_block, whereas alicloud_vswitch includes parameters such as vswitch_name, cidr_block, zone_id, and vpc_id.

Attribute parameter reference

To access a resource property from another resource block, use the following format: <resource_type>.<resource_name>.<attribute>.

In the preceding example, the vSwitch requires the ID of the VPC to which it belongs when the vSwitch is created. The VPC ID is a computing resource property of the alicloud_vpc block. This means that the server generates the VPC ID when the VPC is created. The alicloud_vswitch resource block uses the ID from the created alicloud_vpc block. The reference format is alicloud_vpc.main_vpc.id.

Note that you can use this method only when the related resources are defined in files within the same root configuration folder.

Considerations for resource definitions

When you define a resource block, consider the following important factors:

  1. The resource name must be unique

    A declared resource is identified by its type and name in the current configuration folder. Therefore, the resource name must be unique within its configuration folder.

    image
  2. The resource type cannot be customized

    The resource type is a keyword that is associated with a provider and cannot be customized. An OSS bucket is associated with the Alibaba Cloud provider and is defined by the keyword alicloud_oss_bucket. Any other definition for an OSS bucket causes an error when you run the terraform plan or terraform apply command.

    image
  3. All configuration parameters must be included within the resource block body, which is between the curly braces ({})

    Infrastructure elements and their related properties are defined in the resource block.

    image

    When you execute a Terraform command, the following error message appears:image

  4. All required parameters must be set

    If you do not define all required parameters in the configuration, the terraform plan and terraform apply commands fail.

    image

    When you execute a Terraform command, the following error message appears:image