amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

How to add Global secondary Index with amplify-cli

Open bokuweb opened this issue 7 years ago • 8 comments

Note: If your question is regarding the AWS Amplify Console service, please log it in the official AWS Amplify Console forum

** Which Category is your question related to? **

AppSync with GSI

** What AWS Services are you utilizing? **

appsync

** Provide additional details e.g. code snippets **

I don't know how to add GSI. I tried to copy amplify/api/MYAPP/build/stacks/resource.json to amplify/api/MYAPP/stacks/resource.json and add new GSI like following.

                "GlobalSecondaryIndexes": [
                    {  // ommitted  }
                    {
                        "IndexName": "gsi-FooCategory",
                        "KeySchema": [
                            {
                                "AttributeName": "Category",
                                "KeyType": "HASH"
                            },
                            {
                                "AttributeName": "createdAt",
                                "KeyType": "RANGE"
                            }
                        ],
                        "Projection": {
                            "ProjectionType": "ALL"
                        },
                        "ProvisionedThroughput": {
                            "Fn::If": [
                                "ShouldUsePayPerRequestBilling",
                                {
                                    "Ref": "AWS::NoValue"
                                },
                                {
                                    "ReadCapacityUnits": {
                                        "Ref": "DynamoDBModelTableReadIOPS"
                                    },
                                    "WriteCapacityUnits": {
                                        "Ref": "DynamoDBModelTableWriteIOPS"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        },

However this chnages cause error when amplify api push.

UPDATE_FAILED               FooyTable                                                                AWS::DynamoDB::Table       Thu Mar 21 2019 12:18:07 GMT+0900 (GMT+09:00) Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes

UPDATE_FAILED      Foo      AWS::CloudFormation::Stack Thu Mar 21 2019 11:26:04 GMT+0900 (GMT+09:00) Embedded stack arn:aws:cloudformation:ap-northeast-1:xxxxxxxx:stack/lawgue-beta-xxxxxxx-MYAPP-xxxxxxxx-Foo-XXXXXXXXXX was not successfully updated. Currently in UPDATE_ROLLBACK_IN_PROGRESS with reason: The following resource(s) failed to update: [FooTable, ListFooResolver].

Currently is it possible to add GSI ? If so what should I do to add it ?

bokuweb avatar Mar 21 '19 02:03 bokuweb

I needed to edit "AttributeDefinitions" for GSI.

Should I add FooTable definition to stacks/CustomResources.json's Resources fileld ??

bokuweb avatar Mar 21 '19 02:03 bokuweb

I tried to add FooTable definition to stacks/CustomResources.json's Resources fileld. It looks fine. However it can not resolve "GetAttGraphQLAPIApiId" reference when pushed. How do I resolve "GetAttGraphQLAPIApiId" in stacks/CustomResources.json??

bokuweb avatar Mar 21 '19 04:03 bokuweb

It could work with editing build/stacks/YourTypeName.json, and deploy with amplify push --no-gql-override, but this looks like a hack more than a solution.

If you need GetAttGraphQLAPIApiId, here suggests your template of custom resources in stacks/YourCustomResources.json should use AppSyncApiId, look like

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "An auto-generated nested stack.",
  "Metadata": {},
  "Parameters": {
    "AppSyncApiId": {
      "Type": "String",
      "Description": "The id of the AppSync API associated with this project."
    },
    "env": {
      "Type": "String",
      "Description": "The environment name. e.g. Dev, Test, or Production",
      "Default": "NONE"
    }
  },
  "Resources": {
    "MyTestResolver": {
      "Type": "AWS::AppSync::Resolver",
      "Properties": {
        "ApiId": {
          "Ref": "AppSyncApiId"
        },
       "OtherProps": "Not shown here"
     }
   }
}

https://aws-amplify.github.io/docs/cli/graphql#add-a-custom-resolver-that-targets-an-aws-lambda-function And if you also need to customize others, add to parameter.json and write a corresponding block in Parameters

kftsehk avatar Mar 21 '19 08:03 kftsehk

@kftsehk Thanks for your reply :)

but this looks like a hack more than a solution I know... 😢 However I can not find better way doing this now...

I'll try it 😄 Thanks :)

bokuweb avatar Mar 21 '19 10:03 bokuweb

We are working on adding support for custom indexes. Please see https://github.com/aws-amplify/amplify-cli/issues/1062 and offer feedback.

mikeparisstuff avatar Mar 21 '19 20:03 mikeparisstuff

We launched support for custom indexes today. You can find docs for the same out here - https://aws-amplify.github.io/docs/cli/graphql#key Please let us know if you're still not able to solve your problem through this solution and we'll re-open this issue for you.

kaustavghosh06 avatar May 30 '19 05:05 kaustavghosh06

I'm still having some issue. the @key directive only creates indexes on "not null" and it should be an existing field. Is anyone knows how can I create an index on the budgetCategoryParentId in this type: type BudgetCategory @model { id: ID! title: String! items: [BudgetItem]! @connection(name: "BudgetCategoryItems") children: [BudgetCategory]! parent: BudgetCategory @connection(name:"BudgetCategoryParent", keyField: "budgetCategoryParentId") }

im-dnsosso avatar Jun 11 '19 12:06 im-dnsosso

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 27 '21 07:05 github-actions[bot]