# Directly proxying Lambda via Cloudfront without API Gateway

**URL:** <https://forum.serverless.com/t/directly-proxying-lambda-via-cloudfront-without-api-gateway/3808>\
**Category:** Serverless Framework\
**Tags:** aws, lambda\
**Created:** [February 20, 2018, 12:03am UTC](https://forum.serverless.com/t/directly-proxying-lambda-via-cloudfront-without-api-gateway/3808 "2018-02-20T00:03:03Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![giorgio-zamparelli](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/giorgio-zamparelli/32/1701_2.png) [@giorgio-zamparelli](https://forum.serverless.com/u/giorgio-zamparelli)\
**Post date:** [July 16, 2018, 5:53pm UTC](https://forum.serverless.com/t/directly-proxying-lambda-via-cloudfront-without-api-gateway/3808/11 "2018-07-16T17:53:18Z")

</div>

Could you share some snippet code for the lambda@edge rewrite @jonsmirl?

I managed to add the authorization header using aws4 but the Lambda Custom Origin is returning the error:

```
504 ERROR
The request could not be satisfied.
CloudFront attempted to establish a connection with the origin, but either the attempt failed or the origin closed the connection. 
For more information on how to troubleshoot this error, please refer to the CloudFront documentation (https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-504-gateway-timeout.html). 

```

```

//lamba@edge requestRewriter.js
const aws4 = require('aws4');

exports.handler = function (event, context, callback) {

	const request = event.Records[0].cf.request;

	let opts = aws4.sign({
		service: 'lambda',
		method: 'POST',
		path: '/2015-03-31/functions/myFunc/invocations',
	}, {
		accessKeyId: 'MY_ACCESS_KEY',
		secretAccessKey: 'MY_SECRET_KEY',
	});

	request.method = opts.method;
	request.uri = opts.path;
	request.headers = Object.keys(opts.headers).reduce((headers, key) => {

		if (key !== 'Host') {
			headers[key.toLowerCase()] = [{
				key,
				value: opts.headers[key]
			}];
		}

		return headers;

	}, {
		host: request.headers.host
	});

	callback(null, request);

};

```

---

_[View the full topic](https://forum.serverless.com/t/directly-proxying-lambda-via-cloudfront-without-api-gateway/3808)._
