amazon-web-servicesamazon-dynamodbaws-amplifyflutter-aws-amplify

AWS Amplify Functions allow.resource() not working


I am writing an app in Flutter and use AWS Amplify for this.

Now I am creating a simple Blacklist check in an AWS Amplify function. For this this function needs access to my database table given in my resource.ts file like this:

    BlacklistData: a.model({
        Word: a.string().required(),
      })
      //.authorization(allow => [allow.resource(blacklistPost), allow.authenticated()]) 
      .authorization(allow => [allow.authenticated()]) 
      .identifier(['Word']),

When I now take the aws amplify documentation it says you should use .authorization(allow => [allow.resource(functionWithDataAccess)]);

But when I add this in my ressource.ts file like this:

    BlacklistData: a.model({
                Word: a.string().required(),
              })
              //.authorization(allow => [allow.resource(blacklistPost), allow.authenticated()]) 
              .authorization(allow => [allow.authenticated(), allow.resource(blacklist)]) 
              .identifier(['Word']),

It always gives me an error in VS Code:

vs code error

and in the aws cli sandbox it writes me the following lines: cli output

What is wrong with my implementation that it is not working like in the documentation? Am I missing something obvious or is this a bug? Badly I was not able to solve this with KI or Googeling.


Solution

  • You are doing it wrong. You should apply authorization on entire schema, not just a specific model.

    Try this, it should work.

    const schema = a
      .schema({
        BlacklistData: a.model({
            Word: a.string().required(),
          })
      })
      .authorization(allow => [allow.resource(blacklistPost), allow.authenticated()]);