Terraform
Create and manage resources overview
This topic describes how to configure resources using Terraform. For an overview of how to provision the infrastructure defined in your configuration, refer to Core Terraform workflow.
Introduction
A resource is any infrastructure object you create and manage with Terraform, including virtual networks, compute instances, or higher-level components such as DNS records. Resources depend on providers, which are plugins that let Terraform interface with external APIs to manage that service's objects. Each provider defines a set of supported resources. Refer to Providers for more information.
Write the resource configuration
Add a resource
block in your Terraform configuration file and specify the necessary arguments. Most arguments are specific to the resource and control its behavior. You can also configure Terraform-specific arguments, called meta-arguments, that determine how Terraform creates and manages the resource.
Refer to Configure a resource for more information.
Initialize your workspace
When you start a new Terraform project, you must initialize the workspace to download and install providers and modules you want to use to create the resources. If you change providers or modules in an existing Terraform project, you must reinitialize the workspace before you can apply the configuration. Refer to Initialize the Working Directory for details.
Apply the configuration
Use the Terraform CLI to apply the configuration. When you apply a configuration, Terraform performs the following operations:
- Creates resources in the configuration that do not yet exist as real infrastructure objects.
- Destroys resources that exist in the state but no longer exist in the configuration.
- Updates any resources in place if their arguments have changed.
- Destroys and re-creates resources whose arguments have changed but cannot be updated in-place due to remote API limitations.
- Updates the state file so that the configuration, real infrastructure, and state match.
Manage resources
As your infrastructure needs change, you may want to add or remove resources from your configuration, refactor collections of resources into reusable modules, or remove or retire resources from your cloud provider or state file. Refer to the following topics for more information about managing resources:
- Creating modules provides instructions on how to collect resources into reusable modules.
- Refactoring modules provides instructions on moving and renaming sources.
- Remove a resource from state describes how to remove a resource from state without destroying the actual infrastructure object.
- Destroy a resource describes how to remove a resource from state and destroy the actual infrastructure.
Many providers also allow you to read data from the provider so that you can use data from exisitng infrastructure without provisioning actual infrastructure objects. Refer to Query data sources for more information.