Dynamo
Reference doc for the `sst.aws.Dynamo` component.
The Dynamo
component lets you add an Amazon DynamoDB table to your app.
Minimal example
Add a global index
Optionally add a global index to the table.
Add a local index
Optionally add a local index to the table.
Subscribe to a DynamoDB Stream
To subscribe to a DynamoDB Stream, start by enabling it.
Then, subscribing to it.
Link the table to a resource
You can link the table to other resources, like a function or your Next.js app.
Once linked, you can query the table through your app.
Constructor
Parameters
-
name
string
-
args
DynamoArgs
-
opts?
ComponentResourceOptions
DynamoArgs
deletionProtection?
Type Input
<
boolean
>
Enable deletion protection for the table. When enabled, the table cannot be deleted.
fields
Type Input
<
Record
<
string
, “
string
”
|
“
number
”
|
“
binary
”
>
>
An object defining the fields of the table that’ll be used to create indexes. The key is the name of the field and the value is the type.
While you can have fields field types other than string
, number
, and binary
; you can only use these types for your indexes.
globalIndexes?
Type Input
<
Record
<
string
, Input
<
Object
>
>
>
Configure the table’s global secondary indexes.
You can have up to 20 global secondary indexes per table. And each global secondary index should have a unique name.
globalIndexes[].hashKey
Type Input
<
string
>
The hash key field of the index. This field needs to be defined in the fields
.
globalIndexes[].projection?
Type Input
<
Input
<
string
>
[]
|
“
all
”
|
“
keys-only
”
>
Default “all”
The fields to project into the index.
Project only the key fields: userId
and createdAt
.
Project the noteId
field in addition to the key fields.
globalIndexes[].rangeKey?
Type Input
<
string
>
The range key field of the index. This field needs to be defined in the fields
.
localIndexes?
Type Input
<
Record
<
string
, Input
<
Object
>
>
>
Configure the table’s local secondary indexes.
Unlike global indexes, local indexes use the same hashKey
as the primaryIndex
of the table.
You can have up to 5 local secondary indexes per table. And each local secondary index should have a unique name.
localIndexes[].projection?
Type Input
<
Input
<
string
>
[]
|
“
all
”
|
“
keys-only
”
>
Default “all”
The fields to project into the index.
Project only the key field: createdAt
.
Project the noteId
field in addition to the key field.
localIndexes[].rangeKey
Type Input
<
string
>
The range key field of the index. This field needs to be defined in the fields
.
primaryIndex
Type Input
<
Object
>
Define the table’s primary index. You can only have one primary index.
primaryIndex.hashKey
Type Input
<
string
>
The hash key field of the index. This field needs to be defined in the fields
.
primaryIndex.rangeKey?
Type Input
<
string
>
The range key field of the index. This field needs to be defined in the fields
.
stream?
Type Input
<
“
keys-only
”
|
“
new-image
”
|
“
old-image
”
|
“
new-and-old-images
”
>
Default Disabled
Enable DynamoDB Streams for the table.
When an item in the table is modified, the stream captures the information and sends it to your subscriber function.
You can configure what will be written to the stream:
new-image
: The entire item after it was modified.old-image
: The entire item before it was modified.new-and-old-images
: Both the new and the old items. A good default to use since it contains all the data.keys-only
: Only the keys of the fields of the modified items. If you are worried about the costs, you can use this since it stores the least amount of data.
transform?
transform.table?
Type TableArgs
|
(
args
:
TableArgs
,
opts
:
ComponentResourceOptions
,
name
:
string
)
=>
void
Transform the DynamoDB Table resource.
ttl?
Type Input
<
string
>
The field in the table to store the Time to Live or TTL timestamp in. This field should
be of type number
. When the TTL timestamp is reached, the item will be deleted.
Read more about Time to Live.
Here the TTL field in our table is called expireAt
.
Properties
arn
Type Output
<
string
>
The ARN of the DynamoDB Table.
name
Type Output
<
string
>
The name of the DynamoDB Table.
nodes
nodes.table
Type Output
<
Table
>
The Amazon DynamoDB Table.
SDK
Use the SDK in your runtime to interact with your infrastructure.
Links
This is accessible through the Resource
object in the SDK.
-
name
string
The name of the DynamoDB Table.
Methods
subscribe
Parameters
The name of the subscriber.name
string
The function that’ll be notified.subscriber
Input
<
string
|
FunctionArgs
|
“arn:aws:lambda:${string}”
>
Configure the subscription.args?
DynamoSubscriberArgs
Returns Output
<
DynamoLambdaSubscriber
>
Subscribe to the DynamoDB Stream of this table.
Add a filter to the subscription.
Customize the subscriber function.
Or pass in the ARN of an existing Lambda function.
static get
Parameters
The name of the component.name
string
The name of the DynamoDB Table.tableName
Input
<
string
>
-
opts?
ComponentResourceOptions
Returns Dynamo
Reference an existing DynamoDB Table with the given table name. This is useful when you create a table in one stage and want to share it in another stage. It avoid having to create a new table in the other stage.
Imagine you create a table in the dev
stage. And in your personal stage frank
,
instead of creating a new table, you want to share the table from dev
.
Here app-dev-mytable
is the name of the DynamoDB Table created in the dev
stage.
You can find this by outputting the table name in the dev
stage.
static subscribe
Parameters
The name of the subscriber.name
string
The ARN of the DynamoDB Stream to subscribe to.streamArn
Input
<
string
>
The function that’ll be notified.subscriber
Input
<
string
|
FunctionArgs
|
“arn:aws:lambda:${string}”
>
Configure the subscription.args?
DynamoSubscriberArgs
Returns Output
<
DynamoLambdaSubscriber
>
Subscribe to the DynamoDB stream of a table that was not created in your app.
For example, let’s say you have a DynamoDB stream ARN of an existing table.
You can subscribe to it by passing in the ARN.
Add a filter to the subscription.
Customize the subscriber function.
DynamoSubscriberArgs
filters?
Type Input
<
Input
<
Record
<
string
, any
>
>
[]
>
Filter the records processed by the subscriber
function.
You can pass in up to 5 different filter policies. These will logically ORed together. Meaning that if any single policy matches, the record will be processed.
For example, if your DynamoDB table’s stream contains the follow record.
To process only those records where the CustomerName
is AnyCompany Industries
.
transform?
Type Object
Transform how this subscription creates its underlying resources.
transform.eventSourceMapping?
Type EventSourceMappingArgs
|
(
args
:
EventSourceMappingArgs
,
opts
:
ComponentResourceOptions
,
name
:
string
)
=>
void
Transform the Lambda Event Source Mapping resource.