Resource
What is a resource
A resource is an infrastructure element you configure using Terraform code — an Elastic Compute Service (ECS) instance, an Object Storage Service (OSS) bucket, a Virtual Private Cloud (VPC) network, or any other cloud object. Terraform calls each Alibaba Cloud service's API to create and manage these resources, including ECS instances, ECS instance deployment templates, security groups, VPC networks, firewall rules, VPNs, route tables, databases, and Server Load Balancers (SLBs).
Syntax for resource declaration
Define resources in .tf files. Place resources of similar types in the same folder and declare them in main.tf.
In this example, main.tf is the root configuration file:
-
resource
The
resourcekeyword declares a single infrastructure object. -
resource_type
resource_typeidentifies the type of resource to create. Available types come from the provider declared in the Terraform provider block. A provider is a plugin that exposes a set of resource types. -
resource_name
resource_nameis a logical identifier for the resource within the current configuration folder — not the name of the actual infrastructure object created in the cloud.resource_namemust 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 at creation time; others are optional and let you configure advanced features.
A single Terraform configuration file can contain multiple resources of the same or different types, and those resources can span multiple providers.
In this example, the first resource block declares a VPC network and the second declares a vSwitch, both in the same main.tf file. The VPC resource is of type alicloud_vpc and named main-vpc; the vSwitch resource is of type alicloud_vswitch and named main-vswitch.
All parameters in the alicloud_vpc block are optional. For example, cidr_block is optional because it has a default value — if you omit it, the provider or VPC service backend generates one automatically.
For alicloud_vswitch, vpc_id, cidr_block, and zone_id are required; all other parameters are optional.
Resource parameters are specific to each resource type and vary significantly between types. For example, alicloud_vpc accepts vpc_name and cidr_block, while alicloud_vswitch accepts vswitch_name, cidr_block, zone_id, and vpc_id.
Attribute parameter reference
To reference a resource property from another resource block, use the format: <resource_type>.<resource_name>.<attribute>.
In the example above, alicloud_vswitch requires the VPC ID at creation time. Because the VPC ID is generated by the server when the VPC is created, alicloud_vswitch references it from the alicloud_vpc block using alicloud_vpc.main_vpc.id.
Cross-resource references only work when the related resources are defined in files within the same root configuration folder.
Considerations for resource definitions
Keep the following rules in mind when defining resource blocks:
-
The resource name must be unique
Each resource is identified by its type and name within the current configuration folder. Resource names must be unique among all resources of the same type in that folder.
-
The resource type cannot be customized
The resource type is a fixed keyword tied to a provider. An OSS bucket, for example, must use the keyword
alicloud_oss_bucket. Any other identifier causesterraform planandterraform applyto fail. -
All configuration parameters must be declared inside the resource block body
All infrastructure properties for a resource must be declared inside the resource block, between the curly braces
{}.When you run a Terraform command, an error similar to the following appears:
Error: Argument or block definition required on main.tf line 4: 4: storage_class An argument or block definition is required here. -
All required parameters must be set
Omitting any required parameter causes
terraform planandterraform applyto fail.When you run a Terraform command, an error similar to the following appears:
Error: Missing required argument on main.tf line 1, in resource "alicloud_vswitch" "main_vswitch": 1: resource "alicloud_vswitch" "main_vswitch" { The argument "vpc_id" is required, but no definition was found.