Skip to content

Instantly share code, notes, and snippets.

@acomagu
Last active March 12, 2020 06:24
Show Gist options
  • Select an option

  • Save acomagu/2fee1fbfbae3fb8a08b325cb789bfd92 to your computer and use it in GitHub Desktop.

Select an option

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.
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': '*',
},
};
};
@acomagu

acomagu commented Mar 12, 2020

Copy link
Copy Markdown
Author

Setup Lambda Proxy Integration as ANY /{proxy+} with this Lambda Function.

@acomagu

acomagu commented Mar 12, 2020

Copy link
Copy Markdown
Author
https.request(..., {
    headers: event.headers,
    ...
});

didn't work. I guess because of API Gateway special headers like apigw-requestid

@acomagu

acomagu commented Mar 12, 2020

Copy link
Copy Markdown
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