Skip to content

Commit a95ddf4

Browse files
[1.10][AWS DynamoDB] Documentation for Partition Key Metadata Field (#3052)
* [1.10][AWS DynamoDB] Documentation for Partition Key Metadata Field Signed-off-by: Roberto J Rojas <[email protected]> * Update daprdocs/content/en/reference/components-reference/supported-state-stores/setup-dynamodb.md Co-authored-by: Mark Fussell <[email protected]> Signed-off-by: Roberto Rojas <[email protected]> * Update daprdocs/content/en/reference/components-reference/supported-state-stores/setup-dynamodb.md Co-authored-by: Mark Fussell <[email protected]> Signed-off-by: Roberto Rojas <[email protected]> * updates status to stable + fixes as PR review Signed-off-by: Roberto J Rojas <[email protected]> * Update setup-dynamodb.md Removing unneeded dots Signed-off-by: Mark Fussell <[email protected]> * Update setup-dynamodb.md can to console Signed-off-by: Mark Fussell <[email protected]> Signed-off-by: Roberto J Rojas <[email protected]> Signed-off-by: Roberto Rojas <[email protected]> Signed-off-by: Mark Fussell <[email protected]> Co-authored-by: Mark Fussell <[email protected]>
1 parent fbc95ef commit a95ddf4

File tree

2 files changed

+89
-5
lines changed
  • daprdocs

2 files changed

+89
-5
lines changed

daprdocs/content/en/reference/components-reference/supported-state-stores/setup-dynamodb.md

+87-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ spec:
2121
version: v1
2222
metadata:
2323
- name: table
24-
value: "mytable"
24+
value: "Contracts"
2525
- name: accessKey
2626
value: "AKIAIOSFODNN7EXAMPLE" # Optional
2727
- name: secretKey
@@ -34,6 +34,8 @@ spec:
3434
value: "myTOKEN" # Optional
3535
- name: ttlAttributeName
3636
value: "expiresAt" # Optional
37+
- name: partitionKey
38+
value: "ContractID" # Optional
3739
```
3840
3941
{{% alert title="Warning" color="warning" %}}
@@ -42,19 +44,20 @@ The above example uses secrets as plain strings. It is recommended to use a secr
4244
4345
## Primary Key
4446
45-
In order to use DynamoDB as a Dapr state store, the table must have a primary key named `key`.
47+
In order to use DynamoDB as a Dapr state store, the table must have a primary key named `key`. See the section [Partition Keys]({{< ref "setup-dynamodb.md#partition-keys" >}}) for an option to change this behavior.
4648

4749
## Spec metadata fields
4850

4951
| Field | Required | Details | Example |
5052
|--------------------|:--------:|---------|---------|
51-
| table | Y | name of the DynamoDB table to use | `"mytable"`
53+
| table | Y | name of the DynamoDB table to use | `"Contracts"`
5254
| accessKey | N | ID of the AWS account with appropriate permissions to SNS and SQS. Can be `secretKeyRef` to use a secret reference | `"AKIAIOSFODNN7EXAMPLE"`
5355
| secretKey | N | Secret for the AWS user. Can be `secretKeyRef` to use a secret reference |`"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"`
5456
| region | N | The AWS region to the instance. See this page for valid regions: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html. Ensure that DynamoDB are available in that region.| `"us-east-1"`
5557
| endpoint | N |AWS endpoint for the component to use. Only used for local development. The `endpoint` is unncessary when running against production AWS | `"http://localhost:4566"`
5658
| sessionToken | N |AWS session token to use. A session token is only required if you are using temporary security credentials. | `"TOKEN"`
5759
| ttlAttributeName | N |The table attribute name which should be used for TTL. | `"expiresAt"`
60+
| partitionKey | N |The table primary key or partition key attribute name. This field is used to replace the default primary key attribute name `"key"`. See the section [Partition Keys]({{< ref "setup-dynamodb.md#partition-keys" >}}). | `"ContractID"`
5861

5962
{{% alert title="Important" color="warning" %}}
6063
When running the Dapr sidecar (daprd) with your application on EKS (AWS Kubernetes), if you're using a node/pod that has already been attached to an IAM policy defining access to AWS resources, you **must not** provide AWS access-key, secret-key, and tokens in the definition of the component spec you're using.
@@ -70,6 +73,87 @@ In order to use DynamoDB TTL feature, you must enable TTL on your table and defi
7073
The attribute name must be defined in the `ttlAttributeName` field.
7174
See official [AWS docs](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html).
7275

76+
## Partition Keys
77+
78+
By default, the DynamoDB state store component uses the table attribute name `key` as primary/partition key in the DynamoDB table.
79+
This can be overridden by specifying a metadata field in the component configuration with a key of `partitionKey` and a value of the desired attribute name.
80+
81+
To learn more about DynamoDB primary/partition keys, read the [AWS DynamoDB Developer Guide.](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.PrimaryKey)
82+
83+
The following `statestore.yaml` file shows how to configure the DynamoDB state store component to use the partition key attribute name of `ContractID`:
84+
85+
```yaml
86+
apiVersion: dapr.io/v1alpha1
87+
kind: Component
88+
metadata:
89+
name: statestore
90+
spec:
91+
type: state.aws.dynamodb
92+
version: v1
93+
metadata:
94+
- name: table
95+
value: "Contracts"
96+
- name: partitionKey
97+
value: "ContractID"
98+
```
99+
100+
The above component specification assumes the following DynamoDB Table Layout:
101+
102+
```console
103+
{
104+
"Table": {
105+
"AttributeDefinitions": [
106+
{
107+
"AttributeName": "ContractID",
108+
"AttributeType": "S"
109+
}
110+
],
111+
"TableName": "Contracts",
112+
"KeySchema": [
113+
{
114+
"AttributeName": "ContractID",
115+
"KeyType": "HASH"
116+
}
117+
],
118+
}
119+
```
120+
121+
The following operation passes `"A12345"` as the value for `key`, and based on the component specification provided above, the Dapr runtime will replace the `key` attribute name
122+
with `ContractID` as the Partition/Primary Key sent to DynamoDB:
123+
124+
```shell
125+
$ dapr run --app-id contractsprocessing --app-port ...
126+
127+
$ curl -X POST http://localhost:3500/v1.0/state/<store_name> \
128+
-H "Content-Type: application/json"
129+
-d '[
130+
{
131+
"key": "A12345",
132+
"value": "Dapr Contract"
133+
}
134+
]'
135+
```
136+
137+
The following AWS CLI Command displays the contents of the DynamoDB `Contracts` table:
138+
```shell
139+
$ aws dynamodb get-item \
140+
--table-name Contracts \
141+
--key '{"ContractID":{"S":"contractsprocessing||A12345"}}'
142+
{
143+
"Item": {
144+
"value": {
145+
"S": "Dapr Contract"
146+
},
147+
"etag": {
148+
"S": "....."
149+
},
150+
"ContractID": {
151+
"S": "contractsprocessing||A12345"
152+
}
153+
}
154+
}
155+
```
156+
73157
## Related links
74158

75159
- [Basic schema for a Dapr component]({{< ref component-schema >}})

daprdocs/data/components/state_stores/aws.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
- component: AWS DynamoDB
22
link: setup-dynamodb
3-
state: Alpha
3+
state: Stable
44
version: v1
5-
since: "1.0"
5+
since: "1.10"
66
features:
77
crud: true
88
transactions: false

0 commit comments

Comments
 (0)