I'm writing a custom Terraform provider and I'd like to implement the so-called complex case. I.e. my resource returns multiple sub-resources. When I look at the resulting .tfstate
file, the names of the sub-resources are generated from the names of the parent resource. So, for example, if I have a parent resource named 'cluster', then the sub-resources are named 'cluster', 'cluster-1', 'cluster-2' etc which is pretty useless to me. I can't find any way of controlling this outcome from within the provider. Is it possible to return a sensible name per sub-resource, based on the sub-resource attributes?
Here's a pruned .tfstate
example of what I mean. Note how the name
fields of each my_sub_resource
are derived from the my_parent_resource
resource name
field.
{
"version": 4,
"terraform_version": "1.0.1",
"serial": 1,
"lineage": "9e9e660b-35fb-df03-119a-5e8d06e775f1",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "my_sub_resource",
"name": "parent",
"provider": "***",
"instances": [
{
"schema_version": 0,
"attributes": {
"parent": "e2e7c3c2-219d-4ab1-a981-330cc431c0df",
"id": "9e2799fc-44d7-45eb-95bc-a8dd34c45ba1",
"tag": "resource1"
},
"sensitive_attributes": []
}
]
},
{
"mode": "managed",
"type": "my_sub_resource",
"name": "parent-1",
"provider": "***",
"instances": [
{
"schema_version": 0,
"attributes": {
"parent": "e2e7c3c2-219d-4ab1-a981-330cc431c0df",
"id": "2f4fa368-b7b2-44cb-a23d-ddb89696d55f",
"tag": "resource2"
},
"sensitive_attributes": []
}
]
},
{
"mode": "managed",
"type": "my_parent_resource",
"name": "parent",
"provider": "***",
"instances": [
{
"schema_version": 0,
"attributes": {
"id": "e2e7c3c2-219d-4ab1-a981-330cc431c0df",
"name": "parent"
},
"sensitive_attributes": []
}
]
}
]
}
To my knowledge this can't be fixed. What I did as a workaround, instead of having 2 separate resources, was have a single resource with multiple sub resource blocks. Each of the sub-resources can have a name
attribute and I therefore have the level of control I need. E.g.
resource "provider_cluster" "cluster" {
cluster_app {
name = "app1"
}
cluster_app {
name = "app2"
}
cluster_app {
name = "app3"
}
}