#✅ - Authorize custom handler to perform "dynamodb:BatchWriteItem"

21 messages · Page 1 of 1 (latest)

winter goblet
#

How can I authorize a custom function handler to perform a "dynamodb:BatchWriteItem"?

Here is my mutation:

{
...otherPartsOfSchema
 likePost: a
      .mutation()
      .arguments({
        postId: a.string(),
      })
      .returns(a.json())
      .authorization((allow) => [allow.publicApiKey()])
      .handler(
        a.handler.custom({
          dataSource: a.ref('Todo'),
          entry: './increment-like.js',
        })
      )
}

In my increment-like.js file, I can successfully call a non-batch write operation like "UpdateItem", but can not call an operation like "BatchPutItem".

Here's the successful UpdateItem operation

export function request(ctx) {
  return {
    operation: 'UpdateItem',
    key: util.dynamodb.toMapValues({ id: ctx.args.id }),
    update: {
      expression: 'SET content = :newContent',
      expressionValues: { ':newContent': { S: "THIS HAS BEEN UPDATED" } },
    }
  }
}

Can you provide an example of how to authorize a custom handler to do batch writes?

winter goblet
#

Authorize custom handler to perform "dynamodb:BatchWriteItem"

winter goblet
prime nebula
#

Cc @fossil jackal

#

Thanks Stephen, we’re still working on the best way to pass in the table names dynamically. Our current thinking is to push it into the stash object but we’re still evaluating different approaches to make sure the solution scales with the table count.

winter goblet
#

@prime nebula Got it. Do you have any examples of doing this?

prime nebula
#

Sorry I meant baking it into the Amplify capability directly.

#

Rn I have a solution that’s “so so” by using environment variables but it does require some hard coding of the environment variable

winter goblet
#

Do you mind sharing?

#

I'll take anything hah

#

I know there's some discussion of this on the repo

prime nebula
#

Let me see if I can find it. (Just got a new computer... sorry)

winter goblet
#

I appreciate it!

prime nebula
#

But let me describe it first:

  • you deploy your application without the batch resolver, (so your table is available)
  • then, you grab the table name, then paste it into the Amplify console's build time environment variable
  • in the backend.ts file you set at AppSync environmentVariable by doing backend.data.cfnResources.cfnGraphqlApi.enviromentVariables...
  • and set a value of TODO_TABLE: process.env....
  • then in your batch resolver code you can have ctx.env.TODO_TABLE as a valid value
#

it's still somewhat hardcoded, so it's still kinda "so so".

#

I can't find the repo rn for the life of me. I'll need to look on my old computer some other day sorry.

winter goblet
#

Ok thanks so much! The demo really saved me.

misty valve
#

@prime nebula Do we have any kind of timeline for baking dynamic Dynamodb table names into Amplify Gen2?

grand matrix
#

I need this as well, or even better, set our own table names instead with a provided string

rotund bobcatBOT
#

✅ - Authorize custom handler to perform "dynamodb:BatchWriteItem"