4
votes

I am using Amplify to setup a dynamodb with a corresponding lambda using the amplify blueprint for dynamodb.

Accessing the dynamodb the "classic" way with KeyConditionExpression etc works just fine but today I wanted to try and use PartiQL instead with the executeStatement and I am just not able to get it to work.

I have added the "dynamodb:PartiQLSelect" permission to the cloudfront template where all the other dynamodb permissions are so it looks like:

 "Action": [
                "dynamodb:DescribeTable",
                "dynamodb:GetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem",
                "dynamodb:PartiQLSelect"
              ],

and I do not get any permission error so I hope that part is ok, it does however return the same error even without that line added.

The error that is always returned is: ValidationException: Unexpected from source"

and no matter what I have tried, it does not help. My code is quite basic so far:

const dynamodb2 = new AWS.DynamoDB();

let tableName = "habits_sensors";
if(process.env.ENV && process.env.ENV !== "NONE") {
  tableName = tableName + '-' + process.env.ENV;
}

app.get(path, function(req, res) {
  let params = {
    Statement: `select * from ${tableName}`
  };

  dynamodb2.executeStatement(params, (err, data) => {
    if (err) {
      res.statusCode = 500;
      res.json({error: `Could not get users from : ${tableName} =>` + err});
    } else {
      res.json(data.Items);
    }
  });
});

The complete error string returned from the lambda is:

{
    "error": "Could not get users from : habits_sensors-playground =>ValidationException: Unexpected from source"
}

and I have the table habits_sensors-playground in my AWS account and I can access it the classic way without problems. That is why the "Unexpected from source" is confusing. I interpret it as referring to that the tableName (in from) in the select query is not correct but the name is matching what I have in AWS and it works using the documentclient.

Any suggestion on what might be wrong is very appreciated.

2

2 Answers

7
votes

Answering myself in case anyone else ends up here.

I got a reply from AWS that if the table name contains dashes, you need to quote the table name with double quotes when using PartiQL (I had tried single quotes and that did not work).

Maybe this will change in a future release of PartiQL.

0
votes

The exception ValidationException: Unexpected from source (CLI: An error occurred (ValidationException) when calling the ExecuteStatement operation: Unexpected from source) happens when table name contains dashes and is not quoted.

So change

aws dynamodb execute-statement --statement "SELECT * FROM my-table WHERE field='string'"

To:

aws dynamodb execute-statement --statement "SELECT * FROM \"my-table\" WHERE field='string'"

or add the double quotes " around the table name in the SDK you're using to use PartiQL.

Note that the WHERE string='value' uses single quotes ' and the table name requires double quotes ".