Last active
March 12, 2020 06:24
-
-
Save acomagu/2fee1fbfbae3fb8a08b325cb789bfd92 to your computer and use it in GitHub Desktop.
The Lambda Handler to proxy any other API Gateway/HTTP endpoints. This accepts only Authorization header as the request and adds CORS headers to the response. No dependencies.
This file contains hidden or 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 https = require('https'); | |
| exports.handler = async (event) => { | |
| console.log(event); | |
| const [res, body] = await new Promise((resolve, reject) => { | |
| const req = https.request(`https://example.com/${event.pathParameters.proxy}`, { | |
| method: event.httpMethod, | |
| headers: { 'authorization': event.headers.Authorization }, | |
| }, (res) => { | |
| let body = ''; | |
| res.on('data', dat => body += dat); | |
| res.on('end', () => resolve([res, body])); | |
| }); | |
| req.end(event.body); | |
| }); | |
| return { | |
| statusCode: res.statusCode, | |
| body, | |
| headers: { | |
| ...res.headers, | |
| 'access-control-allow-origin': '*', | |
| }, | |
| }; | |
| }; |
Author
Author
https.request(..., {
headers: event.headers,
...
});didn't work. I guess because of API Gateway special headers like apigw-requestid
Author
Note that some AWS internal endpoints don't accepts Transfer-Encoding: chunked body.
Below sends chunked body:
req.send(body);
req.end();Below does non-chunked body:
req.end(body);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup Lambda Proxy Integration as
ANY /{proxy+}with this Lambda Function.