Get startedGet started for free

Introduction to Resources

1. Introduction to Resources

The first topic in this module is resources. Let’s get started with an introduction to resources, before jumping into meta-arguments and dependencies. In the Terraform world, resources are infrastructure elements, such as Compute Engine instances and Cloud Storage buckets. These elements can be configured with Terraform code. Real-world infrastructure has a diverse set of resources and resource types. Terraform uses the underlying APIs of each Google Cloud service to deploy your resources. Resources include instances, instance templates, groups, VPC networks, firewall rules, VPN tunnels, Cloud Routers, and load balancers. Resources are defined within a . tf file. It’s recommended that you place similar types of resources in a directory and define resources in the main.tf file. In this example, main.tf is the root configuration. The resource block is used to declare a single infrastructure object. The resource type identifies the type of resource being created, and depends on the provider being declared within a Terraform module. A provider is a plugin that provides a collection of resource types. Generally, a provider is a cloud infrastructure platform. In this course, Google Cloud is the provider. Resource arguments use expressions to declare attributes. Some resource arguments are mandatory for resource creation, and others are optional. Attributes can be used to define any advanced features associated with a resource. You can include multiple resources of the same or different types within the same Terraform configuration file. These resources can even span across multiple providers. Here, the first resource block declares the VPC network, and another that declares the subnet. Both resources are in the same main.tf file. Let’s examine a couple more resource blocks. A resource is identified by the resource keyword, followed by the resource type. This example shows a VPC resource named vpc_network, which is of the type google_compute_network. The name is required for the google_compute_network block, but the other arguments are optional. You can also define advanced features, such as versioning, within the same block. This example defines a VPC subnet called subnetwork-ipv6. For the subnetwork block, the name, the network in which the subnet should be created, and the IP CIDR range are required. Notice that the resource arguments depend on the resource type. This means that the network includes arguments such as name, project, and auto_create subnetworks, and the subnetwork includes arguments such as name, IP_cidr_range, and network. When accessing a resource attribute from another resource block, use the format ... In this example, when a subnet is created, the subnet requires the network ID of the VPC network it belongs to. The network ID is a computed resource attribute of a google_compute_network block. The attribute is generated when the network is created. The subnet resource block uses the network ID created from the vpc_network block and uses the format google_compute_network.vpc_network.id. This method can be used only when resources are defined within the same root configuration. When defining a resource block, there are important factors to consider. First, a declared resource is identified by its type and name. Therefore the resource name must be unique within the module. The resource type is a keyword associated with the provider and cannot be user-defined. A Cloud Storage bucket is associated with the Google Cloud provider and is defined by the keyword google_storage_bucket. Any user-defined type would result in an error when terraform plan or terraform apply is executed. All configuration arguments must be enclosed within the resource block body, which is between the curly brackets. An infrastructure element and its associated attributes are defined within a resource block. A Terraform configuration will not pass through the plan and apply phases successfully without all the required arguments defined within the configuration.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.