CloudFront Lambda @Edge SigV4 IAM API-. API- CloudFront ().
, API- CloudFront, () Lambda @Edge, , API-, . IAM AmazonAPIGatewayInvokeFullAccess
Lambda, API .
Then, if you use aws4 as a signing client, your lambda code will look like this:
const aws4 = require("aws4");
const signCloudFrontOriginRequest = (request) => {
const searchString = request.querystring === "" ? "" : '?${request.querystring}';
const dummyRequest = {
host: request.origin.custom.domainName,
method: request.method,
path: '${request.origin.custom.path}${request.uri}${searchString}',
};
if (Object.hasOwnProperty.call(request, 'body')) {
const { data, encoding } = request.body;
const buffer = Buffer.from(data, encoding);
const decodedBody = buffer.toString('utf8');
if (decodedBody !== '') {
dummyRequest.body = decodedBody;
dummyRequest.headers = { 'content-type': request.headers['content-type'][0].value };
}
}
const credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN
};
aws4.sign(dummyRequest, credentials);
const signedRequest = JSON.parse(JSON.stringify(request));
signedRequest.headers.authorization = [ { key: "Authorization", value: dummyRequest.headers.Authorization } ];
signedRequest.headers["x-amz-date"] = [ { key: "X-Amz-Date", value: dummyRequest.headers["X-Amz-Date"] } ];
signedRequest.headers["x-amz-security-token"] = [ { key: "X-Amz-Security-Token", value: dummyRequest.headers["X-Amz-Security-Token"] } ];
return signedRequest;
};
const handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const signedRequest = signCloudFrontOriginRequest(request);
callback(null, signedRequest);
};
module.exports.handler = handler;
Please note that if you include the body in your request, you will have to manually configure the Lambda @Edge function to include the body through the console or SDK, or configure the CloudFormation user resource to call the SDK, since CloudFormation does not support it initially
source
share