Table
Resource Overview
A Table represents an Amazon DynamoDB table. DynamoDB is a NoSQL database that offers predictable performance and is perfect for storing the data of your serverless applications.
Key Features of DynamoDB include:
- Fully-managed by AWS
- Ability to store an unlimited amount of data
- Pricing based on throughput capacity instead of storage
- Secure authentication methods to protect data
- Highly available data via automatic Multi-AZ replication
Your tables partition data based on a Hash Key (also known as a Partition Key). The Hash Key is a value that all records must have, and is typically used to identify or segment records. Tables may optionally have a Range Key (also known as a Sort Key), which sorts records with the same Hash Key according to the Range Key value. See the DynamoDB Core Components documentation for a more complete discussion of keys, records, and attributes.
Event Subscription
Event subscription wires (solid line) visualize and configure event subscription integrations between two resources.
The following resources can be subscribed to a Table:
- Function
Service Discovery
Service discovery wires (dashed line) provide compute resources (Function, Edge Function, Docker Task) with the permissions and environment variables required to perform actions using cloud resources within the stack. This resource is on the receiving end of a service discovery wire originating from compute resources.
The following compute resources can use a service discovery wire to access a Secrets resource:
- Function
Configurable Properties
Display Name
Human readable name for this resource that is displayed on the Stackery Dashboard and Stackery CLI output.
Logical ID
The unique identifier used to reference this resource in the stack template. Defining a custom Logical ID is recommended, as it allows you to quickly identify a resource and any associated sub-resources when working with your stack in AWS, or anywhere outside of the Stackery Dashboard. As a project grows, it becomes useful in quickly spotting this resource in template.yaml
or while viewing a stack in Template View mode.
The Logical ID of all sub-resources associated with this Table will be prefixed with this value.
The identifier you provide must only contain alphanumeric characters (A-Za-z0-9) and be unique within the stack.
Default Logical ID Example: Table2
IMPORTANT : AWS uses the Logical ID of each resource to coordinate and apply updates to the stack when deployed. On any update of a resource's logical ID (or any modification that results in one), CloudFormation will delete the currently deployed resource and create a new one in it's place when the updated stack is deployed.
Hash Key Name
In DynamoDB tables, there is a field required in every row that is used for indexing. No two items in your DynamoDB table can have the same Hash Key value so be sure to choose a unique identifier. Hash key names may contain only letters, numbers, and underscores.
The Hash Key Name may not be changed after deployment.
Hash Key Type
The type of your Hash Key attribute.
Range Key Name (optional)
If a Range Key is used, the name of the Range Key attribute and its type. Range Key names may contain only letters, numbers, and underscores.
The Range Key Name may not be changed after deployment.
Range Key Type (optional)
The type of your Range Key attribute.
Expiration Key (optional)
If an Expiration Key is used, the name of the Expiration Key attribute. The attribute contains a timestamp indicating when the item should be deleted.
Use Existing DynamoDB Table
When enabled, this feature provides you with a field to specify the Amazon Resource Name (ARN) of an existing DynamoDB Table to reference in your application.
You may reference an environment parameter in order to conditionally reference existing infrastructure based on environment.
IAM Permissions
When connected by a service discovery wire (dashed wire), a Function or Docker Task will add the following IAM statement/policy to its role and gain permission to access this resource.
DynamoDBCrudPolicy
Grants a Function or Docker Task permission to create, read, update, and delete items from a Table resource within the stack or an existing DynamoDB Table outside of the current stack.
Environment Variables
When connected by a service discovery wire (dashed wire), a Function or Docker Task will automatically populate and reference the following environment variables in order to interact with this resource.
TABLE_NAME
The Logical ID of the Table resource.
Example: Table2
TABLE_ARN
The Amazon Resource Name of the Table.
Example: Table2.Arn
AWS SDK Code Example
Language-specific examples of AWS SDK calls using the environment variables discussed above.
Writing items to a Table
// Load AWS SDK and create a new DynamoDB object
const AWS = require("aws-sdk");
const dynamodb = new AWS.DynamoDB.DocumentClient();
const tableName = process.env.TABLE_NAME // supplied by Function service-discovery wire
exports.handler = async message => {
// Construct parameters for the batchWriteItem call
const params = {
TableName: tableName,
Item: {
id: '1',
ItemName: 'My item',
User: 'Amy',
Account: '13246548',
},
ConditionExpression: 'attribute_not_exists(id)', // do not overwrite existing entries
ReturnConsumedCapacity: 'TOTAL'
};
const response = await dynamodb.put(params).promise();
console.log('Item added to table: ' + tableName);
return response;
}
import boto3
import os
# Create an DynamoDB client
dynamodb = boto3.client('dynamodb')
table_name = os.environ['TABLE_NAME'] # Supplied by Function service-discovery wire
table = dynamodb.Table(table_name)
def handler(message, context):
# Add items to your DynamoDB Table
params = {
'id': '1',
'ItemName': 'This is my content'
'User': 'Amy'
'Account': '13246548'
}
response = table.put_item(Item = params, ConditionExpression='attribute_not_exists(id)') # do not overwrite existing entries
print('Item added to table: ' + table_name)
return response
Metrics & Logs
Double clicking a resource while viewing your stack's current deployment gives you access to your pre-configured resource properties, and the following metrics and logs.
- Read Capacity Units
- Throttled Reads
- Write Capacity Units
- Throttled Writes
- Request Latency
- Error Metrics
Related AWS Documentation
AWS Documentation: AWS::DynamoDB::Table