Skip to content

Commit 63c8c32

Browse files
authored
Stacks - specify to delete the component block while adding remove (#1102)
1 parent 3cb4560 commit 63c8c32

File tree

2 files changed

+88
-34
lines changed
  • content/terraform
    • v1.13.x/docs/language/stacks/component
    • v1.14.x (beta)/docs/language/stacks/component

2 files changed

+88
-34
lines changed

content/terraform/v1.13.x/docs/language/stacks/component/manage.mdx

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ Stacks are made up of components, and each component includes a Terraform module
1010

1111
## Add components
1212

13-
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](/terraform/language/block/stack/tfcomponent/component).
13+
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. When you define a `component` block, you specify the module that component sources its configuration from, any input variables the module requires, and provider configurations.
14+
15+
To learn more about the `component` refer to the [`component` block reference](/terraform/language/block/stack/tfcomponent/component).
1416

1517
## Remove components
1618

17-
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.
19+
Stacks take a systematic approach to removing components from your configuration. To remove a component from your Stack, you must do the following:
20+
1. Delete the `component` block for the component you want to remove.
21+
1. Add a `removed` block specifying the component you want to remove.
22+
1. Push your configuration changes to HCP Terraform and apply the changes to your Stack.
23+
24+
In the `removed` block, you specify the component you want to remove, the module that component sources, and that component's provider configurations. Specifying all of a component's details ensures that Terraform can properly destroy all the resources associated with that component.
1825

19-
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:
26+
For example, if you want to remove the following `database` component:
2027

2128
<CodeBlockConfig filename="component.tfcomponent.hcl">
2229

