Amplify-cli: How to add Global secondary Index with amplify-cli

Created on 21 Mar 2019  路  7Comments  路  Source: aws-amplify/amplify-cli

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 ?

feature-request graphql-transformer

Most helpful comment

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.

All 7 comments

I needed to edit "AttributeDefinitions" for GSI.

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

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??

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 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 :)

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

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.

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") }

Was this page helpful?
0 / 5 - 0 ratings