Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless framework for AWS : Adding initial data into Dynamodb table

Currently I am using Serverless framework for deploying my applications on AWS.

https://serverless.com/

Using the serverless.yml file, we create the DynamoDB tables which are required for the application. These tables are accessed from the lambda functions.

When the application is deployed, I want few of these tables to be loaded with the initial set of data.

Is this possible?

Can you provide me some pointers for inserting this initial data?

Is this possible with AWS SAM?

like image 848
Dattatray Avatar asked Oct 18 '25 05:10

Dattatray


1 Answers

Don't know if there's a specific way to do this in serverless, however, just add a call to the AWS CLI like this to your build pipeline:

aws dynamodb batch-write-item --request-items file://initialdata.json

Where initialdata.json looks something like this:

{
    "Forum": [
        {
            "PutRequest": {
                "Item": {
                    "Name": {"S":"Amazon DynamoDB"},
                    "Category": {"S":"Amazon Web Services"},
                    "Threads": {"N":"2"},
                    "Messages": {"N":"4"},
                    "Views": {"N":"1000"}
                }
            }
        },
        {
            "PutRequest": {
                "Item": {
                    "Name": {"S":"Amazon S3"},
                    "Category": {"S":"Amazon Web Services"}
                }
            }
        }
    ]
}

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleData.LoadData.html

like image 102
robasaurus Avatar answered Oct 19 '25 20:10

robasaurus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!