@@ -35,44 +42,64 @@ component "database" {
3542
```
3643
</CodeBlockConfig>
3744

38-
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.
45+
Add a `removed` block that specifies the component to remove, the module that component sources, and the providers that component uses.
3946

4047
<TIP>
4148

4249
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.
4350

4451
</TIP>
4552

46-
In the following example, Terraform removes the `database` component that sources from `./modules/database` and uses the `aws.main` provider:
53+
The following example deletes the `database` component block and adds a `removed` block to tell Terraform to remove that component from your Stack:
4754

4855
```hcl
49-
component "database" {
56+
removed {
5057
source = "./modules/database"
58+
from = component.database
5159
52-
inputs = {
53-
instance_class = "db.t3.micro"
60+
providers = {
61+
aws = provider.aws.main
5462
}
63+
}
64+
```
65+
66+
After you apply the configuration in HCP Terraform, Terraform removes the `database` component and its resources from the associated Stack deployments. You can then delete the `removed` block from your component configuration file.
67+
68+
### Remove multiple components
69+
70+
If your `component` block uses the `for_each` meta-argument to define multiple components, use the `for_each` meta-argument in a `removed` block to let Terraform destroy multiple component instances. Using `for_each` in a `removed` block lets you remove all of the instances of a component, or a subset of those instances.
71+
72+
To remove all of the instances of a component, duplicate the same `for_each` expression in the `removed` block that you use in the `component` block. For example, to remove all of the instances of `env` components:
73+
74+
```hcl
75+
component "env" {
76+
for_each = ["dev", "staging"]
77+
78+
source = "../local-component"
5579
5680
providers = {
57-
aws = provider.aws.main
81+
#...
5882
}
83+
#...
5984
}
85+
```
6086

87+
Remove the `env` component block and add a `removed` block that uses the same `for_each` expression:
88+
89+
```hcl
6190
removed {
62-
source = "./modules/database"
63-
from = component.database
91+
for_each = ["dev", "staging"]
92+
from = component.env[each.key]
6493
6594
providers = {
66-
aws = provider.aws.main
95+
#...
6796
}
6897
}
6998
```
7099

71-
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.
72-
73-
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.
100+
After you apply the configuration in HCP Terraform, Terraform removes all of the instances of the `env` component and its resources from the associated Stack deployments. You can then delete the `removed` block from your component configuration file.
74101

75-
In the following example, the `removed` block iterates through `local.deprecated_components` to remove the `staging` components and corresponding resources:
102+
If you want to remove a subset of component instances, you must keep the `component` block that defines the instances you want to keep. In the following example, the `removed` block iterates through `local.deprecated_components` to remove the `staging` components and corresponding resources:
76103

77104
```hcl
78105
locals {
@@ -101,7 +128,7 @@ removed {
101128
}
102129
```
103130

104-
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.
131+
After you apply the configuration in HCP Terraform, Terraform removes the `staging` component but the `dev` and `prod` components remain in your Stack. You can additionally delete more components by removing them from the `local.components` list and adding them to the `local.removed_components` list.
105132

106133
## Manage resources
107134

content/terraform/v1.14.x (beta)/docs/language/stacks/component/manage.mdx

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ Stacks are made up of components, and each component includes a Terraform module
1010

1111
## Add components
1212

13-
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](/terraform/language/block/stack/tfcomponent/component).
13+
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. When you define a `component` block, you specify the module that component sources its configuration from, any input variables the module requires, and provider configurations.
14+
15+
To learn more about the `component` refer to the [`component` block reference](/terraform/language/block/stack/tfcomponent/component).
1416

1517
## Remove components
1618

17-
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.
19+
Stacks take a systematic approach to removing components from your configuration. To remove a component from your Stack, you must do the following:
20+
1. Delete the `component` block for the component you want to remove.
21+
1. Add a `removed` block specifying the component you want to remove.
22+
1. Push your configuration changes to HCP Terraform and apply the changes to your Stack.
23+
24+
In the `removed` block, you specify the component you want to remove, the module that component sources, and that component's provider configurations. Specifying all of a component's details ensures that Terraform can properly destroy all the resources associated with that component.
1825

19-
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:
26+
For example, if you want to remove the following `database` component:
2027

2128
<CodeBlockConfig filename="component.tfcomponent.hcl">
2229

@@ -35,44 +42,64 @@ component "database" {
3542
```
3643
</CodeBlockConfig>
3744

38-
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.
45+
Add a `removed` block that specifies the component to remove, the module that component sources, and the providers that component uses.
3946

4047
<TIP>
4148

4249
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.
4350

4451
</TIP>
4552

46-
In the following example, Terraform removes the `database` component that sources from `./modules/database` and uses the `aws.main` provider:
53+
The following example deletes the `database` component block and adds a `removed` block to tell Terraform to remove that component from your Stack:
4754

4855
```hcl
49-
component "database" {
56+
removed {
5057
source = "./modules/database"
58+
from = component.database
5159
52-
inputs = {
53-
instance_class = "db.t3.micro"
60+
providers = {
61+
aws = provider.aws.main
5462
}
63+
}
64+
```
65+
66+
After you apply the configuration in HCP Terraform, Terraform removes the `database` component and its resources from the associated Stack deployments. You can then delete the `removed` block from your component configuration file.
67+
68+
### Remove multiple components
69+
70+
If your `component` block uses the `for_each` meta-argument to define multiple components, use the `for_each` meta-argument in a `removed` block to let Terraform destroy multiple component instances. Using `for_each` in a `removed` block lets you remove all of the instances of a component, or a subset of those instances.
71+
72+
To remove all of the instances of a component, duplicate the same `for_each` expression in the `removed` block that you use in the `component` block. For example, to remove all of the instances of `env` components:
73+
74+
```hcl
75+
component "env" {
76+
for_each = ["dev", "staging"]
77+
78+
source = "../local-component"
5579
5680
providers = {
57-
aws = provider.aws.main
81+
#...
5882
}
83+
#...
5984
}
85+
```
6086

87+
Remove the `env` component block and add a `removed` block that uses the same `for_each` expression:
88+
89+
```hcl
6190
removed {
62-
source = "./modules/database"
63-
from = component.database
91+
for_each = ["dev", "staging"]
92+
from = component.env[each.key]
6493
6594
providers = {
66-
aws = provider.aws.main
95+
#...
6796
}
6897
}
6998
```
7099

71-
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.
72-
73-
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.
100+
After you apply the configuration in HCP Terraform, Terraform removes all of the instances of the `env` component and its resources from the associated Stack deployments. You can then delete the `removed` block from your component configuration file.
74101

75-
In the following example, the `removed` block iterates through `local.deprecated_components` to remove the `staging` components and corresponding resources:
102+
If you want to remove a subset of component instances, you must keep the `component` block that defines the instances you want to keep. In the following example, the `removed` block iterates through `local.deprecated_components` to remove the `staging` components and corresponding resources:
76103

77104
```hcl
78105
locals {
@@ -101,7 +128,7 @@ removed {
101128
}
102129
```
103130

104-
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.
131+
After you apply the configuration in HCP Terraform, Terraform removes the `staging` component but the `dev` and `prod` components remain in your Stack. You can additionally delete more components by removing them from the `local.components` list and adding them to the `local.removed_components` list.
105132

106133
## Manage resources
107134

0 commit comments

Comments
 (0)