Hi - Is there a recommended way to populate some data in a DynamoDB table that is created as part of...
s
Hi - Is there a recommended way to populate some data in a DynamoDB table that is created as part of the deployment process? I.e. it needs to wait until the table is created before inserting the data. I’m using SST and in SST V2 we used the Script component, looking at Pulumi we could maybe use a CustomResource or a Dynamic Provider. Is there a recommendation or anyone with some experience of doing similar? I’d like to avoid the CustomResource as I believe it means using CloudFormation. When I try a Dynamic Provider like this:
Copy code
class PrimaryTablePopulatorProvider implements pulumi.dynamic.ResourceProvider {
  async create(tableName: string): Promise<pulumi.dynamic.CreateResult> {
    await seedDynamoDb(); // Function that's using @aws-sdk to populate the table
    return {
      outs: {},
      id: tableName,
    };
  }
}

class PrimaryTablePopulator extends pulumi.dynamic.Resource {
  constructor(name: string, args: TablePopulatorInputs, opts?: pulumi.CustomResourceOptions) {
    super(
      new PrimaryTablePopulatorProvider(),
      name,
      {
        ...args,
        urn: undefined,
        id: undefined,
      },
      opts,
    );
  }
}
and then inside the SST run() function I do
Copy code
new PrimaryTablePopulator(
      'primary-table-populator',
      {
        tableName: primaryTable.name,
      },
      {
        dependsOn: [primaryTable],
      },
    );
I get the error:
Copy code
Failed    
   Error: package.json export path for ".pnpm/@aws-sdk+core@3.716.0/node_modules/@aws-sdk/core/dist-cjs/submodules/account-id-endpoint/index.js" not found
       at ModuleMap.get (/Users/rosscoundon/Documents/GitHub/omw-pso-be/.sst/platform/node_modules/@pulumi/runtime/closure/package.ts:220:19)
       at Object.getModuleFromPath (/Users/rosscoundon/Documents/GitHub/omw-pso-be/.sst/platform/node_modules/@pulumi/runtime/closure/package.ts:273:35)
       at /Users/rosscoundon/Documents/GitHub/omw-pso-be/.sst/platform/node_modules/@pulumi/runtime/closure/createClosure.ts:1404:19
       at Generator.next (<anonymous>)
       at /Users/rosscoundon/Documents/GitHub/omw-pso-be/.sst/platform/node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:21:71
       at new Promise (<anonymous>)
       at __awaiter (/Users/rosscoundon/Documents/GitHub/omw-pso-be/.sst/platform/node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:17:12)
       at captureModuleAsync (/Users/rosscoundon/Documents/GitHub/omw-pso-be/.sst/platform/node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:940:20)
       at /Users/rosscoundon/Documents/GitHub/omw-pso-be/.sst/platform/node_modules/@pulumi/runtime/closure/createClosure.ts:1097:19
       at Generator.next (<anonymous>)
       at fulfilled (/Users/rosscoundon/Documents/GitHub/omw-pso-be/.sst/platform/node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:18:58)
       at processTicksAndRejections (node:internal/process/task_queues:95:5) {
     promise: Promise { <rejected> [Circular *1] }
   }
Any thoughts on how to achieve this? I’m not wed to this approach if there’s a simpler or better way. Cheers
With SST it turned out this was as simple as
Copy code
primaryTable.name.apply((name) => {
  seedDynamoDb(name).catch((err) => {
    console.error(`Error seeding DynamoDB: ${err}`);
  });
});