It is sometimes useful to add multiple configuration sources. This example illustrates the use of a custom config factory to achieve that.
-
Create a new netcoreapp3.1 and add the following packages:
- Lambdajection
-
Create a ConfigFactory that enables file-based configuration. Set the ConfigFactory argument in the Lambda Handler attribute to point to your config factory's type.
-
Create a Config / Options class that will be used to hold your configuration items. Inject this class into your handler and read values from it inside the
Handle
method. -
Add a Startup Class and configure any services that will be injected into your Lambda Handler in here.
-
Create a CloudFormation template for your Lambda.
- Note that we are using the serverless transform in the example, however using the transform is optional.
- During a build, the generator will use your config factory in place of the default one. The type of this class is added as a generic argument to the Lambda Host.
- When the lambda is run, the Lambda Host Builder will create a new instance of your config factory and create a configuration instance using that factory. That config instance will then be added to the service collection / provider passed to the lambda host as well as your startup class.