Hi! I'm trying to add a secondary global index to ...
# help
a
Hi! I'm trying to add a secondary global index to a
sst.Table
that already exists, with 1 existing secondary index. I've tried adding it to
TableProps.secondaryIndexes
property of the table, and also tried using
table.dynamodbTable.addGlobalSecondaryIndex()
but when I deploy, no changes are found for the stack.. any ideas? thanks!
f
Hey @Andrew Blaney, can you share a code snippet of how you are defining the
sst.Table
and adding the index?
a
hey @Frank thanks for getting back to me.. this is the code for adding it using
Copy code
table.dynamodbTable.addGlobalSecondaryIndex
as adding it to
secondaryIndexes.gsi2
property of the below
sst.Table
call didnt make any changes
Copy code
const table = new sst.Table(this, 'theTable', {
      fields: {
        pk: sst.TableFieldType.STRING,
        sk: sst.TableFieldType.STRING,
        gsi1pk: sst.TableFieldType.STRING,
        gsi1sk: sst.TableFieldType.STRING,
        gsi2pk: sst.TableFieldType.STRING,
        gsi2sk: sst.TableFieldType.STRING,
      },
      primaryIndex: {
        partitionKey: 'pk',
        sortKey: 'sk',
      },
      secondaryIndexes: {
        gsi1: {
          partitionKey: 'gsi1pk',
          sortKey: 'gsi1sk',
          indexProps: {
            projectionType: ProjectionType.ALL,
          },
        },
      },
      dynamodbTable: {
        removalPolicy: env.database.removalPolicy,
        pointInTimeRecovery: true,
        timeToLiveAttribute: 'expiresAt',
      },
      stream: true,
    });

    table.dynamodbTable.addGlobalSecondaryIndex({
      projectionType: ProjectionType.KEYS_ONLY,
      indexName: 'gsi2',
      partitionKey: { name: 'gsi2pk', type: AttributeType.STRING },
      sortKey: { name: 'gsi2sk', type: AttributeType.STRING },
    });
is this what you were expecting to see @Frank?
f
Sorry @Andrew Blaney, missed ur previous msg. Lemme give it a try.
Hey @Andrew Blaney, I just gave your code a try, it seems to work for me.
This is the entire stack definition. I had the 2nd GSI commented out and ran
sst deploy
, that created the table with
gsi1
.
Then I uncommented the 2nd GSI and ran
sst deploy
again.
Just curious, have you manually removed the GSI from the table manually (ie. from AWS console)?
^as an aside, why call
addGlobalSecondaryIndex
instead of configuring both GSIs in
TableProps.secondaryIndexes
?
a
sorry frank, I've just removed my stack and re-created it with one GSI and then added the commented out code and deployed and again and everything looks fine. thanks for your help, I'm unsure what I did wrong the first time!
f
No worries! Glad u got it to work!