|
| 1 | +--- |
| 2 | +page_title: Manage components and resources in Stacks |
| 3 | +description: |- |
| 4 | + Learn how to update your Stack component configuration to manage components. Also, learn how to manage resources within the Terraform modules that each component sources. |
| 5 | +--- |
| 6 | + |
| 7 | +# Manage components and resources in Stacks |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +## Add components |
| 12 | + |
| 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/component_configuration/component). |
| 14 | + |
| 15 | +## Remove components |
| 16 | + |
| 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. |
| 18 | + |
| 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: |
| 20 | + |
| 21 | +<CodeBlockConfig filename="component.tfcomponent.hcl"> |
| 22 | + |
| 23 | +```hcl |
| 24 | +component "database" { |
| 25 | + source = "./modules/database" |
| 26 | +
|
| 27 | + inputs = { |
| 28 | + instance_class = "db.t3.micro" |
| 29 | + } |
| 30 | +
|
| 31 | + providers = { |
| 32 | + aws = provider.aws.main |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | +</CodeBlockConfig> |
| 37 | + |
| 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. |
| 39 | + |
| 40 | +<TIP> |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +</TIP> |
| 45 | + |
| 46 | +In the following example, Terraform removes the `database` component that sources from `./modules/database` and uses the `aws.main` provider: |
| 47 | + |
| 48 | +```hcl |
| 49 | +component "database" { |
| 50 | + source = "./modules/database" |
| 51 | +
|
| 52 | + inputs = { |
| 53 | + instance_class = "db.t3.micro" |
| 54 | + } |
| 55 | +
|
| 56 | + providers = { |
| 57 | + aws = provider.aws.main |
| 58 | + } |
| 59 | +} |
| 60 | +
|
| 61 | +removed { |
| 62 | + source = "./modules/database" |
| 63 | + from = component.database |
| 64 | +
|
| 65 | + providers = { |
| 66 | + aws = provider.aws.main |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 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. |
| 74 | + |
| 75 | +In the following example, the `removed` block iterates through `local.deprecated_components` to remove the `staging` components and corresponding resources: |
| 76 | + |
| 77 | +```hcl |
| 78 | +locals { |
| 79 | + components = ["dev", "prod"] |
| 80 | + deprecated_components = ["staging"] |
| 81 | +} |
| 82 | +
|
| 83 | +component "env" { |
| 84 | + for_each = toset(local.components) |
| 85 | +
|
| 86 | + source = "../local-component" |
| 87 | +
|
| 88 | + providers = { |
| 89 | + #... |
| 90 | + } |
| 91 | + #... |
| 92 | +} |
| 93 | +
|
| 94 | +removed { |
| 95 | + for_each = toset(local.deprecated_components) |
| 96 | + from = component.env[each.key] |
| 97 | +
|
| 98 | + providers = { |
| 99 | + #... |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 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. |
| 105 | + |
| 106 | +## Manage resources |
| 107 | + |
| 108 | +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. |
| 109 | + |
| 110 | +Use the following blocks in the Terraform configuration files that make up your module to manage individual resources: |
| 111 | + |
| 112 | +- To remove a resource from your module, use the `removed` block. To learn more, refer to [Destroy a resource](/terraform/language/resources/destroy). |
| 113 | +- 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](/terraform/language/import). |
| 114 | +- To move a resource from one module to another, use the `moved` block. To learn more, refer to [Move a resource](/terraform/language/modules/develop/refactoring#move-a-resource-or-module). |
0 commit comments