简体   繁体   中英

How to store and load custom json appsettings and Connection Strings in an AWS Lambda Project?

I am developing an Alexa Skills Kit app in Visual Studio and have created and AWS Lambda Project in .NET Core 2.0. When creating the project, it automatically comes with a file called aws-lambda-tools-defaults.json. The settings here are used by the AWS Lambda function.

Now, I want to store my own appsettings and Connection Strings for use by the application. What is the preferred route on how I can store my settings and get them in code?

I've gotten as far as adding the NuGet package called Microsoft.Extensions.Configuration.Json. But I'm not sure what I do from here.

1) Do I need to create another json file, or can I use the existing file aws-lambda-tools-defaults.json? If I do need to create a new file, what should it be called?

2) Is there a Constructor I need to add to the Function.cs file in order to load json file?

3) What code can I write that will actually get my settings and Connection Strings?

I'm a bit lost here, so really any advice would be helpful! Thank you!

  1. Yes, you do need to create another files, which will be used depending on the environment. However, aws-lambda-tools-defaults.json is not the file that needs to hold the connection strings, and other app configuration settings. Instead, it is the appsettings.json file.

在此处输入图片说明

2 and 3. You can follow microsoft's instructions on how to use the configuration files from here on.

Basically, in the Startup file, you can get the configurations via IConfiguration :

Configuration.GetValue<string>("myConfigValue");

Specifically, for aws serverless application with netcore 2.1, if you want to use appsettings.{environment}.json , you can do the following:

    /// <summary>
    /// The Main function can be used to run the ASP.NET Core application locally using the Kestrel webserver.
    /// </summary>
    public static class LocalEntryPoint
    {
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((builderContext, config) =>
                {
                    string defaultAppSettingsFile = "appsettings.json";

                    // because of a bug in Visual Studio 2017 version 15.8.2 (newest at this time) with NetCore projects, simply changing the configuration won't work
                    // you have to Close, and then Re-open the project in order for the Conditional Compilation Symbols to be taken in effect
#if Debug
                    string appSettingsFileToAdd = "appsettings.Debug.json";
#elif Beta
                    string appSettingsFileToAdd = "appsettings.Beta.json";
#elif Release
                    string appSettingsFileToAdd = "appsettings.Release.json";
#else
                    string appSettingsFileToAdd = "appsettings.Debug.json";
#endif
                    config
                        .AddJsonFile(
                            defaultAppSettingsFile,
                            optional: false,
                            reloadOnChange: true)
                        .AddJsonFile(
                            appSettingsFileToAdd,
                            optional: true,
                            reloadOnChange: true);
                })
                .UseStartup<Startup>()
                .Build();

        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }
    }

You would need to create your own ServiceCollection and the IConfiguration to it. I've created some attraction called ILambdaHost, that does it in a similar way to IHose. I don't no recommend using IWebHost because you don't need a web host functionality inside a Lambda, it would be a lot of overhead.

Here is how you create the configuration class and add the appsettings.json to it.

using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace MyNamespace
{
    public static class LambdaConfigurationBuilder
    {
         public static IConfiguration Build() => new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .Build();
    }
}



   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM