amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

[Feature Request]Add an option of ProjectionType for Dynamodb Global Secondary Index (Amplify Gen2)

Open MyNameIsTakenOMG opened this issue 1 year ago • 7 comments

Environment information

System:
  OS: macOS 14.6.1
  CPU: (8) arm64 Apple M1 Pro
  Memory: 122.77 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 18.20.4 - /usr/local/bin/node
  Yarn: 1.22.19 - /opt/homebrew/bin/yarn
  npm: 10.7.0 - /usr/local/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.0
  @aws-amplify/backend: 1.1.1
  @aws-amplify/backend-auth: 1.1.3
  @aws-amplify/backend-cli: 1.2.5
  @aws-amplify/backend-data: 1.1.2
  @aws-amplify/backend-deployer: 1.1.0
  @aws-amplify/backend-function: 1.3.4
  @aws-amplify/backend-output-schemas: 1.2.0
  @aws-amplify/backend-output-storage: 1.1.1
  @aws-amplify/backend-secret: 1.1.0
  @aws-amplify/backend-storage: 1.1.2
  @aws-amplify/cli-core: 1.1.2
  @aws-amplify/client-config: 1.2.1
  @aws-amplify/deployed-backend-client: 1.4.0
  @aws-amplify/form-generator: 1.0.1
  @aws-amplify/model-generator: 1.0.5
  @aws-amplify/platform-core: 1.0.6
  @aws-amplify/plugin-types: 1.2.1
  @aws-amplify/sandbox: 1.2.0
  @aws-amplify/schema-generator: 1.2.1
  aws-amplify: 6.5.2
  aws-cdk: 2.154.1
  aws-cdk-lib: 2.154.1
  typescript: 5.5.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Describe the feature

When customizing secondary indexes with .secondaryIndexes((index) => [index("GSI-pk")]) by following the documentation, we need a way to configure the ProjectionType(KEYS_ONLY, INCLUDE , or ALL), for example, add a method .projection(<type>, [fields]?), so that we could specify which fields would be projected into our GSI tables.

By the time I am writing, the amplify backend will provision our GSIs with ProjectionType of ALL, which in my opinion is not recommended, because it might incur higher storage costs and could slow down write operations on the base table since updates will be replicated across all GSIs.

Use case

In my case, I am designing my table following one-table design, which contains multiple GSI tables. However, as I mentioned above, right now the amplify backend would provision GSIs with projectionType of ALL, which means my table data would be replicated for multiple times, which could incur a lot costs.

In addition, the projectionType cannot be modified once the GSI is provisioned, which means I either have to manually delete those GSIs and re-create them with proper ProjectionType or create those GSIs myself via aws console, cli, scripts, or custom resource

MyNameIsTakenOMG avatar Oct 11 '24 14:10 MyNameIsTakenOMG

Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂

ykethan avatar Oct 11 '24 17:10 ykethan

Hey @MyNameIsTakenOMG, Thanks for requesting this. We are marking this as a feature request for the team to evaluate further.

AnilMaktala avatar Oct 21 '24 13:10 AnilMaktala

I am experiencing the same issue; I am getting a complete list of attributes returned, which is highly inefficient. Are there any fixes or workarounds for this?

jaouad-ankour avatar Jan 08 '25 18:01 jaouad-ankour

+1

Same situation. I have indexes that only need a few fields projected and would like to avoid the storage and cost waste.

Workarounds explored:

There is a way to override the configuration of tables using the AmplifyDynamoDbTableWrapper like so:


Object.values(
    backend.data.resources.cfnResources.amplifyDynamoDbTables,
  ).forEach((table) => {
    table.timeToLiveAttribute = {
      enabled: true,
      attributeName: '_ttl',
    };

  });

...however, there is no interface exposed to configure globalSecondaryIndexes.

naedx avatar Apr 15 '25 21:04 naedx

There's also the (less than ideal) workaround to define the table using the CDK and attach it as a data source.

It's not clear to me how nicely this will play with the Amplify sandbox and deployment though...

naedx avatar Apr 15 '25 22:04 naedx

I'm facing the same issue. Any update on this feature request?

mkaivs avatar Jul 07 '25 20:07 mkaivs

@AnilMaktala @ykethan

Is there any new update on this feature request?

mkaivs avatar Jul 18 '25 22:07 mkaivs