Skip to main content

Manage Terraform resources

Last update:

All resources created using Terraform are billed according to the service payment model. Billing for a resource starts after creating the resource.

The cost of resources created using Terraform is the same as the cost of resources created using the control panel.

Approaches to resource management

To manage resources, you can use the resource-based approach and the modular approach.

For getting started with Terraform, we recommend using the resource-based approach. This approach is considered easier to learn due to its clear syntax, lack of dependencies, and ease of resource debugging.

Resource-based approach

In the resource-based approach, every resource is described individually in a configuration file. This provides maximum control over every infrastructure component and is suitable for small projects, testing new functionality, or developing prototypes. The resource-based approach is easy to learn but can lead to code duplication when scaling.

Usage examples can be found in the Infrastructure creation examples section.

Modular approach

In the modular approach, logically related resources are combined into modules — independent, reusable infrastructure components. Modules encapsulate logic, for example, creating a cloud server or a cloud database cluster. This simplifies maintenance, testing, and collaboration, and also allows you to standardize infrastructure. It is suitable for medium and large projects.

Module examples can be found in the Selectel repository on GitHub.

Create or edit resources

To add new resources to your infrastructure or edit existing resources, you need to edit the configuration file. Terraform will automatically determine which resources need to be created, updated, or deleted.

If you make changes via the control panel, they will not be reflected in the Terraform configuration files.

We recommend creating resources in order. If you create all resources described in the configuration file at once, an error may occur — Terraform creates resources independently of the order in which they are listed in the file. Read more about creating resources in the Create resource dependencies guide in the Terraform documentation.

  1. Open the CLI.

  2. Make sure you are in the directory with the configuration file.

  3. Add the resources you want to create or edit the resource description in the configuration file.

  4. Check that the configuration file is valid:

    terraform validate
  5. Format the configuration file:

    terraform fmt
  6. Check which resources will be created:

    terraform plan
  7. Apply the changes and create the resources:

    terraform apply
  8. Confirm creation — enter yes and press Enter. Changes will be reflected in the Control Panel.

  9. If you do not have enough quotas to create resources, increase your quotas.

Limitations of the openstack_compute_flavor_v2 resource

If you change the vcpus, ram, or disk parameters in the openstack_compute_flavor_v2 resource description to an invalid value, an error will occur — Terraform will delete the resource and fail to create a new one.

To create a new flavor first and then delete the old one, add a block to the openstack_compute_flavor_v2 resource description:

lifecycle {
create_before_destroy = true
}

See the detailed description of the openstack_compute_flavor_v2 resource.

Delete resources

You can delete specific resources or the entire infrastructure that was created using Terraform. Resources created by other means (for example, in the control panel) will not be deleted.

  1. Make sure you are in the directory with the configuration file.

  2. Remove the resources from the configuration file.

  3. Check which resources will be deleted:

    terraform plan
  4. Apply the changes and delete the resources:

    terraform apply
  5. Confirm deletion — enter yes and press Enter. Deleted resources will stop appearing in the Control Panel.