I have AWS Rest API and need to generate AWS Version 4 Signature.
I searching and get many links.
Successfully generate signature but getting error like "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.".
I have AWS Rest API and need to generate AWS Version 4 Signature.
I searching and get many links.
Successfully generate signature but getting error like "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.".
- Hi Milan, wele to stackoverflow. We'll need a bit more information, have you written your own code to pute the signature or are you using a library? If you're using a library, which one? – brabster Commented Apr 30, 2018 at 7:25
- I get idea from gist.github./davidkelley/c1274cffdc0d9d782d7e – Milan Hirapra Commented Apr 30, 2018 at 7:34
2 Answers
Reset to default 4Generating an AWS v4 signature is plicated. If you're looking to learn about the process and implement it yourself, I remend the AWS documentation. There's a step-by-step walkthrough of the process, examples (including one in Javascript) and test suite. It's a good exercise to do if you're learning!
Trying to follow someone else's example without the information AWS provide is going to be tough and I don't think you'll understand as much.
If you just want to sign stuff, I'd suggest using a library rather than writing your own. AWS provide a library to use their services in the browser, and if you just want to sign requests, something like aws4 is a lightweight way to do that.
In 2024 this is now feasible using the aws-sigv4-fetch NPM package, a wrapper around the @smithy/signature-v4 package. See the snippet below. Refer to the aws-sigv4-fetch official docs for more info:
import { createSignedFetcher } from 'aws-sigv4-fetch';
const signedFetch = createSignedFetcher({
service: 'lambda', // depends on what you want to access
region: 'us-east-1',
credentials: {
accessKeyId: [YOUR_AWS_ACCESS_KEY],
secretAccessKey: [YOUR_AWS_SECRET_KEY]
}
});
const response = await signedFetch('https://your_aws_service/', {
method: 'post',
body: JSON.stringify(requestData),
headers: { 'Content-Type': 'application/json' }
});
console.log(response.status);
console.log(await response.json());
console.log(await response.arrayBuffer());