skip to Main Content

Is there a way in CDK with Typescript to add items to a Dynamodb Table? I know how to create the partition/sort keys, but can’t find any clear cut answer on just adding items or attributes to those items? Also how do you specify the value for the partition key? (I have the string type set, just want to add a number value to it.) Any help would be great…thank you in advance!

 const dynamoTable= new Table(this, "dynamoTable", {
      tableName: "dynamoTable",
      partitionKey: { name: 'viewer_id', type: AttributeType.STRING },
      billingMode: BillingMode.PAY_PER_REQUEST,
      tableClass: TableClass.STANDARD
    });

2

Answers


  1. Dynamodb is a non-sql database, attributes can be added dynamically at runtime.
    Such as below, you insert each row into the table, attribute will be created automatically based on your item.
    enter image description here

    You would not need to specify attributes at initial stage.

    Login or Signup to reply.
  2. Beyond attributes, no you cannot through CDK alone add items. CDK does not actually do anything other than synth the CloudFormation Template and pass that to Cloudformation. CloudFormation does not do anything other than set up resources – it can’t add items to a Dynamo for instance.

    If you want to do this, you need either a lambda or maybe just a bash script using the CLI to add items from a JSON or something – this then needs to be part of a pipeline of some kind in order to be coordinated.

    Good rule of thumb:

    if it happens Before deployment, you can do it in CDK using the aws SDK to retrieve information or do something in the account first (just be aware that everything it does is done on every single Synth, so dont do things like SDK creating roles or something).

    if it happens during or after then it needs to be handled by a pipeline of some sort.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search