4

I have the following lambda function

const partiQl = "UPDATE '?' SET hits = hits + 1 WHERE path = \"'?'\" RETURNING ALL NEW *"
console.log(partiQl)
console.log(event.path)
console.log(process.env.HITS_TABLE_NAME)

const execStatementCommand = new ExecuteStatementCommand({
  Statement: partiQl,
  Parameters: [
    { "S": process.env.HITS_TABLE_NAME } , 
    { "S": event.path } 
  ]
});
console.log(execStatementCommand)

And the path values are basically URLs with forward slashed '/'

And I am getting this error but cannot figure out why:

2021-10-12T02:56:41.021Z    23d1ea78-48d2-4390-b100-2a3479697e4f    ERROR   Statement wasn't well formed, can't be processed: Expected identifier for simple path

the event.path value for the test run is /test and HITS_TABLE is passed down from the CDK call. any ideas?

UPDATE

I have change the partiQL statement to this and the error changed to table name not being supported.

const partiQl = `UPDATE "?" SET hits = hits + 1 WHERE "path" = "?" RETURNING ALL NEW *`

here is the new error:

ERROR   
Table name should be within the length [3, 255] 
and only contain 'a-z', 'A-Z', '0-9', '-', '_', '.'.

If I include the arguments directly in the string, it seems like it fixes the PartiQl issue though:

const partiQl = `UPDATE "${process.env.HITS_TABLE_NAME}" SET hits = hits + 1 ` + 
  `WHERE "path" = "${event.path}" RETURNING ALL NEW *`
console.log(partiQl)

const execStatementCommand = new ExecuteStatementCommand({
  Statement: partiQl
});

There are no good examples for PartiQl and AWS SDK v3, any ideas to why?

UPDATE

using the second method, I get the following error

User: arn:XXXX is not authorized to perform: dynamodb:PartiQLUpdate on resource: arn:YYYYY

even though I have granted read and write permission to the table using this method:

table.grantReadWriteData(this.handler);

I have looked at https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-iam.html

and the list of permission I need the lambda to have are:

"dynamodb:PartiQLInsert",
"dynamodb:PartiQLUpdate",
"dynamodb:PartiQLDelete",
"dynamodb:PartiQLSelect"

do I have to create a custom IAM policy to add these? Sorry I am very new to AWS in general.

UPDATE

after adding the following policy rule, the following error occurs:

  this.handler.role?.addToPolicy(new iam.PolicyStatement({
    effect: iam.Effect.ALLOW,
    resources: [table.tableArn],
    actions: [
      'dynamodb:PartiQLUpdate', 
      'dynamodb:PartiQLSelect',
      'dynamodb:PartiQLInsert',
      'dynamodb:PartiQLDelete'
    ]
  }));

and here is the following error:

ERROR   Where clause does not contain a mandatory equality on all key attributes

I assume it's because the update call is not finding any rows.

ArmenB
  • 2,125
  • 3
  • 23
  • 47

0 Answers0