2
votes

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?

2

2 Answers

1
votes

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

0
votes

A more Serverless Framework option is to use a tool like the serverless-plugin-scripts plugin that allows you to add your own CLI commands to the deploy process by default:

https://github.com/mvila/serverless-plugin-scripts