1. 程式人生 > 程式設計 >【譯】Pulumi:.NET Core雲架構

【譯】Pulumi:.NET Core雲架構

原文連結

本月伊始,Pulumi宣佈在他們支援的語言中新增了.NET Core。Pulumi是一個開源工具,支援在多個雲供應商上以程式碼的形式建立,開發並管理基礎架構,類似於HashiCorp Terraform

架構即程式碼(IaC)就是使用配置檔案以可描述的模型來管理架構的過程,通常與持續交付相結合用於DevOps。大部分主要的雲供應商提供了他們自己的IaC解決方案,通常基於JSON或YAML配置檔案。然後這些檔案被整合到開發過程中,被包含在程式碼庫中,並進行版本控制。IaC工具包括Chef,Puppet,AWS CloudFormation以及Azure Resource Manager(ARM)。

去年才推出,Pulumi在Iac領域可以說是初出茅廬。根據他們網站的說法:

“Pulumi雲開發平臺是一系列工具,庫,執行時以及服務,為雲原生基礎架構提供一致的開發和運營控制平面,Pulumi不僅使您能夠以程式碼形式管理基礎結構,而且還允許您使用真正的程式語言(及其所有支援工具)而不是YAML來定義和管理基礎結構。”

類似於HashiCorp Terraform,Pulumi使用程式對虛擬環境進行分割槽,配置和擴充套件。不同之處在於,在Terraform中這些程式是使用自定義的特定領域語言(DSL)編寫的,Pulumi的程式使用通用語言。.NET Core的支援使得Pulumi程式可以使用C#,VB.NE或F#編寫。

使用通用語言允許將IaC與現存的語言生態結合起來。就.NET而言,好處包括與已有的IDE(包括Visual Studio 和 Visual Studio Code)整合,NuGet支援(包括使用現有庫以及釋出Iac程式),並使用標準的編譯錯誤。

使用.NET編寫Iac程式還可以使用語言特定的資源,例如LINQ以及非同步程式碼。下面的程式碼片段展示瞭如何使用一個無服務的Azure AppService FunctionApp建立一個Azure CosmosDB並根據資料庫自動縮放:

using System;
using System.Collections.Generic;

using Pulumi;
using Pulumi.Azure.AppService;
using Pulumi.Azure.Core;
using Pulumi.Azure.CosmosDB;

class Program
{
    static Task<int> Main(string[] args)
    {
        return Deployment.RunAsync(() => {
            var locations = new[] { "WestUS","WestEurope","SouthEastAsia" };

            var rg = new ResourceGroup("myapp-rg",new ResourceGroupArgs {
                Location = locations[0],});

            var app = new CosmosApp("myapp",new CosmosAppArgs {
                 ResourceGroup = resourceGroup,Locations = locations,DatabaseName = "pricedb",ContainerName = "prices",Factory = (location,db) => {
                     var func = new ArchiveFunctionApp("myapp-func",new ArchiveFunctionAppArgs {
                              Location = location,Archive = new FileArchive("app"),AppSettings = new Dictionary<string,string> {
                                  ["COSMOSDB_ENDPOINT"] = db.Endpoint,},);
                      return func.App.ID;
                 },});
        });
    }

    // Definitions of CosmosApp and ArchiveFunctionApp elided for brevity.
    // Actual runtime application code is stored in the "app" directory.
    // See link to the full example at the end of this article.
}
複製程式碼

完整示例

除了.NET,Pulumi還支援JavaScript,TypeScript,Python以及Go。Pulumi已經在GitHub上開源。

支援的雲供應商