ããæ°å¹´ã¯ Azure Functions ããã«ã«æ´»ç¨ããã¢ããªã±ã¼ã·ã§ã³ãå®è£ ãããã¨ãå¤ãã£ãã®ã§ãããåæã« Azure Functions ã失æããªãããã«ä½¿ãæ¹æ³ãåãã£ã¦ããã®ã§ããããã§ã¡ããã¨è¨èªåãã¦ããã¾ãã
æè¿ã¯ç¹ã« Azure Light-up ã¨ããããã«ã½ã³ãè¡ããã¨ãå¤ãã®ã§ãããAzure Functions ã使ãå ´åã«ã¯å¿ ããã®è¾ºãã¯æ¯å説æããããã«ãã¦ãã¾ããè¦ããã« Azure Functions ã®å©ç¹ã»ç¹æ§ãç解ãã¦è³¢ã使ãããªããã¨ãã話ã§ãã
- Binding / Trigger ã§å®ç¾åºæ¥ãªããèãã
- Function ã®å®è£ ã¯åºæ¥ãéãå°ããä¿ã¤
- ãªãã©ã¤ã®ããããå®è£ ãéè¦ãã
- ææ°ã® .NET ã§ã®ä½æ³ã«æ²¿ã£ãã³ã¼ããæ¸ã
- Graceful Shutdown ã«å¯¾å¿ããã³ã¼ããæ¸ã
- æ©è½åä½ã§ Function App ããã¸ã§ã¯ããåãã
- æ©ããã¡ãã CI / CD ãã¤ãã©ã¤ã³ãçµã
å³ããã£ãæ¹ãããããããæ°ããã¾ããããä½æããå æ°ããªãã£ãã®ã§é·æã«ãªãã¾ãããããã¾ã§ãç§ãæèãã¦ãããã¨ãªã®ã§ãããããèãæ¹ãããã¨æãã¦è²°ããã°è¯ãã§ãã
Binding / Trigger ã§å®ç¾åºæ¥ãªããèãã
Azure Functions æ大ã®ç¹å¾´ã¨ããããè±å¯ãã¤æ¡å¼µå¯è½ãª Binding / Trigger ã¯è¦ä»¶ã«åãã°å¿ ã使ã£ã¦ãã¾ããAzure Storage ã Event Hub ãªã©ã® SDK ã®ä½¿ãæ¹ãç¥ããã¨ã使ãã便å©ãªæ©è½ã§ãã
HttpTrigger
ã TimerTrigger
ã¯ã»ã¼ä½¿ã£ããã¨ãããã¨æãã¾ãããä»ã§ã¯ãã以å¤ã«ãããªãã®æ°ãæä¾ããã¦ãã¾ããæè¿ã§ã¯ CosmosDBTrigger
ã使ããã¨ãé常ã«å¤ãã§ãã
Blob ã Queue ã®èªã¿æ¸ãã¯ã»ã¼åãã³ã¼ãã«ãªãã®ã§å²ã¨éå±ãªé¨åã®å²ã«ãã³ã¼ãéã¯ããããå¿ è¦ã«ãªãã®ã§ Binding / Trigger ã使ã£ã¦é ãã¦ãã¾ãã¾ãããã®è¾ºãã¯ã¢ããªã±ã¼ã·ã§ã³ã®æ¬è³ªçã§ã¯ãªãã³ã¼ãã«ãªãã®ã§ãå®éã®ãã¸ãã¹ãã¸ãã¯ã«éä¸ããããã«ãéè¦ã§ãã
Function ã®å®è£ ã¯åºæ¥ãéãå°ããä¿ã¤
1 ã¤ã® Function ã®å®è£ ã大ããã¨ãåªçæ§ã®æ ä¿ããªãã©ã¤ãé£ãããªããã¨ãå¤ãã®ã§ãåºæ¬çã« 1 ã¤ã® Function ã«ã¯ 1 ã¤ã®å¦çã ãããããããã«ãã¦ãã¾ãã
åæ©è½ã® Function ã Queue Storage ã Cosmos DB Change Feed ã§çµã¿åããããã¨ã§ãç®çã®ã¢ããªã±ã¼ã·ã§ã³ãå®è£ ããããã«ãã¦ãã¾ããFunction ã®è²¬åãã¯ã£ãããããªãã¨ãããã¤ããé£ãããªãã¾ãã
ä»ã§ã¯ Durable Functions ã使ããã¨ã§ãQueue ãã¤ã³ã¹ã¿ã³ã¹ãæèãããã¨ãªãè¤æ° Function ãæä½ããã¢ããªã±ã¼ã·ã§ã³ãå®è£ ã§ããã®ã§ã以åãã Function ã®å®è£ ãå°ããä¿ã¡ãããã§ãã
ãã ãå®éã®ãã¸ãã¹ãã¸ãã¯ã¯å¥éãµã¼ãã¹ããªãã¸ããªã¯ã©ã¹ã¨ãã¦åé¢ãã¦ããããã DI ã§çµã¿åããã¦ä½ãä¸ãã¦ããã®ã§ãã³ã¼ãéèªä½ã¯ããå°ãå¤ããªãã¾ãã
æè¿ã¯ Azure Functions ã§ã DI ã使ãããããªã£ã¦ããã®ã§åæåãè¥å¤§åæ°å³ã§ãããå Function App ããå ±éã§åç §ãããã¯ã©ã¹ã©ã¤ãã©ãªãä½æããDI ã®ç»é²å¨ãã¨å¦çã®å ±éåãè¡ãããã«ãã¦ãã¾ãã
ãªãã©ã¤ã®ããããå®è£ ãéè¦ãã
Function ãåæ©è½ã¨ãã¦å®è£ ãããã¨ã§ãèãããã¨ãæ¸ãã®ã§ãªãã©ã¤ã¸ã®å¯¾å¿ãç°¡åã«ãªãã¾ããåªçæ§ã®æ ä¿ãåæ§ã«ãããããªãã¾ãã
Azure SDK ã«ã¯æåãããªãã©ã¤ã¯çµã¿è¾¼ã¾ãã¦ããã®ã§ãåºæ¬ã¯çµã¿è¾¼ã¿ããã®ã¾ã¾ä½¿ãã¤ã¤ãå¿ è¦ã«å¿ã㦠Polly ã Azure Function ã® Retry Policy ãå©ç¨ããããã«ãã¦ãã¾ãã
ç¹ã«æè¿ã¯é¿ããããã«ãã¦ããå®è£ ã¨ãã¦ã1 ã¤ã® Function ãè¤æ°ã®ã¹ãã¬ã¼ã¸ã¸ã®æ¸ãè¾¼ã¿ãè¡ããããªã±ã¼ã¹ãããã¾ããä½ããã®å¦ççµæã SQL DB ã«æ¸ãè¾¼ã¿ã¤ã¤ Blob ã¸ãæ¸ãè¾¼ããããªå®è£ ã¯ãå¾è ã®å¦çã失æããå ´åã«ãªã«ããªã¼ãé£ãããªãã¾ããå®è£ ãããããã«å¯¾ãã¦ã®ã¨ã©ã¼ãã³ããªã³ã°ãå¿ è¦ã«ãªããããç°å¸¸ã«è¦éããæªããªããã¨ãããã¾ãã
ãããã£ãå ´åã«ã¯ Blob ã§ããã° Event Grid ã使ã£ã¦ãã¤ãã³ãããªãã³ã§ Blob ã¸ã®æ¸ãè¾¼ã¿ãæ¤ç¥ãã¦å¾ç¶ã®æ¸ãè¾¼ã¿ãè¡ãããã«ãã¾ãã
æè¿ã§ã¯ Cosmos DB ã® Change Feed ã使ã£ã¦ã大æ¬ã® Function ã§ã¯ Cosmos DB ã¸ã®æ¸ãè¾¼ã¿ãè¡ããSQL DB ã Blob ã¸ã®æ¸ãè¾¼ã¿ã¯ããããå¥ã® Function ã¨ãã¦å®è£ ããã±ã¼ã¹ãå¤ãã§ããããããã® Function ã®å¦çãã·ã³ãã«ã«ãªãã®ã§ããªãã©ã¤ãè¡ãããããªãã¾ãã
ææ°ã® .NET ã§ã®ä½æ³ã«æ²¿ã£ãã³ã¼ããæ¸ã
.NET ã®ä½æ³ã¨ãã£ã¦ãææ°ã® C# è¨èªæ©è½ã使ãã¨ããããã§ã¯ãªãããã¼ã¹ã¨ãªã£ã¦ãããã¬ã¼ã ã¯ã¼ã¯ã«æ²¿ã£ãã³ã¼ããæ¸ãããã«æèãã¦ãã¾ãã
Azure Functions v3 㯠.NET Core 3.1 / ASP.NET Core 3.1 ãã¼ã¹ãªã®ã§ããã®ä½æ³ã«å¾ã£ã¦ããã¨ã·ã³ãã«ãªã³ã¼ãã«åºæ¥ã¾ããç¹ã« DI ã¯å¿
é ã«ãªã£ã¦ããã®ã§ãVisual Studio ãä½æããæã«çæããã static
ãªã¯ã©ã¹ã¨ã¡ã½ããã¯å³åº§ã«æ¨ã¦ã¦ãã¾ãã
ãã ãè¤é㪠DI ã®ä½¿ãæ¹ãããã¨éå¹æã«ãªãã®ã§ããããããªã DI ã¨ãã¦ã·ã³ãã«ãªä¾åé¢ä¿ã®è§£æ±ºã¨ãã¤ã³ã¹ã¿ã³ã¹ã®çåæé管çãã¡ã¤ã³ã«ä½¿ã£ã¦ãã¾ãã
æ㯠App Settings ãåãã®ã«ãè¦å´ããã¾ããããææ°ã® Microsoft.Azure.Functions.Extensions
ãã¤ã³ã¹ãã¼ã«ããã¨ã·ã¼ã ã¬ã¹ãªçµ±åãå®ç¾ããã¦ããã®ã§ãç©æ¥µçã«ä½¿ãããã«ãã¦ãã¾ããä¸è¨ã® static
ãªã¯ã©ã¹åé¤ã¨ DI ã®è¨å®ã¯ãã³ãã¬ã¼ãã«å
¥ã£ã¦ãã¦æ¬²ãããããã§ãã
å°ã .NET ããå¤ãã¾ãã Managed Identity ã Key Vault ãå©ç¨ããããã«ãã¦ãã¾ãã
Graceful Shutdown ã«å¯¾å¿ããã³ã¼ããæ¸ã
æã¯ç¨¼åä¸ã® Azure Functions ã¸ã®ãããã¤ãè¡ãåã«ãApplication Insights ã§å¦çãè¡ããã¦ããªããã¨ã確èªãããã¨ãããã¾ããããå°ãåã« Graceful Shutdown ãæ£ããåä½ããããã«ãªã£ãã®ã§å¯¾å¿ããããã«å®è£ ãã¦ããããã¤ã¯ã¿ã¤ãã³ã°ãæèããã«è¡ããããã«ãã¦ãã¾ãã
Azure Functions 㯠.NET ã§æ¨æºã¨ãªã£ã¦ãã CancellationToken
ã使ã£ã¦ã·ã£ãããã¦ã³ãæ¤ç¥ã§ããã®ã§ãå¦çãå£ããªãããã«å¿
è¦ãªå¦çã ããè¡ã£ã¦ Function ãå®äºããããã¨ãåºæ¥ã¾ãã
è±èªããã°ã®æ¹ã«å®éã«ã¢ããªã±ã¼ã·ã§ã³ãå®è£ ãã¦å¾ãç¥è¦ããGraceful Shutdown ã®ãã¹ããã©ã¯ãã£ã¹ã¨ãã¦ã¾ã¨ãã¦ãã¾ãã常æãã¼ã¿ãæµãã¦ããã¢ããªã±ã¼ã·ã§ã³ä¸ã§å ¥å¿µã«æ¤è¨¼ãè¡ãã¾ããã
Deployment Slot ã使ãã° Graceful Shutdown ã¯ä¸è¦ã¨èããæ¹ãããã¨æãã¾ãããApp Service / Azure Functions ã® Deployment Slot ã¯æ¬çªã§ã¯ãªãå´ã®ã¹ãããã®ã¦ã©ã¼ã ã¢ãããå®äºå¾ãã«ã¼ãã£ã³ã°ãå ¥ãæ¿ãã¦æ¬çªã ã£ãã¹ããããåèµ·åãããããGraceful Shutdown ã«å¯¾å¿ããã³ã¼ããæ¸ãã¦ããªãå ´åã¯å¦çä¸ã§ãä¸å®æéå¾ã«åçç¡ç¨ã§ã·ã£ãããã¦ã³ããã¾ãã
å¾ã£ã¦ã¹ã¯ããåã«ç¢ºå®ã«å¦çãæ£å¸¸çµäºããä¿è¨¼ã¯ãªããããDeployment Slot ã使ã£ããããã¤ã®å ´åã§ã Graceful Shutdown ã¸ã®å¯¾å¿ã¯éè¦ã§ãã
ãã以åã®è©±ã¨ãã¦ãApp Service / Azure Functions ã¯ãã©ãããã©ã¼ã æ´æ°ãªã©ã®è¦å ã§ã1 ãæã«æ°åèªåçã«ã¤ã³ã¹ã¿ã³ã¹ã®ç§»åãè¡ããã¦ãã¢ããªã±ã¼ã·ã§ã³ã®åèµ·åãè¡ããã¦ãã¾ãããã®ã¿ã¤ãã³ã°ã¯å¶å¾¡ä¸å¯è½ãªã®ã§ãã¢ããªã±ã¼ã·ã§ã³ã¯ã·ã£ãããã¦ã³ã«åããå¿ è¦ãããã¾ãã
æ©è½åä½ã§ Function App ããã¸ã§ã¯ããåãã
æ£ç¢ºã«ã¯ã¹ã±ã¼ãªã³ã°ã¨ãããã¤ã®åä½ã§ Function App ããã¸ã§ã¯ããåããã®ãæ£è§£ãªã®ã§ãããæåãããã®è¾ºããè¦ããã±ã¼ã¹ã¯å°ãªãã®ã§ã¾ãã¯æ©è½åä½ã§åãããã¨ãèããããã«ãã¦ãã¾ãã
ãã¾ã« 1 ã¤ã® Function App ã«å¤§éã® Function ãå®è£ ãã¦ããã¢ããªã±ã¼ã·ã§ã³ãè¦ã¾ããããã 1 ã¤ã® Function App ã«ã¾ã¨ãã¦ããçç±ãã³ã¹ããªãæ¤è¨ããªãããã»ããè¯ãã§ãã
é常ã«èª¤è§£ããããããæ£ããç解ããã¦ããªããã¨ãå¤ã App Service ã Azure Functions ã®èª²éåä½ã§ãããç°¡åã«ã¾ã¨ããã¨ä»¥ä¸ã®éãã«ãªãã¾ãã
- Consumption Plan ã¯å®è¡åæ°ã¨å®è¡æé (GB ç§) ã§èª²é
- App Service Plan ã¯ã¤ã³ã¹ã¿ã³ã¹ã®ãµã¤ãºã¨æ°ã§èª²é
- Premium Plan ã¯æå°ã¤ã³ã¹ã¿ã³ã¹ã®ãµã¤ãºã¨æ° + 追å ã¤ã³ã¹ã¿ã³ã¹ã¯ä½¿ç¨ããç§åä½ã§èª²é
éè¦ãªã®ã¯ App Service Plan åä½ã§ã®èª²éã§ãã£ã¦ããã®ä¸ã§åä½ãã App Service / Azure Functions ã®æ°ã¯ç¡é¢ä¿ã¨ãããã¨ã§ããå¾ã£ã¦ 1 ã¤ã® App Service Plan ã«å¯¾ã㦠5 ã¤ã® Function ãæ㤠Function App ã 1 ã¤ãããã¤ããã®ã¨ã1 ã¤ã® Function ãæ㤠Function App ã 5 ã¤ãããã¤ããã®ã¯åãä¾¡æ ¼ã«ãªãã¾ãã
å½ç¶ãªãã App Service Plan ãæã¤ãªã½ã¼ã¹ã¯æéãªã®ã§ã大éã® Function App ãè¼ããã®ã¯ããã©ã¼ãã³ã¹é¢ã§ãç¾å®çã§ã¯ãªãã§ããã1 ã¤ã® Function App ã«å ¨ã¦ã®æ©è½ãã¾ã¨ããããã¨è¨ã£ã¦å®ããªããªããã¨ã¯ç¥ã£ã¦ããå¿ è¦ãããã¾ãã
ããããã©ã®ãã©ã³ãé¸ã¶ã¹ããåãããªãå ´åã¯ãå ¬å¼ããã¥ã¡ã³ãã«ããããã®æ©è½æ¯è¼è¡¨ãããã®ã§ãã©ã®ãã©ã³ã使ãã°è¦ä»¶ãæºãããã確èªãã¦ãã ããã
æ¬çªåãã®ã¢ããªã±ã¼ã·ã§ã³ã§ããã°ã大ä½ã®å ´å㯠App Service Plan ã Premium Plan ã«ãªãã¨æãã¾ããç¹ã«æè¿ã¯ VNET Integration ãå¿ è¦ã¨ãªãã±ã¼ã¹ãå¤ãã®ã§ãã¾ã Consumption Plan ã¯åè£ããæ¶ãã¾ãã
ã¤ã³ã¹ã¿ã³ã¹ã®ãµã¤ãºã¯ã¢ããªã±ã¼ã·ã§ã³ã®ç¹æ§ã«ãã£ã¦å¤æ´ãã¾ãããã»ã¨ãã©ã®ã±ã¼ã¹ã§ã¯ P1V2 ã P1V3 ã§ååãªãã¨ãå¤ãã§ããã¹ã±ã¼ã«ã¢ããã¯ç°¡åãªã®ã§ãªã½ã¼ã¹ã確èªãã¤ã¤å¯¾å¿ãã¦ãã¾ãã
æ©ããã¡ãã CI / CD ãã¤ãã©ã¤ã³ãçµã
ãã¯ãè¨ãã¾ã§ããªãã¨æãã¾ãããããå°è¦æ¨¡ã®ã¢ããªã±ã¼ã·ã§ã³ãé¤ã大ä½ã¯ Function App 㯠1 ã¤ä»¥ä¸ä½æãããã¨ãå¤ãã§ãããããã« Visual Studio ããæåã§ãããã¤ãããã¨ã¯ç¾å®çã§ã¯ãªãã®ã§ãGitHub Actions ã Azure Pipelines ã使ã£ã¦èªååãã¾ãã
ãããã¤èªä½ã¯ Action / Task ãç¨æããã¦ããã®ã¨ãé常ã«ç°¡åãªã®ã§ç¹ã«æ©ããã¨ããªãã¯ãã§ããæè¿ã§ã¯ããã«ã½ã³ã®æä¸ã« CI / CD ãã¤ãã©ã¤ã³ã¾ã§çµããã¨ãå¤ã ãããããã§ãã
æ®æ®µããã«ã½ã³ã§è©±ãã¦ããå 容ãåºæ¥ãã ãã¾ã¨ããã¤ããã§ãããæãæ¼ããããããç¥ããªãã®ã§è¿½è¨ããããããã¾ãããAzure Light-up ã«èå³ãããå ´åã¯é£çµ¡ãã ããã