Terraform
Manage components and resources in Stacks
Stacks are made up of components, and each component includes a Terraform module as its source. Learn how to add and remove components from a Stack, and how to manage resources within the module that each component sources.
Add components
The component
block defines the pieces that make up your Stack. Add a component
block for each top-level module you want to include in the Stack. You must specify the source module, inputs, and providers for each component. To learn more about the component
refer to the component
block reference.
Remove components
Stacks take a systematic approach to removing components from your configuration. You must use a dedicated removed
block in your component configuration to ensure Terraform can properly remove your component and the resources associated with that component.
To remove a component, add a removed
block to your component configuration specifying the component you want to remove. For example, if you want to remove the following database
component:
component.tfcomponent.hcl
component "database" {
source = "./modules/database"
inputs = {
instance_class = "db.t3.micro"
}
providers = {
aws = provider.aws.main
}
}
You add a removed
block that specifies the component to remove, module that component sources, and the providers that component uses. The removed
block also tells Terraform to destroy the resources managed by a component.
Do not remove providers from your component configuration without first removing the components that require those providers. Terraform requires a component's providers to ensure it can successfully remove that component.
In the following example, Terraform removes the database
component that sources from ./modules/database
and uses the aws.main
provider:
component "database" {
source = "./modules/database"
inputs = {
instance_class = "db.t3.micro"
}
providers = {
aws = provider.aws.main
}
}
removed {
source = "./modules/database"
from = component.database
providers = {
aws = provider.aws.main
}
}
After you apply the configuration in HCP Terraform, Terraform removes the database
component and its resources from the associated Stack deployments. You can then remove both the component
block and the removed
block from your component configuration file.
If your component
block uses the for_each
meta-argument to define multiple components, you can define a removed
block with the for_each
meta-argument to ensure Terraform destroys each component instance.
In the following example, the removed
block iterates through local.deprecated_components
to remove the staging
components and corresponding resources:
locals {
components = ["dev", "prod"]
deprecated_components = ["staging"]
}
component "env" {
for_each = toset(local.components)
source = "../local-component"
providers = {
#...
}
#...
}
removed {
for_each = toset(local.deprecated_components)
from = component.env[each.key]
providers = {
#...
}
}
In the above example, you can delete a component by removing it from the local.components
list and adding it to the local.removed_components
list.
Manage resources
A Stack component sources its configuration from a module. Module use traditional Terraform configuration files that end with .tf
. To interact with individual resources in a component, you must update the module your component sources its configuration from.
Use the following blocks in the Terraform configuration files that make up your module to manage individual resources:
- To remove a resource from your module, use the
removed
block. To learn more, refer to Destroy a resource. - To tell Terraform to take over the management of an existing infrastructure resource, use the
import
block. To learn more, refer to Import a resource. - To move a resource from one module to another, use the
moved
block. To learn more, refer to Move a resource.