A simple CDK JSON seeder for DynamoDB
Project description
aws-cdk-dynamodb-seeder ![Mentioned in Awesome CDK](https://pypi-camo.freetls.fastly.net/a7f9ec91914eb8db0d16705a853ffd50570a76ce/68747470733a2f2f617765736f6d652e72652f6d656e74696f6e65642d62616467652e737667)
A simple CDK JSON seeder for DynamoDB
Why this package
Glad you asked!
Using AWS CDK for automating infrastructure deployments is an amazing way of integrating the development and operations into one process and one codebase.
However, building dev or test environments that come pre-populated with data can be tricky, especially when using Amazon DynamoDB.
How do I use it
Install using your favourite package manager:
yarn add aws-cdk-dynamodb-seeder
Example usage
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from aws_cdk_dynamodb_seeder import Seeder
my_table = Table(stack, "MyTable",
table_name="MyTable",
partition_key={"name": "Id", "type": AttributeType.STRING}
)
Seeder(stack, "MySeeder",
table=my_table,
setup=require("./items-to-put.json"),
teardown=require("./keys-to-delete.json"),
refresh_on_update=True
)
For a more in-depth example, see: elegantdevelopment/aws-cdk-dynamodb-seeder-examples.
Importing seed data
Data passed into setup
("Items" to put) or teardown
("Keys" to delete) should be an array
of JavaScript objects (that are, in turn, representations of string
to AttributeValue maps).
setup
elements should use the format ofparams.Item
from AWS.DynamoDB.DocumentClient.put()teardown
elements should use the format ofparams.Key
from AWS.DynamoDB.DocumentClient.delete()
Versioning
We will attempt to align the major and minor version of this package with AWS CDK, but always check our release descriptions for compatibility.
Internals
Behind the scenes we use an AwsCustomResource as a representation of the related table's seed state. The custom resource's event handlers invoke a Function to perform setup and/or teardown actions.
Deploying a stack
On deployment, we write copies of your seed data locally and use a BucketDeployment to write it to an S3 Bucket.
We then create the handler function and custom resource to field seed requests (the onCreate
event will immediate fire as the stack deploys, reading the data from the bucket and seeding the table using AWS.DynamoDB.DocumentClient).
Updating a stack
On a stack update, the onUpdate
handler is triggered when Seeder.props.refreshOnUpdate
is true
.
This will run AWS.DynamoDB.DocumentClient.delete() on every teardown "Key" followed by AWS.DynamoDB.DocumentClient.put() on every setup "Item".
Destroying a stack
When the stack is destroyed, the event handler's onDelete
function will be invoked, providing Seeder.props.teardown
is set.
This simply runs AWS.DynamoDB.DocumentClient.delete() on every teardown "Key" before destroying the Seeder
's resources.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for aws-cdk-dynamodb-seeder-1.26.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d47142185ab7569b3b1a249046f861ce81e62e8cb979c04478bbd28d579c2d5 |
|
MD5 | ea8104dc2bd7c5f400f3ae336637bfde |
|
BLAKE2b-256 | 32161e1f00b2c7cc3063a8630ac84b2a61772bd2c958acb8388d48e2b07ff190 |
Hashes for aws_cdk_dynamodb_seeder-1.26.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6942dcab997ff9ba879f196f68c414f12806314bbc7dd02fc7e63fdedf1c752 |
|
MD5 | 2468ed4b8199b9e85669a0fb232d2d27 |
|
BLAKE2b-256 | ccf5e09b4be94cf2a3a28570d7c5cd8d1d0f05fbaee199c2543146b05f85b876 |