簡體   English   中英

如何使用 asp.net core 3.1 serverless 在 AWS lambda 中為復雜的配置參數設置環境變量?

[英]How to set environment variables for complex configuration parameters in AWS lambda using asp.net core 3.1 serverless?

In my asp.net core 3.1 web API launchsettings.json I have a environment variable named "AdminstratorConfig:AdminstratorPassword": "myPasswordValue"

現在在我的代碼中,我還有一個名為AppSettings的 class 定義如下:

public class AppSettings
{
    public AdminstratorConfiguration AdminstratorConfig { get; set; }
}

public class AdminstratorConfiguration
{
    public string AdminstratorPassword { get; set; }
}

本地運行時,我可以在Startup中使用類似的東西將環境變量綁定到我的AppSettings實例中

public class Startup
{

    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        var appSettings = new AppSettings();
        Configuration.Bind(appSettings);
        // Here appSettings.AdminstratorConfig.AdminstratorPassword contains value 'myPasswordValue' 
    }
}

如果我的配置定義為

{
   "AdminstratorConfig": 
    {
       "AdminstratorPassword": "myPasswordValue"
    }
}

但是,在將我的應用程序部署為 AWS 無服務器 lambda 后,我嘗試在 Lambda 配置部分設置相同的環境變量,但此處不允許使用特殊字符“

有沒有一種方法可以像我的本地一樣在 AWS Lambda 中設置和加載這些復雜的環境變量? 如果不是,可能的替代方法是什么?

您可以使用 __(雙下划線)代替:(冒號),因此 lambda 中的環境變量將為:“AdministratorConfig__AdministratorPassword”作為鍵,您的“myPasswordValue”作為值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM