This example illustrates the use of both option classes and encrypted values within them. On startup, any option property annotated with the [Encrypted] attribute will be automatically decrypted before being passed to your Lambda Function.
-
Create a new netcoreapp3.1 and add the following packages:
- Lambdajection
- Lambdajection.Encryption
- Amazon.Lambda.Serialization.SystemTextJson (optional - may use other serializer)
-
Add a Startup Class and configure services that will be injected into your Lambda Handler in here.
- An IDecryptionService will be injected into your container without any need for specific configuration.
- The default IDecryptionService uses KMS to decrypt options.
- You can use your own IDecryptionService by injecting it into the container as a singleton in your Startup file.
-
Add a Lambda Handler containing the code that will run when the Lambda is invoked.
-
Add one or more Option classes
- Encrypted values should be annotated with the [Encrypted] attribute.
- Please note that the [Encrypted] attribute cannot be accessed with reflection since it is only present at build time.
-
Create a CloudFormation template for your Lambda.
- Add environment variables for each of your options. Naming goes like:
SectionName__OptionName
. - Note that we are using the serverless transform in the example, however using the transform is optional.
- Add environment variables for each of your options. Naming goes like:
- During a build, the Code Generator scans your code for option classes. For each option class it finds, it keeps track of the option class, the section name provided in the [LambdaOptions] attribute, and a list of encrypted properties.
- Generator builds a LambdaConfigurator subclass inside your Lambda class. This will contain a ConfigureOptions method, which will configure each option class.
- If there were any encrypted properties found:
- An IDecryptionService will be injected into the IoC container inside LambdaConfigurator.ConfigureOptions.
- A Decryptor class is generated that uses the IDecryptionService to decrypt each encrypted property. This will also be injected into the IoC container.
- When the Lambda Host is setup, it will first call your startup class' ConfigureServices then the LambdaConfigurator.ConfigureOptions method - that way you can use your own decryption service if you want.