æè¡é¨ã®ä¹ é (@hkusu_) ã§ããã¯ãã¯ãããã§ã¯ã¢ãã¤ã«åºç¤ã°ã«ã¼ãã«ã¦ Android çã¯ãã¯ãããã¢ããªã®éçºã»ã¡ã³ããã³ã¹ã«æºãã£ã¦ãã¾ãã
æ®æ®µã®æ¥åã¨ã¯å°ãç°ãªãã®ã§ãããç»åãªã¯ã¨ã¹ãã«å¿ããªã¢ã«ã¿ã¤ã ã«ç»åãå¤æãã¦ã¬ã¹ãã³ã¹ããã¨ããä»çµã¿ã AWS ã® Lambda@Edge ãç¨ãã¦å®ç¾ãã¦ã¿ãã®ã§ãæ§ç¯ããç°å¢ã®å 容ãã³ã¼ããç´¹ä»ãããã¨æãã¾ããç»åå¤æã®å 容ã¯ã³ã¼ãã®å®è£ 次第ã§å¤§æ¦ã®ãã¨ã¯å®ç¾ã§ãããã§ãããä»åã®ã³ã¼ãã®å 容ã¯ã¹ããåã WEB ãµã¤ããã¢ãã¤ã«ã¢ããªåãã®ç»åé ä¿¡ãæ³å®ããé信容éã®åæ¸ããã³è¡¨ç¤ºé度ã®åä¸ãç®çã¨ããç»åã®ãªãµã¤ãºï¼ä¸»ã«ç¸®å°ï¼ã¨ WebP å½¢å¼ã¸ã®å¤æã§ãã
注æï¼å®éç¨ãã¦ãã段éã§ã¯ãªãã®ã§åèã«ãããå ´åã¯ã注æãã ãããã¡ãªã¿ã«ã¯ãã¯ãããã«ã¯æ¬çªç°å¢ã¨ã¯åãé¢ããã調æ»ã»æ¤è¨¼ç¨ã® AWS ç°å¢ããããä»åã®æ稿ã®ããã«ã¨ã³ã¸ãã¢ã¯èªç±ã« AWS ã®å種ã³ã³ãã¼ãã³ãã試ããã¨ãã§ãã¾ãã
æ¦è¦
Lambda@Edge ã¨ã¯ãå ¬å¼ããã¥ã¡ã³ã ã«ã説æãããã¾ãã CDN ã§ãã CloudFront ã®å ¥åºå HTTP ãªã¯ã¨ã¹ãã»ã¬ã¹ãã³ã¹ãæä½ã§ãã Lambda é¢æ°ã§ããä»å㯠CloudFront ã®ãªãªã¸ã³ã¨ã㦠S3 ãæå®ããS3 ããã®ç»åã¬ã¹ãã³ã¹ã Lambda@Edge ã§å¤æããä»çµã¿ãæ§ç¯ãã¾ããã
ãã®ä»çµã¿ã§ã¯ç»åã¸ã®ãªã¯ã¨ã¹ãã«å¿ãã¦ãã®å ´ã§ç»åå¤æãè¡ãã®ã§ããµã¼ãã¹ã®éå¶ã«ããã¦æ§ã ãªããªã¨ã¼ã·ã§ã³ã®ç»åãå¿ è¦ãªå ´åã§ãã£ã¦ãããããäºãç¨æãã¦ããå¿ è¦ããªããç»åãå¤æããçºã®ãµã¼ããå¿ è¦ã¨ãã¾ãããS3 ã«ç»åãã¡ã¤ã«ããç½®ãã°ããã®ã§ãµã¼ããµã¤ãã®ã¢ããªã±ã¼ã·ã§ã³ã®ç¨®é¡ãè¨èªãåããããã¨ãéç㪠WEB ãµã¤ãã§ãã£ãã¨ãã¦ãæ§ã ãªããªã¨ã¼ã·ã§ã³ã®ç»åãæä¾ãããã¨ãã§ãã¾ãã
å¤æå¾ã®ç»å㯠CloudFront ã«ãã£ãã·ã¥ãããã®ã§ãå¤æå¦çãè¡ãããã®ã¯ CloudFront ã«ãã£ãã·ã¥ããªãå ´åã®ã¿ã§ããAmazon Web Services ããã° ã§ã¯å¤æå¾ã®ç»åã S3 ã«ä¿åããæ¹æ³ãç´¹ä»ããã¦ãã¾ãããä»åã®æ¹æ³ã§ã¯å¤æå¾ã®ç»å㯠CloudFront ã®ã¿ã«æã¤æ§æã¨ãã¦ãã¾ãããããã CloudFront ã®ãã£ãã·ã¥æéãé·ãè¨å®ãã¦ããã°ããã¨ãã話ãããã¾ããããã¨ã CloudFront ã®ãã£ãã·ã¥ããããå ´åã§ãç»åå¤æãå度å®è¡ããã®ã§ã¯ãªã CloudFront ã®ãã£ãã·ã¥ã®ä¿ææéã延é·ãããã¨ã§å¤æã³ã¹ããæãããã¨ãã§ãã¾ãï¼ãã®æ¹æ³ã«ã¤ãã¦ã¯å¾è¿°ãã¾ãï¼ãã¾ãå¤æå¾ã®ç»åããã£ãã·ã¥ã§ããä¿æãã¦ããªãã®ã§ãå¾ããç»åå¤æã®ä»æ§ãå¤ãã£ããä¸å ·åããã£ããããã¨ãã¦ã S3 ä¸ã®ç»åãæ¶ãçã®ãªãã¬ã¼ã·ã§ã³ãå¿ è¦ã¨ãããCloudFront ä¸ã®ãã£ãã·ã¥ãæ¶ãï¼CloudFront ã« invalidation ãªã¯ã¨ã¹ããéãï¼ã ãã§å¯¾å¿ã§ãã¾ãã
Lambda@Edge ãå©ç¨ããä¸ã§ã®æ³¨æç¹
ã¾ã Lambda@Edge ç¨ã®é¢æ°ã®éçºã§å©ç¨ã§ããè¨èªã¯ Node.js ãã¤ãã¼ã¸ã§ã³ã¯ 6.10 ã®ã¿ã§ããæ§æ¥ã® Lambda é¢æ°ã®éçºã§å©ç¨ã§ãã 8ç³» ã¯ç¾æç¹ã§å¯¾å¿ãã¦ãã¾ãããã¾ã Lambda@Edge ç¨ã®é¢æ°ãä½æã§ããã®ã¯ç±³å½æ±é¨ï¼ãã¼ã¸ãã¢åé¨ï¼ãªã¼ã¸ã§ã³ã®ã¿ã§ãï¼ãã ãä½æããé¢æ°ã¯å CloudFront ã®ã¨ãã¸ã¸ã¬ããªã±ã¼ãããã¾ãï¼ã
ãã®ã»ãå¶éã¯å ¬å¼ããã¥ã¡ã³ãã® Lambda@Edgeã®å¶é ã®ã¨ããã§ãã注æãã¹ã㯠Lambda@Edge ã§ãªãªã¸ã³ã¬ã¹ãã³ã¹ï¼ä»åã®æ§æã§ã¯ S3 ããã®ç»åã¬ã¹ãã³ã¹ï¼ãæä½ããå ´åãæä½å¾ã®ã¬ã¹ãã³ã¹ã®ãµã¤ãºã¯ãããã¼çãå«ã㦠1MB ã«æããå¿ è¦ããããã¨ã§ãããé常ã®WEBãµã¤ããã¢ãã¤ã«ã¢ããªã§ã®ç¨éã¨ãã¦ã¯ååãªæ°ããã¾ããã¿ã¤ã ã¢ã¦ãã¾ã§ã®å¶éæé㯠30 ç§ã¨é·ããã¡ã¢ãªãæ大 3GB ã»ã©ä½¿ãããã¨ããç»åãæ±ãç°å¢ã¨ãã¦ã¯åé¡ãªãããã§ãã
ã¾ãããã㯠Lambda@Edge ç¨ã®é¢æ°ã®å®è£ æã®å¶ç´ãªã®ã§ãããé¢æ°ãããªãªã¸ã³ã¬ã¹ãã³ã¹ãåãæ±ãéãé¢æ°ããã¯ã¬ã¹ãã³ã¹Bodyï¼ç»åãã¼ã¿ï¼ã«ã¢ã¯ã»ã¹ã§ãã¾ããããã£ã¦ãæ¹ãã¦é¢æ°ãã S3 ã¸ã¢ã¯ã»ã¹ãç»åãã¡ã¤ã«ãåå¾ããå¿ è¦ãããã¾ãã
ç°å¢ã®æ§ç¯ (CloudFrontãS3)
ç¹çãã¹ããã¨ã¯ãªã S3 ã®ãã±ãããä½æããããããªãªã¸ã³ã¨ã㦠CloudFront ãè¨å®ããã°åé¡ããã¾ãããï¼ãã©ã¦ã¶åãã« HTTP/2 ããµãã¼ãããããã«ãã¦ããã¾ãããï¼ãã¯ã¨ãªæååã¯ãã©ã¯ã¼ãï¼ãã£ãã·ã¥ã®ãã¼ã«å«ããï¼ã¯ã¨ãªæååãç°ãªãã°å¥ãã¡ã¤ã«ã¨ãã¦ãã£ãã·ã¥ããï¼ããã«ãã¦ãã ãããããã¯å¾è¿°ãã¾ããã¯ã¨ãªæååã§ç»åã®å¤æãªãã·ã§ã³ãæå®ããçºã§ããã¾ã CloudFront ã®ãã£ãã·ã¥æéãé©å®ãè¨å®ãã¦ããã¾ãï¼åä½ç¢ºèªä¸ã¯1åãªã©çããã¦ããã¨ããã§ãï¼ã
ç°å¢ã®æ§ç¯ (Lambda@Edge)
Lambda@Edge ã®ç°å¢æ§ç¯ã«ã¤ãã¦ã¯ å ¬å¼ããã¥ã¡ã³ã ã«è©³ããã§ããã§ã¯ãã¤ã³ãã®ã¿è¿°ã¹ã¾ãã
ãã¼ã«ã®ä½æ
AWS ã® IAM ã«ã¦äºã Lambda@Edge ç¨ã®é¢æ°ã®å®è¡ãã¼ã«ãä½æãã¦ããã¾ãã
ã¢ã¿ããããããªã·ã¼
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": [ "arn:aws:s3:::*" ] } ] }
- S3 ã®ç»åãåç §ãã権éã追å
ä¿¡é ¼é¢ä¿
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com", "edgelambda.amazonaws.com" ] }, "Action": "sts:AssumeRole" } ] }
edgelambda.amazonaws.com
ã追å
é¢æ°ã®ä½æ
Lambda@Edge ç¨ã®é¢æ°ã®å®è£ ãç¨æããåã«ã管çã³ã³ã½ã¼ã«ä¸ã§é¢æ°ã®æ çµã¿ã ãä½æãã¦ããã¾ããç±³å½æ±é¨ï¼ãã¼ã¸ãã¢åé¨ï¼ãªã¼ã¸ã§ã³ã® Lambda ã®ã¡ãã¥ã¼ããé¢æ°ãä½æãã¾ãã
[é¢æ°ã®ä½æ] ãã¿ã³ãæ¼ä¸ãé¢æ°ãä½æãã¾ããé¸æããå®è¡ãã¼ã«ã®æ å ±ãå ã«ãæ¬é¢æ°ããã¢ã¯ã»ã¹ãå¯è½ãª AWS ã®ãªã½ã¼ã¹ã次ã®ããã«è¡¨ç¤ºããã¾ãã
åé¡ãªããã°ä¸æ¦ãæ çµã¿ã®ä½æã¯å®äºã§ãã
é¢æ°ã®å®è£ ã®ç¨æ
å®è£
ä¾ã¨ãã¦ããµã³ãã«ã³ã¼ããç§ã®æ¹ã§ä½æãã¾ãããGitHub ã«ç½®ãã¦ããã®ã§åèã«ãã¦ãã ããã
â hkusu/lambda-edge-image-convert
ãµã³ãã«ã³ã¼ãã®èª¬æ
ç»åã®ãªãµã¤ãºã¨ WebP å½¢å¼ã¸ã®å¤æã®æ©è½ãæä¾ãã¾ããä»æ§ã¯æ¬¡ã®ã¨ããã§ãã
å¤æå ã®ç»å㯠JPEG å½¢å¼ã®ç»åã®ã¿
ãªãµã¤ãºã®éã«ç»åã®ã¢ã¹ãã¯ãæ¯ï¼æ¨ªå¹ ã¨ç¸¦å¹ ã®æ¯çï¼ã¯å¤æ´ããªã
å¤æãªãã·ã§ã³ã¯ã¯ã¨ãªæååã§æå®ãã
ãã¼ å¤ ããã©ã«ã æå¤§å¤ è£è¶³ wãã æå¤§æ¨ªå¹ (ãã¯ã»ã«)ãæå® 1200ããã 1200ãã å¤æå ã®ç»åãã大ããªå¤ã¯ç¡å¹
(=æ¡å¤§ããªã)h æå¤§ç¸¦å¹ (ãã¯ã»ã«)ãæå® åä¸ åä¸ åä¸ p t (true)ï¼WebP å½¢å¼ã¸å¤æãã
f (false)ï¼WebP å½¢å¼ã¸å¤æããªãf (false) - - - ä¾
https://xxx.com/sample.jpg?w=500&p=t
w
h
ã§ãæ大ãã¨ãã¦ããã®ã¯æçµçã«é©ç¨ãããå¤ã¯ã¢ã¹ãã¯ãæ¯ãç¶æããªãã決å®ãããçº
- ä¾
å¤æå¾ã®ç»åå質(quality)ã¯ä¸å¾ã§å¤æå ç»åã® 80% ã¨ãã
å¤æå¾ã®ç»åã®ã¡ã¿ãã¼ã¿ã¯å ¨ã¦åé¤ããï¼æå³ããä½ç½®æ å ±çãé²åºããã®ãé²ãçºï¼
ãã®ä»æ§ã ã¨ã¡ã¤ã³ãã¸ãã¯ã¯ index.js 1ãã¡ã¤ã«ã«åã¾ãã¾ãããã¡ã¤ã³ãã¸ãã¯ãå°ãè£è¶³ããã¾ãã
l.7ãl.12
let sharp; if (process.env.NODE_ENV === 'local') { sharp = require('sharp'); } else { sharp = require('../lib/sharp'); }
ç»åå¤æã«ã¯ sharp ã¨ããã©ã¤ãã©ãªãå©ç¨ãã¦ãã¾ãããã®ã©ã¤ãã©ãªã®ã©ã³ã¿ã¤ã ã¯å®è¡ç°å¢ã«ããç°ãªãçºããã¼ã«ã«ã§ã¯éçºç°å¢ã®æ§ç¯æã«ã¤ã³ã¹ãã¼ã«ããã node_modules/sharp
ãå©ç¨ããAWS ä¸ã§ Lambda é¢æ°ã¨ãã¦å®è¡ããé㯠lib/sharp
ãã£ã¬ã¯ããªã®ãã®ãå©ç¨ããããã«ãã¦ãã¾ãã
â» ãµã³ãã«ã³ã¼ãã®ãªãã¸ããªã«ã¯ lib/sharp
ãã£ã¬ã¯ããªã¯å«ã¾ãã¦ãã¾ãããAWS ä¸ã§ Lambda é¢æ°ã¨ãã¦åããã«ã¯ãEC2 çãæ§ç¯ã㦠Amazon Linux ä¸ã§ $ npm install sharp
ãå®è¡ããçæããã node_modules/sharp
ãã£ã¬ã¯ããªãä¸èº«ã㨠lib
ãã£ã¬ã¯ããªé
ä¸ã¸é
ç½®ãã¦ãã ããã
l.18ãl.19
exports.handler = (event, context, callback) => { const { request, response } = event.Records[0].cf;
é¢æ°ã¸ã®å
¥åã¨ãã¦æ¸¡ããã event
ãªãã¸ã§ã¯ããã request
ãªãã¸ã§ã¯ããresponse
ãªãã¸ã§ã¯ããåãåºãã¦ãã¾ãã
l.36ãl.39
if (response.status === '304') { responseOriginal(); return; }
CloudFront ä¸ã®ãã£ãã·ã¥ããããå ´åãS3 ã«å¯¾ã㦠ETag ä»ãã®æ¡ä»¶ã¤ããªã¯ã¨ã¹ããéã£ã¦ãã¾ããS3 ãã㯠304 ã³ã¼ããè¿ã£ã¦ããã®ã§ããã®å ´åã¯ä½ãããã¬ã¹ãã³ã¹ãã¹ã«ã¼ãã¦çµäºãã¾ããCloudFront ã 304 ã¬ã¹ãã³ã¹ãåãåã£ãå ´åããã£ãã·ã¥ã®ç ´æ£ã§ã¯ãªããã£ãã·ã¥ã®ä¿ææéã®å»¶é·ãè¡ããã¾ãã
l.77ãl.82
s3.getObject( { Bucket: BUCKET, Key: options.filePath.substr(1), // å é ã®'/'ãåé¤ }) .promise()
éåæã®å¦çãè¡ãã«ãããã³ã¼ã«ããã¯ã®ãã¹ããæ·±ããªã£ã¦ãã¾ãã®ã§ãå¯èªæ§ã®åä¸ãç®çã« Promise ã¤ã³ã¿ãã§ã¼ã¹ãå©ç¨ãã¦ãã¾ãã
l.85
return sharpBody.metadata();
å¤æåã®ç»åã®ã¡ã¿ãã¼ã¿ãåå¾ãã¦ãã¾ãããã ãåå¾ã¯éåæã§ãã
l.95
sharpBody.resize(options.width, options.height).max();
ç»åã®ãªãµã¤ãºãè¡ã£ã¦ãã¾ãããã㧠.max()
ãæå®ãããã¨ã«ããç»åã®ã¢ã¹ãã¯ãæ¯ãç¶æããã¾ãã
l.99ãl.101
return sharpBody
.rotate()
.toBuffer();
sharp ã§ã¯ .withMetadata()
ãæå®ããªãéããå¤æå¾ã®ç»åã®ã¡ã¿ãã¼ã¿ã¯å
¨ã¦åé¤ããã¾ãããã®éãç»åã® orientation(åã) ã®æ
å ±ãåé¤ããã¦ãã¾ãçºãå¤æå¾ã®ç»åããã©ã¦ã¶çã§è¡¨ç¤ºããã¨ç»åã®åããåæ ããã¦ãã¾ããã .rotate()
ãæå®ããã¨ãç»åã®åããåãããç»åãã¼ã¿ãã®ãã®ãå転ããã¾ãã
ã¾ããä»å .quality()
ã§ã®ç»åå質ã®æå®ã¯è¡ã£ã¦ããªãã®ã§ãããã©ã«ãã® 80 ãé©ç¨ããã¾ãã
l.104ãl.112
response.status = '200'; if (options.webp) { response.headers['content-type'] = [{ key: 'Content-Type', value: 'image/webp' }]; } else { response.headers['content-type'] = [{ key: 'Content-Type', value: 'image/jpeg' }]; } response.body = buffer.toString('base64'); response.bodyEncoding = 'base64'; callback(null, response);
ç»åãã¬ã¹ãã³ã¹ããã³ã¼ãã§ãã Content-Length
ãããã¯ããã§è¨å®ããªãã¦ã AWS å´ã§èªåã§ä»ä¸ããã¾ãã
ä»åãS3 ããã®ã¬ã¹ãã³ã¹(response
å¤æ°)ããã®ã¾ã¾å©ç¨ãå¿
è¦ãªç®æã ãä¸æ¸ããã¦ããçºãETag
Last-Modified
ãããã¯ããã§åè¨å®ããªãéã S3 ããè¿ããããã®ããã®ã¾ã¾ CloudFront ã«æ¸¡ãã¾ããå¤æãªãã·ã§ã³æ¯ã«å¤æå¾ã®ç»åãã¼ã¿ã¯ç°ãªãçºãETag
Last-Modified
ããããå¤æãªãã·ã§ã³æ¯ã«å¤æ´ããæ¹ãè¯ãã¨èããããããã¾ããããã CloudFront ã§ã¯ã¨ãªæåå込㮠URL ãã¼ã¹ã§ãã£ãã·ã¥ããããã«ãã¦ããå ´åã¯ãETag
Last-Modified
ãããã¯å
±éã§åé¡ããã¾ãããå¤æãªãã·ã§ã³ãç°ãªãã° URL ãç°ãªãã®ã§ãå¥ãã¡ã¤ã«ã¨ãã¦ã¿ãªãããããã§ãã
ãããã¬ã¹ãã³ã¹æã«ã¯ã©ã¤ã¢ã³ãå´ã CloudFront ã®ãã£ãã·ã¥ãå¶å¾¡ããå ´å㯠response.headers['cache-control'] = [{ key: 'Cache-Control', value: 'max-age=604800, s-maxage=31536000' }]
çã¨ãã¾ãããã s-maxage
㯠CloudFront å´ã®è¨å®ã¨ã®å
¼ãåããããçºãããã§ã¯è¨å®ãã CloudFront å´ã®ãã£ãã·ã¥æéã®è¨å®ã«å§ããæ¹ãå®å
¨ããããã¾ããã
l.145
class FormatError extends Error {}
Promise ãã§ã¼ã³ä¸ã§çºçããã¨ã©ã¼ãåºå¥ããçºã®ã«ã¹ã¿ã ã¨ã©ã¼ã§ãã
ãã¼ã«ã«éçºç°å¢ã«ã¤ãã¦
ãµã³ãã«ã³ã¼ãã®ãªãã¸ããªãè¦ã¦ãããã°åããã¨æãã¾ãããç¹ã«ãã¬ã¼ã ã¯ã¼ã¯çã¯ä½¿ã£ã¦ãã¾ããããã ãæ©è½ã®éçºä¸ã«ãã¼ã«ã«ã§ãå®è¡ã§ããããã«ã¯ãã¦ããã¾ãããªãªã¸ãã«ã¬ã¹ãã³ã¹ã¯ããã¼ã® JSON ã§ä»£æ¿ãã¦ãã¾ãããé¢æ°ãã S3 ã«ã¯å®éã«ã¢ã¯ã»ã¹ãã¦ç»åãåå¾ãã¾ããéçºä¸ã¯ããã¼ã® JSON ã®ä¸èº«ãé©å®å¤æ´ããç»åãã¡ã¤ã«ã¯ãã¹ãç¨ã®ç»åã S3 ã«ç½®ãã¦ãã ããã
ãã¼ã«ã«ã® Node.js ã®ãã¼ã¸ã§ã³ã¯ AWS ä¸ã® Lambda ã®å®è¡ç°å¢ã¨åãã㦠6.10 ã¨ãã¦ãã ãããã¾ããã¼ã«ã«ãã S3 ã¸ã¢ã¯ã»ã¹ããçºã«ãããã¸ã§ã¯ããã£ã¬ã¯ããªã®ä¸ã¤ä¸ã®é層㫠AWS SDK ãã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã
$ npm install aws-sdk
- AWS SDK ããã³ã¢ã¯ã»ã¹ãã¼ã«ã¤ãã¦è©³ãã㯠https://aws.amazon.com/jp/sdk-for-node-js/ ãåç §ãã ããã
ãã¼ã«ã«ã§é¢æ°ãå®è¡ããã«ã¯ã³ã³ã½ã¼ã«ã§æ¬¡ã®ããã«ãã¾ãã
$ npm run local-run
å¤æå¾ã®ç»åã«ã¤ãã¦ã¯ base64 ã¨ã³ã³ã¼ããããæååãã³ã³ã½ã¼ã«ã¸è¡¨ç¤ºããã¾ãããã®ç°å¢ãæ¡å¼µãã¦ç»åãä¿åã»è¡¨ç¤ºããããã«ããã¨ããè¯ãããããã¾ããã
AWS ã®ç®¡çã³ã³ã½ã¼ã«ã¸ã¢ãããã¼ãããçºã®ã¢ã¼ã«ã¤ã(***.zip
ãã¡ã¤ã«)ãä½æããå ´åã¯ãã³ã³ã½ã¼ã«ã§æ¬¡ã®ããã«ãã¾ãã
$ npm run create-package
ãã¼ã«ã«éçºç°å¢ã«ã¤ãã¦ã¯ä¸è¨ã®ã¹ã©ã¤ãã«ãæ¸ããã®ã§ããããããã°åç §ãã ãããããã¯ä»¥åã«ç§ã æ±äº¬Nodeå¦å ã§ãã¼ã¯ããéã®è³æã§ããLambda@Edge ç¨ã§ãªãé常㮠Lambda é¢æ°ã®éçºã«ã¤ãã¦èª¬æããè³æã§ããã14ãã¼ã¸ä»¥éã®ãã¼ã«ã«ç°å¢ã«ã¤ãã¦ã®è¨è¼ã¯ Lambda@Edge ã§ãå ±éã§ãã
é¢æ°ã®ã¢ãããã¼ãã¨åä½ç¢ºèª
ä½æããé¢æ°ãå®éã« AWS ä¸ã§åããã«ã¯ãå
ã®æé ã§ä½æããé¢æ°ã®æ çµã¿ãéããã¢ã¼ã«ã¤ããã¢ãããã¼ããã¾ããä»åã®ãµã³ãã«ã³ã¼ãã§ã¯ã¡ã¤ã³ãã¸ã㯠index.js
㯠src
ãã£ãã¯ããªé
ä¸ã«ç½®ãã¦ããã®ã§ãããã³ãã©ãã«ã¯ src/index.handler
ãæå®ãã¾ãã
ã¾ããã¡ã¢ãªããã¿ã¤ã ã¢ã¦ãããé©å®ãå¤æ´ãã¦ããã¾ããå ç»åã®å¤§ããã«ããã¾ãããçµé¨çã«ã¯ã¡ã¢ãªã¯ 1024 MBãã¿ã¤ã ã¢ã¦ãã¯æ°ç§ããã°ååããã§ãããããã§ã¯ä½è£ããã£ã¦ãããã 2048 MBã15 ç§ãæå®ãããã¨ã«ãã¾ãããã®ãããã¯å ç»åã¨ç»åå¤æã®å 容ã«ããã®ã§é©å®ã調æ´ãã¦ãã ããã
è¨å®ãä¿åããããæ°ããããã¼ã¸ã§ã³ããçºè¡ãã¾ãã
Lambda é¢æ°ã¯ã³ã¼ãã¨è¨å®ãã²ã¨ã¾ã¨ãã«ãã¦å±¥æ´ç®¡çã§ãã¾ãããã®æä½ã¯1ã¤ã®å±¥æ´ã®ãã¼ã¸ã§ã³ã¨ãã¦ä¿åããã¨ããæå³ã§ãã
ãã¼ã¸ã§ã³ãä½æããããããã®ãã¼ã¸ã§ã³ã®ã³ã¼ãããã³è¨å®ã CloudFront ã¨é¢é£ã¥ããã¾ããããªã¬ã¼ã¨ã㦠CloudFront ãé¸ã³æ¬¡ã®ããã«è¨å®ãè¡ãã¾ãã
è¨å®ãä¿åããCloudFront ã¸åæ ãããã®ãå°ãå¾ ã£ãå¾ããã©ã¦ã¶ã§ CloudFront ã®ãã¹ã + ç»åã® URL ã¸å¤æãªãã·ã§ã³ã®ã¯ã¨ãªæåãã¤ãã¦ã¢ã¯ã»ã¹ãã¦ã¿ã¾ããæå®ãããµã¤ãºã®ç»åã表示ãããã° OK ã§ãã
HTTP/2 ãæå¹ã«ããçºã«ãç»å㯠HTTPS ãããã³ã«ã§é ä¿¡ãã¾ãããã
ãããã«
Lambda@Edge ã§ç»åããªã¢ã«ã¿ã ã«å¤æããä»çµã¿ã«ã¤ãã¦ç´¹ä»ãã¾ãããä»åã®ãµã³ãã«ã³ã¼ãã¯ç»åã®ãªãµã¤ãºã¨ WebP å½¢å¼ã¸ã®å¤æã¨ããã·ã³ãã«ãªãã®ã§ããããæ´ã«ç»åã®ãã£ã«ã¿ã¼å å·¥ï¼ã¼ããçï¼ãç»åã®ã¯ãããï¼åãæãï¼ãã¾ãç»åã®åæçãå®è£ ãã¦ã¿ãã¨é¢ç½ãããããã¾ããã
åé ã®ã¨ããã¾ã å®éç¨ã§ã¯è©¦ãã¦ãªãã®ã§ãä»å¾ããå®éã«éç¨ããæ©ä¼ããã£ããããã§çºçããåé¡ã解決æ¹æ³ãç¥è¦ãã¾ãç´¹ä»ãããã¨æãã¾ããã¾ãç»åå¤æã«é¢ããã Lambda@Edge ãå®éã«éç¨ãã¦ã¿ãããªã©ã®äºä¾ãããã¾ãããæ¯éããã°çã§ç´¹ä»ããã ããã°å¹¸ãã§ãã
åèã«ãããµã¤ã
- AWS Lambda@Edge [å ¬å¼ããã¥ã¡ã³ã]
- Amazon CloudFront & Lambda@Edge ã§ç»åããªãµã¤ãºãã [Amazon Web Services ããã°]
- Amazon CloudFrontã¨AWS Lambda@Edgeã§SPAã®Basicèªè¨¼ããã£ã¦ã¿ã [Developers.IO]