Terraform
removed block reference for component configurations
Use the removed block to systematically remove components from your component configuration. Define removed blocks in a <NAME>.tfcomponent.hcl file, which is a component configuration file for your Stack.
If you are declaring a removed block in a traditional Terraform configuration file, ending in .tf, refer to the Terraform configuration removed block reference instead.
Background
Stacks take a systematic approach to removing components from your configuration. When you want to remove a component from a Stack, use the removed block to ensure Terraform knows which components to remove and which providers it requires to remove that 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.
The removed block also tells Terraform to destroy the resources managed by a specific component while maintaining the provider configurations needed for the cleanup operation.
Configuration model
The removed block supports the following arguments:
Complete configuration
All available arguments are defined in the following removed block:
removed {
for_each = <MAP_OR_SET>
source = "<MODULE_SOURCE>"
from = component.<COMPONENT_NAME>
providers = {
<PROVIDER_NAME> = provider.<PROVIDER_TYPE>.<ALIAS>
}
}
Specification
A removed block supports the following configuration.
removed
Each removed block represents a component or set of components that you want to remove from your Stack.
The following arguments are supported in a removed block:
| Argument | Description | Type | Required? |
|---|---|---|---|
source | The source module of the component you want to remove. | String | Required |
from | A reference to the component you want to remove. | Reference | Required |
providers | A mapping of provider names to the providers that the component uses. | Map | Required |
for_each | Remove multiple components based on a map or set. | Map or Set | Optional |
source
The source argument specifies the source module of the component you want to remove. The source argument must match the source argument from the original component block.
removed {
source = "<MODULE_SOURCE>"
# ...
}
Summary
- Data type: String
- Default: None
- Required: Yes
from
The from argument specifies a reference to the component you want to remove. Use the syntax component.<COMPONENT_NAME> to reference the component.
removed {
source = "<MODULE_SOURCE>"
from = component.<COMPONENT_NAME>
# ...
}
Summary
- Data type: Reference
- Default: None
- Required: Yes
providers
The providers argument maps provider names to the providers that the component you want to remove uses. HCP Terraform needs these provider configurations to properly remove the component's resources.
removed {
source = "<MODULE_SOURCE>"
from = component.<COMPONENT_NAME>
providers = {
<NAME> = provider.<PROVIDER_NAME>.<PROVIDER_ALIAS>
}
}
The provider mapping must include every provider that the component's module requires for resource destruction.
Summary
- Data type: Map
- Default: None
- Required: Yes
for_each
The for_each meta-argument supports removing multiple components that Terraform created using for_each.
removed {
for_each = [ "<VALUE>" ]
#...
}
The for_each meta-argument is useful when you want to remove components from specific regions or environments.
Summary
- Data type: Map or Set
- Default: None
- Example: Removing regional components
Examples
The following examples demonstrate common use cases for removed blocks.
Fundamental component removal
In the following example, if you apply a configuration that creates the database component in your Stack:
component "database" {
source = "./modules/database"
inputs = {
instance_class = "db.t3.micro"
}
providers = {
aws = provider.aws.main
}
}
On a subsequent apply, you can define a removed block to remove 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 next deployment run 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 tfcomponent.hcl file.
Removing regional components
In the following example, HCP Terraform has deployed the regional_storage component in multiple regions.
After applying the following configuration, HCP Terraform removes regional_storage components from regions specified in var.regions_to_remove, using the corresponding provider configuration from provider.aws.configurations for each region:
component "regional_storage" {
for_each = var.regions
source = "./modules/s3"
inputs = {
region = each.value
}
providers = {
aws = provider.aws.configurations[each.value]
}
}
removed {
for_each = var.regions_to_remove
source = "./modules/s3"
from = component.regional_storage[each.value]
providers = {
aws = provider.aws.configurations[each.value]
}
}
Removing components with multiple providers
In the following example, Terraform removes an existing k8s_app component that sources from ./modules/kubernetes-app and uses three providers:
removed {
source = "./modules/kubernetes-app"
from = component.k8s_app
providers = {
kubernetes = provider.kubernetes.main
helm = provider.helm.main
random = provider.random.main
}
}