Created
February 4, 2019 09:09
-
-
Save aizatto/e0bda80e78dd18765b3fc96772bd0ec8 to your computer and use it in GitHub Desktop.
aws4 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch'); | |
const aws4 = require('aws4'); | |
process.on('unhandledRejection', (reason) => { | |
console.log('Reason: ' + reason); | |
}); | |
async function main() { | |
const headers = { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json', | |
}; | |
const method = 'POST'; | |
const body = JSON.stringify({ | |
url: 'https://www.example.com/', | |
}); | |
const request = aws4.sign({ | |
service: 'execute-api', | |
region: 'ap-southeast-1', | |
path: '/dev/', // this should be your stage | |
host: 'API_GATEWAY_ID.execute-api.REGION.amazonaws.com', // API Gateway host | |
method, | |
headers, | |
body, | |
}, | |
// Or use environment variables | |
// | |
// - AWS_ACCESS_KEY | |
// - AWS_ACCESS_KEY_ID | |
// - AWS_SECRET_KEY | |
// - AWS_SECRET_ACCESS_KEY | |
// - AWS_SECRET_TOKEN | |
// | |
// See https://github.com/mhart/aws4/blob/e2052432f836af766b33ce5782a3bfa21f40db99/aws4.js#L285 | |
{ | |
accessKeyId: '', | |
secretAccessKey: '', | |
}); | |
const response = await fetch( | |
// Invoke URL | |
'https://API_GATEWAY_ID.execute-api.REGION.amazonaws.com/STAGE/', | |
{ | |
headers: request.headers, | |
method, | |
body, | |
} | |
); | |
if (response.status !== 200) { | |
throw new Error(await response.text()); | |
} | |
let json = await response.json(); | |
console.log(json); | |
return json; | |
} | |
try { | |
main(); | |
} catch (error) { | |
console.error(error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment