Closed sirkupkup closed 3 years ago
I can't really help with axios I'm afraid – I don't know it at all. aws4
needs a host
and a path
(it doesn't use url
) – you sure the path
is correct here? Shouldn't it be /wss/@connections/${connectionId}
?
Thank you, you are right!
I actually managed to solve it jumping it a bit to your code (unfortunately saw your answer only afterwards).
You are correct - url
and body
is for axios.
For anybody else out there, here is the finished version. Hope it helps somebody:
const data = {'success': true};
const request = {
host: process.env.AWS_API_GATEWAY_ENDPOINT,
method: 'POST',
url: `https://${process.env.AWS_API_GATEWAY_ENDPOINT}/wss/@connections/${connectionId}`,
path: `/wss/@connections/${connectionId}`,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
data,
}
const signedRequest = aws4.sign(request,
{
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
sessionToken: process.env.AWS_SESSION_TOKEN
})
delete signedRequest.headers['Host']; // It otherwise messes with axios
const response = await axios(signedRequest);
@sirkupersmidt did you make it work with API GW v1 or v2? I'm also getting 403 with the same code as you posted. Thanks.
@amouly here is a link to a post i wrote that explains exactly how to work this thing out :)
https://stackoverflow.com/a/67824275/10313866
Good luck!
Hi @mhart @sirkupersmidt ,
I have stuck with same error but with SQS
Here is my code
`let requestSqs = { host: "sqs.us-east-1.amazonaws.com", region: "us-east-1", service: "sqs", path: "/?Action=SendMessage", method: "GET", headers: { "Content-Type": "application/x-www-form-urlencoded", Host: "sqs.us-east-1.amazonaws.com", "X-Amz-Date": amzDate, }, };
let signedRequest = aws4.sign(requestSqs, { accessKeyId: "*****", secretAccessKey: "****", });
delete signedRequest.headers["Host"]; delete signedRequest.headers["Content-Length"];
const response = await fetch(
https://sqs.us-east-1.amazonaws.com/*************/sqsname/?Action=SendMessage&MessageBody=javedrahman
,
signedRequest
);
`
But this was working fine in postman. Looking for your reply
Thanks in advance
Need some more info - what error are you getting for example? Is it possible a perms issue?
Hi,
I'm trying to sign a request that I'm sending to an endpoint on API Gateway. I'm sending this from a lambda that handles websocket connections.
Here is the data-part of the response I am getting when sending the request through axios (redacted parts of this as I dont know what could be compromising):
Here is the code I'm using to create and send the request:
Thanks for any help!