Skip to content

Commit 1c84840

Browse files
authored
Document set basics (#2198)
Reference documentation to inform development. Document the Set type expectations with references to TF sources.
1 parent 35af14c commit 1c84840

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

docs/sets.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Sets in Terraform
2+
3+
Terraform supports Sets as a special collection type where two collections are considered equal up to element
4+
reordering. In particular this means that Terraform considers set element reordering in programs a no-change plan.
5+
6+
## Sets in SDKv2 providers
7+
8+
To decide if two set elements are equal, TF consults the provider to compute an integer controlled by the provider
9+
author with
10+
[SchemaSetFunc](https://github.com/pulumi/terraform-plugin-sdk/blob/upstream-v2.33.0/helper/schema/schema.go#L246).
11+
Notably this integer is not written to state but is ephemeral.
12+
13+
## Sets in Plugin Framework
14+
15+
The [Set](https://developer.hashicorp.com/terraform/plugin/framework/handling-data/types/set) collection is supported as
16+
with SDKv2 but SchemaSetFunc is not available for Plugin Framework authors. However, similar functionality can be
17+
achieved by using custom-typed values that may override
18+
[Equal](https://github.com/hashicorp/terraform-plugin-framework/blob/v1.10.0/attr/value.go#L55) to define a custom
19+
notion of equality.
20+
21+
Per [Migration Notes](https://developer.hashicorp.com/terraform/plugin/framework/migrating/schema#migration-notes):
22+
23+
> In SDKv2, schema structs have a Set field which can be populated with a SchemaSetFunc which is used for hashing. In
24+
> the Framework, this is not required and does not need to be migrated.
25+
26+
## Example
27+
28+
In the following example, grants to the aws_s3_bucket_acl resource are set elements. Reordering them does not result in
29+
any detected changes when using Terraform CLI.
30+
31+
``` hcl
32+
data "aws_canonical_user_id" "current" {}
33+
34+
resource "aws_s3_bucket" "example" {
35+
bucket = "my-tf-example-bucket-t0yv0-2024"
36+
}
37+
38+
resource "aws_s3_bucket_ownership_controls" "example" {
39+
bucket = aws_s3_bucket.example.id
40+
rule {
41+
object_ownership = "BucketOwnerPreferred"
42+
}
43+
}
44+
45+
resource "aws_s3_bucket_acl" "example" {
46+
depends_on = [aws_s3_bucket_ownership_controls.example]
47+
48+
bucket = aws_s3_bucket.example.id
49+
access_control_policy {
50+
grant {
51+
grantee {
52+
type = "Group"
53+
uri = "http://acs.amazonaws.com/groups/s3/LogDelivery"
54+
}
55+
permission = "READ_ACP"
56+
}
57+
58+
grant {
59+
grantee {
60+
id = data.aws_canonical_user_id.current.id
61+
type = "CanonicalUser"
62+
}
63+
permission = "READ"
64+
}
65+
66+
owner {
67+
id = data.aws_canonical_user_id.current.id
68+
}
69+
}
70+
}
71+
```

0 commit comments

Comments
 (0)