AWS Lambda@Edge (CloudFront) JavaScript
/pixel.gif へのリクエストに空の GIF を返す、AWS Lambda@Edge (CloudFront) 用の JavaScript スニペット。GIF は起動時に一度だけデコードします。
AWS Lambda@Edge (CloudFront) JavaScript
// Viewer-request trigger: answer /pixel.gif at the edge, pass everything else through.
const GIF_BASE64 = 'R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
exports.handler = async (event) => {
const request = event.Records[0].cf.request;
if (request.uri !== '/pixel.gif') return request;
return {
status: '200',
statusDescription: 'OK',
headers: {
'content-type': [{ key: 'Content-Type', value: 'image/gif' }],
'cache-control': [{ key: 'Cache-Control', value: 'no-store' }],
},
body: GIF_BASE64,
bodyEncoding: 'base64',
};
};
このスニペットは実行し、返されたバイト列を照合しました。
確認
curl -sI http://localhost:8080/pixel.gif
公式サイト: AWS Lambda@Edge (CloudFront)