ã¯ããã«
ããã«ã¡ã¯ãã¹ã¿ãã£ãµããªã®ãããã¯ãåºç¤...ãã®ä¸ã§ãç¹ã«åç»åºç¤ã®éçºãè¡ã£ã¦ãã @highwide ã§ãã
ãã㯠ã¹ã¿ãã£ãµããªProduct Team Advent Calendar 2024 16æ¥ç®ã®è¨äºã§ãã
ä»æ¥ã¯åç»åºç¤ãã¼ã ã§å©ç¨ãã¦ããFastlyã®æ§æããæã ãããã£ã¦ãã¾ã£ãè½ã¨ãç©´ã«ã¤ãã¦æ¸ãããã¨æãã¾ãã
åç»åºç¤ã«ãããFastlyã®ä½¿ãæ¹
ã¹ã¿ãã£ãµããªã§ã¯ãåç»ã®é ä¿¡ã¯Fastlyãéãã¦HLS(HTTP Live Streaming)*1ã«ãã£ã¦è¡ã£ã¦ãã¾ãã HLSã§ã¯ãåç»åçã®ããã«å¿ è¦ãªãã¡ã¤ã«ãåæãããããã§ã¹ããã¡ã¤ã«(m3u8)ã¨ãåç»ã³ã³ãã³ããã®ãã®ã§ããã»ã°ã¡ã³ããã¡ã¤ã«ã®çµã¿åããã«ãã£ã¦åç»ã®è¦è´ãå®ç¾ãã¾ãã
ã¹ã¿ãã£ãµããªã§ã¯ãå¦ç¿ã³ã³ãã³ãã«ç´ã¥ãåç»ã®ã¡ã¿ãã¼ã¿ã¯ã¢ããªã±ã¼ã·ã§ã³ã®DBã«ä¿åãã¦ããã以ä¸ã®ãããªæµãã§åç»è¦è´ãè¡ããã¦ãã¾ãã
- å¦ç¿ã³ã³ãã³ããåå¾ããHTTPãªã¯ã¨ã¹ãã«å¯¾ãã¦ãåç»ã®ã¡ã¿ãã¼ã¿ãè¿å´ãã
- åç»ã®ã¡ã¿ãã¼ã¿ã¨ãã¦åå¾ããm3u8ãã¡ã¤ã«ã®URLã«å¯¾ãã¦åç»ãã¬ã¤ã¤ã¼ããªã¯ã¨ã¹ããè¡ã
- m3u8ãã¡ã¤ã«ã«è¨è¼ãããã»ã°ã¡ã³ããã¡ã¤ã«(ã¹ã¿ãã£ãµããªã§ã¯m2ts)ã«å¯¾ãã¦åç»ãã¬ã¤ã¤ã¼ãé 次ãªã¯ã¨ã¹ããè¡ã£ã¦ãã
ãã®æµãã«ããã¦ãm3u8ãã¡ã¤ã«ãm2tsãã¡ã¤ã«ã«å¯¾ãããªã¯ã¨ã¹ããFastlyã®CDNã«å¯¾ãã¦è¡ããã¾ãã
ä½è«ã§ãããèªåããã¼ã ã«ã¸ã§ã¤ã³ããã°ããã®é ãm2tsãã¡ã¤ã«ã® .ts
ã¨ããæ¡å¼µåãè¦ã¦ãTypeScriptã§åç»é
ä¿¡ããã¦ããã®ã ãããã¨æã£ã¦ãã¾ã£ãæ¥ããããã¨ãã½ã¼ããããã¾ãã
æã ã®Fastlyã®å©ç¨ã®ä»æ¹ã¨ãã¦ã¯ãCDNã®ä»ãä¸é¨ãªã¯ã¨ã¹ããç¹å®ç¨éã®ããã«Edge Computingåºç¤ã§ããFastly Compute*2ã«æµãã¦ãã¾ããCDNããè¦ãã¨"Backend"ã¨ãã¦ãåç»ãã¡ã¤ã«ã®ç½®ãå ´æã§ããAmazon S3ãã¨ãä¸é¨ãªã¯ã¨ã¹ãã«ã¤ãã¦ã®å¦çãè¡ãComputeããæã¡ãCDNã®è¨å®ãã¡ã¤ã«ã§ããVCLã«æ¸ãããæ¡ä»¶ã«å¿ãã¦ãªã¯ã¨ã¹ããæ¯ãåãã¦ãããã¨ããæ§æã§ãã
ã
Fastlyãå©ç¨ãã¦ãã¦ã®è½ã¨ãç©´
ããã§æã ãé¥ã£ãè½ã¨ãç©´ã«ã¤ãã¦ç´¹ä»ãããã¨æãã¾ãã
å ã»ã©ãCDNãè¤æ°ã®Backendã«ãªã¯ã¨ã¹ããæ¯ãåãã¦ãã話ããã¾ããããã¨ãã°ãVCLä¸ã§ãç¹å®ã®pathã«ãããããå ´åã¯Computeã«ãªã¯ã¨ã¹ããæµããã¨ãã£ããã¨ãã§ãã¾ãã
sub vcl_recv { #FASTLY recv if (req.url.path !~ ".+\.m3u8.*") { set req.backend = F_backend_compute_service; } }
ã§ã¯ããã®ãããªãæ示çã« req.backend
ãæå®ããå¦çããéããªãã£ããªã¯ã¨ã¹ãã¯ã©ããªãã§ããããï¼
çãã¯ããããã©ã«ãBackendãã«ãªã¯ã¨ã¹ããæµãããã¨ã¨ãªãã¾ããã§ã¯ãããã©ã«ãBackendã¯ã©ã®ããã«æ±ºã¾ãã§ããããï¼
å®ã¯"å
åã¡"...ã¤ã¾ããæãå¤ãã¿ã¤ã ã¹ã¿ã³ãã®Backendãæé»çã«ããã©ã«ãã¨ãã¦æ±ããã¾ãã
Fastlyã§ã¯ãGUIã§å®ç¾©ã§ããè¨å®æ
å ±ã¨èªåãã¡ã§å®ç¾©ããCustom VCLããã¼ã¸ãã¦ãæçµçã«å©ç¨ãããVCLãçæããã¾ããããShow VCLãã¡ãã¥ã¼ãããã®çæãããVCLãè¦ãã¨ãvcl_recv
ãµãã«ã¼ãã³ã«ããã¦ããã©ã«ãBackendã決å®ããã¦ããæ§åã確èªãããã¨ãã§ãã¾ãã
# default conditions set req.backend = F_s3_ap_northeast_1_amazonaws_com;
ã¨ããã§ãFastlyã®è¨å®ã¯ãã¹ã¿ãã£ãµããªã®ä»ã®ã¤ã³ãã©ãªã½ã¼ã¹åæ§ãTerraformã§ç®¡çãã¦ãã¾ããTerraformã«ãããFastlyã®Backendã®æå®ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
resource "fastly_service_vcl" "our_service" { backend { name = "our_s3" address = "s3-ap-northeast-1.amazonaws.com" # çç¥ } backend { name = "our_compute_service" address = "compute.our.domain" # çç¥ } }
ãã®Terraformãapplyãããéãå®ã¯ãå¿ ãããæ¸ããé ã§Fastlyä¸ã«resourceãä½æããããã¯ä¿è¨¼ãããªããã¨ããã®ãä»åã®éç¨ã«ããã大ããªè½ã¨ãç©´ã§ããã Terraformãå©ç¨ãã¦ãªã½ã¼ã¹ã®æ´æ°ãè¡ã£ãçµæãå ¨ãåãtimestampã§backendãä½ãç´ãã¦ãã¾ããçµæã¨ãã¦é¸æãããããã©ã«ãBackendãå ¥ãæ¿ãã£ã¦ãã¾ã£ãã®ã§ãã
ãã®çµæã
- ç¹å®ã®ãªã¯ã¨ã¹ãã®ã¿Computeã«æµããã
- ãã以å¤ã®å ¨ã¦ã®ãªã¯ã¨ã¹ãã¯S3ãBackendã¨ããã
ãæ¬æ¥æå³ãã¦ããã¯ããã
- ç¹å®ã®ãªã¯ã¨ã¹ããComputeã«æµãã
- ãã以å¤ã®å ¨ã¦ã®ãªã¯ã¨ã¹ããComputeã«æµãã¦ãã¾ã(ããã©ã«ãBackendãS3ã§ãªãComputeã«ãªã£ã¦ãã¾ã£ã¦ãããã)
...ã¨ãããã¨ãèµ·ãã£ã¦ãã¾ãã¾ããã
å®éã«ãBackendãå ¥ãæ¿ãã£ã¦ãã¾ã£ãã®ã¯ãTerraformã®diffã¨ãã¦ã¯å ¨ç¶é¢ä¿ãªãç®æã®å¤æ´ãapplyããã¨ãã ã£ãã®ã§ãåå ã®ç©¶æã«æéããããã¾ããã(Fastlyã®ãµãã¼ãã®æ¹ã«ããªãå©ãã¦ããã ãã¾ãã)
ã¾ããæ«å®å¯¾å¿ã®ããã®ãã¼ã«ããã¯ã«ããã£ã¦ããåé¡ã¨ãªã£ãTerraformãåã«revertããã ãã§ã¯ããã©ã«ãBackendãå ã«æ»ãVCLãçæããããFastlyã®ã³ã³ã½ã¼ã«ä¸ãããã¼ã¸ã§ã³ãæ»ããªãã¨ãæå³ããVCLã«æ»ããªããã¨ãåé¡ã®å¯¾å¦ãããé£ããããã¾ããã
ãã®åé¡ãèµ·ãã£ã¦ä»¥éã¯
- å ¨ã¦ã®æ¡ä»¶ã«ããã¦æ示çã«Backendãæå®ãããããã«VCLãä¿®æ£
terraform apply
ã«ãã£ã¦å³åº§ã«Fastlyä¸ã«å¤æ´ãåæ ãããã®ã§ã¯ãªããFastlyä¸ã§ã¯draftã®ãã¼ã¸ã§ã³ãä½ãããããã«å¤æ´- draftã®ãã¼ã¸ã§ã³ãactivateããã«ã¯æçµçã«çæãããVCLã確èªãã¦ããè¡ãã¨ããéç¨ã®å¤æ´
ã¨ãã対å¿ãè¡ãã¾ããã
Terraformãapplyãããéã«ãå³åº§ã«ãã®ãã¼ã¸ã§ã³ãactivateããã®ã§ã¯ãªãdraftã®ãã¼ã¸ã§ã³ãä½ãããããã«ããã«ã¯ã fastly_service_vcl
resourceã«ããã¦ã activate
ãã£ã¼ã«ããfalseã«ããã¨å®ç¾ãããã¨ãã§ãã¾ãã*3
resource "fastly_service_vcl" "our_service" { activate = false
å®ã¯ããæ示çãªBackendã®æå®ãã¨ããã®ã¯Fastlyãå©ç¨ããããã§ã®ãã¹ããã©ã¯ãã£ã¹ã¨ãã¦ç¥ããã¦ãã¦ãFastly社ã®ç¦ç°ãããæ¸ãããè¨äºã§ããBackendãä¸æ¸ãæã«ã¯ããã©ã«ãè¨å®ãé©ç¨ãããªãããã«æ³¨æãã¨ãããã³ãã·ã£ãªè¨åãããã¾ãã
ã¾ããã¡ã«ã«ãªç¤¾ã®ããã°ã«ããã¦ããããã¯ã¨ã³ãè¨å®ã®æå³ããªãä¸æ¸ãã®çºçãã¨ããè¨åãããã¾ããç¹ã«Request Conditionãå©ç¨ãã¦ãç¹å®ã®Backendã«å¯¾ãããªã¯ã¨ã¹ããæ¥ãªãããã«ãããã¯ããã¯ã¨ããã®ã¯ã¨ã¦ãåèã«ãªãã¾ããã
ãããããèªãã§ããã°...ã¨ããå¾æãããã¾ãããæã ã®ãã®è¨äºãããããã®ããããã注æåèµ·ã«ç¶ããã¨ãã§ããã°å¹¸ãã§ãã
ãããã«
ã¹ã¿ãã£ãµããªProduct Team Advent Calendar 2024 16æ¥ç®ã®è¨äºã¨ãã¦ãã¹ã¿ãã£ãµããªåç»åºç¤ãã¼ã ã§ã¯ããã®ããã«ãã©ã¤ã¢ã³ãã¨ã©ã¼ãéããªãããåç»æè¡ãé ä¿¡æè¡ã«ã¤ãã¦ã®ç¥è¦ãæ·±ããæ¹åãç¶ãã¦ãã¾ããã¨ãããç´¹ä»ã§ããã
ææ¥ã¯ @ryomaejii ããã®ãNew Joiner ãã Working Out Loudãã§ãï¼ãã®ãã¿ããï¼