hashicorp / hashicorp/terraform

Named resource groups/namespaces

Open
#33,751 1 comment 3 reactions 0 assignees View on GitHub
enhancement new
Dominant language
Go
Stars
49.7k
Forks
10.6k
Avg merge
21h 30m
Merged PRs (30d)
100

Description

### Terraform Version

```shell
1.5
```

### Use Cases

Enabling grouping related elements in modules together under a given namespace to allow simplifying names.

Many cases exist where we have some kind of repeated IaC, but the overhead of using modules for them increases the complexity by needing variables and outputs to be redeclared.

An example would be setting up an AWS VPC. You might need to declare VPC endpoints for various AWS services, each needing a security group, security group rules, and other possible configurations like IAM policies. This quickly becomes very hard to separate concerns where the configuration varies slightly each time.

Having the ability to group resources into namespaces would make this easier to read since IDEs could collapse/fold entire namespaces at a time, and all resources under the namespace are enforced to have a consistent base name.

### Attempted Solutions

Right now, we can use modules to physically separate groups of resources but this introduces a lot of repeated code by having to redeclare variables and outputs.

We could also just use more complex names but this becomes harder to read and is difficult to enforce consistently.

### Proposal

Allow using an HCL named block at the top level of a Terraform source file to declare a namespace.

```terraform
namespace "stubs" {
}
```

The namespace can then hold any modules, datasources, resources, etc that relate to the namespace.

Resources within the namespace can refer to each other using their normal names like in existing Terraform code. This can lead to them taking more generalised names that are clearer to their relative module. Take this theoretical example:

```terraform
/**
* Resources related to the creation of a stub API server.
*/
namespace "wiremock_stub" {
resource "wiremock_image" "image" {
version = "1.2.3"
}

resource "wiremock_server" "server" {
image = wiremock_image.image.id
stub_mappings = wiremock_mappings.mappings.json
}

data "wiremock_mappings" "mappings" {
...
}
}
```

Resources can be interacted with outside their namespace using their fully qualified name.

```terraform
namespace "wiremock_stub" {
...

resource "wiremock_server" "this" {
...
}

...
}

resource "something_else" "whatever" {
stub_server_endpoint = wiremock_stub.wiremock_server.this.url
}
```

This could even be extended further to allow features such as those that I suggested in https://github.com/hashicorp/terraform/issues/33750 to be considered, like allowing blocks to be optional in bulk.

### References

_No response_

Contributor guide

Open the contributing guide

Research direction

The issue names no implementation files or tests. Start by reviewing Terraform's existing module, resource, and data block behavior, then compare the related proposal in issue 33750. Done would require an agreed design for namespace syntax, scoping, references, and compatibility before implementation work can begin.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
devops, infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.