I had build an App using React Native and Amplify. First time using AWS it was a bit hard to leanr the complexity of AWS, but i feel like it would be better to remove AWS from the project and add custom server or Firebase.
The current problem is that i made a Lambda function to and after that an API. When testing the API from the AWS Console i get the response i want, but when trying to test it iside my App, i keep getting this error: [InvalidSignatureException: Unknown error] nad oppening the debigger i get a more detailed error with the message: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method.
Error generating client number: Object$metadata:
_response: {statusCode: 403, headers: {…}, body: `{“message”:”The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.nnThe Canonical String for this request should have beenn’GETn/staging/get-new-client-numbernncontent-type:application/json;
This is my function:
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
const COUNTER_TABLE_NAME = 'ClientCounter'; // DynamoDB table to store the counter
/**
* @type {import('@types/aws-lambda').APIGatewayProxyHandler}
*/
exports.handler = async (event) => {
const body = {
firstName: "Deleu",
lastName: "Eduard",
birthdate: "1998-12-01"
}
const { firstName, lastName, birthdate } = body //JSON.parse(event.body); // Extract user data from the request body
try {
const clientNr = await getNextClientNumber(birthdate, firstName, lastName);
console.log('Generated Client Number:', clientNr);
return {
statusCode: 200,
body: JSON.stringify({ message: "Client number generated successfully", clientNr: clientNr }),
};
} catch (err) {
console.error('Error:', err);
return {
statusCode: 500,
body: JSON.stringify({ message: "Error generating client number", error: err.message }),
};
}
};
const getNextClientNumber = async (birthdate, firstName, lastName) => {
const birthdateFormatted = birthdate.replace(/-/g, '');
const initials = `${firstName[0]}${lastName[0]}`.toUpperCase();
const counter = await incrementCounter();
return `PF-${birthdateFormatted}${initials}${counter}`;
};
const incrementCounter = async () => {
const params = {
TableName: 'ClientCounter',
Key: { id: 'clientCounter' },
UpdateExpression: 'set counterValue = counterValue + :inc',
ExpressionAttributeValues: { ':inc': 1 },
ReturnValues: 'UPDATED_NEW'
};
const result = await docClient.update(params).promise();
const counterValue = result.Attributes.counterValue;
return counterValue.toString().padStart(5, '0');
};
I tried playing with policy of IAM Roles and API Resources Policy in the hope to see a change in the Error output. But no luck, i wasted 2 days on this.
1