Trong phần này, chúng ta sẽ tạo một Lambda@Edge function.
1 - Truy cập vào AWS Console và đảm bảo rằng bạn đang ở khu vực US-EAST-1 N. Virginia.
2 - Truy cập vào Lambda console và chọn Create function
Thực hiện cấu hình:
{
"Records": [
{
"cf": {
"config": {
"distributionDomainName": "d123.cloudfront.net",
"distributionId": "EDFDVBD6EXAMPLE",
"eventType": "viewer-request",
"requestId": "MRVMF7KydIvxMWfJIglgwHQwZsbG2IhRJ07sn9AkKUFSHS9EXAMPLE=="
},
"request": {
"clientIp": "2001:0db8:85a3:0:0:8a2e:0370:7334",
"method": "GET",
"querystring": "size=large",
"uri": "/picture.jpg",
"headers": {
"host": [
{
"key": "Host",
"value": "d111111abcdef8.cloudfront.net"
}
],
"user-agent": [
{
"key": "User-Agent",
"value": "curl/7.51.0"
}
]
},
"origin": {
"custom": {
"customHeaders": {
"my-origin-custom-header": [
{
"key": "My-Origin-Custom-Header",
"value": "Test"
}
]
},
"domainName": "example.com",
"keepaliveTimeout": 5,
"path": "/custom_path",
"port": 443,
"protocol": "https",
"readTimeout": 5,
"sslProtocols": [
"TLSv1",
"TLSv1.1"
]
},
"s3": {
"authMethod": "origin-access-identity",
"customHeaders": {
"my-origin-custom-header": [
{
"key": "My-Origin-Custom-Header",
"value": "Test"
}
]
},
"domainName": "my-bucket.s3.amazonaws.com",
"path": "/s3_path",
"region": "us-east-1"
}
}
}
}
}
]
}
Bây giờ chúng ta sẽ viết một hàm để tạo ra HTML được tạo.
Hãy sao chép và dán mã sau vào cửa sổ mã hàm:
const handler = (event, context, callback) => {
console.log("Request Event:" + JSON.stringify(event, null, 2));
const requestHeaders = event.Records[0].cf.request.headers;
var htmlContent;
//Insert code to generate the html content content here.
const response = {
status: '200',
statusDescription: 'OK',
headers: {
'cache-control': [{
key: 'Cache-Control',
value: 'max-age=100'
}],
'content-type': [{
key: 'Content-Type',
value: 'text/html'
}],
'content-encoding': [{
key: 'Content-Encoding',
value: 'UTF-8'
}],
},
body: htmlContent,
};
callback(null, response);
};
module.exports.handler = handler;
exports.handler = (event, context, callback) => {
const requestHeaders = event.Records[0].cf.request.headers;
let str = '<table border="1" width="100%">' +
'<thead>' +
'<tr><td><h1>Header</h1></td><td><h1>Value</h1></td></tr>' +
'</thead>' +
'<tbody>';
for (const key in requestHeaders) {
if (requestHeaders.hasOwnProperty(key)) {
str += '<tr><td>' + key + '</td><td>' + requestHeaders[key][0].value + '</td></tr>';
}
}
str += '</tbody></table>';
const htmlContent = `
<html lang="en">
<body>
<table border="1" width="100%">
<thead>
<tr><td><h1>Lambda@Edge Lab</h1></td></tr>
</thead>
<tfoot>
<tr><td>Immersion Days - Edge Services - Module 3</td></tr>
</tfoot>
<tbody>
<tr><td>Response sent by API</td></tr>
</tbody>
<tbody>
<tr><td>${str}</td></tr>
</tbody>
</table>
</body>
</html>
`;
const response = {
status: '200',
statusDescription: 'OK',
headers: {
'cache-control': [{ key: 'Cache-Control', value: 'max-age=100' }],
'content-type': [{ key: 'Content-Type', value: 'text/html' }],
'content-encoding': [{ key: 'Content-Encoding', value: 'UTF-8' }],
},
body: htmlContent,
};
callback(null, response);
};
version 1
và chọn Publish