├── .gitattributes ├── .gitignore ├── NetCoreLambda.Tests ├── FunctionTest.cs └── NetCoreLambda.Tests.csproj ├── NetCoreLambda.sln ├── NetCoreLambda ├── ConfigurationService.cs ├── Constants.cs ├── EnvironmentService.cs ├── Function.cs ├── IConfigurationService.cs ├── IEnvironmentService.cs ├── NetCoreLambda.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── appsettings.Development.json ├── appsettings.json └── aws-lambda-tools-defaults.json └── Readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /NetCoreLambda.Tests/FunctionTest.cs: -------------------------------------------------------------------------------- 1 | using Xunit; 2 | using Amazon.Lambda.TestUtilities; 3 | using Microsoft.Extensions.Configuration; 4 | using Moq; 5 | 6 | namespace NetCoreLambda.Tests 7 | { 8 | public class FunctionTest 9 | { 10 | [Fact] 11 | public void Function_Should_Return_Config_Variable() 12 | { 13 | // Mock IConfiguration 14 | var expected = "val1"; 15 | var mockConfig = new Mock(); 16 | mockConfig.Setup(p => p[It.IsAny()]).Returns(expected); 17 | 18 | // Mock IConfigurationService 19 | var mockConfigService = new Mock(); 20 | mockConfigService.Setup(p => p.GetConfiguration()).Returns(mockConfig.Object); 21 | 22 | // Invoke the lambda function and confirm config value is returned 23 | var function = new Function(mockConfigService.Object); 24 | var result = function.FunctionHandler("env1", new TestLambdaContext()); 25 | Assert.Equal(expected, result); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /NetCoreLambda.Tests/NetCoreLambda.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | all 16 | runtime; build; native; contentfiles; analyzers 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /NetCoreLambda.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.168 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreLambda", "NetCoreLambda\NetCoreLambda.csproj", "{F2F29A2A-9893-46E4-A64B-67C198234912}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreLambda.Tests", "NetCoreLambda.Tests\NetCoreLambda.Tests.csproj", "{B4C11B6D-D4D9-4AFD-989E-3F499348CA35}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {F2F29A2A-9893-46E4-A64B-67C198234912}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {F2F29A2A-9893-46E4-A64B-67C198234912}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {F2F29A2A-9893-46E4-A64B-67C198234912}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {F2F29A2A-9893-46E4-A64B-67C198234912}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {B4C11B6D-D4D9-4AFD-989E-3F499348CA35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {B4C11B6D-D4D9-4AFD-989E-3F499348CA35}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {B4C11B6D-D4D9-4AFD-989E-3F499348CA35}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {B4C11B6D-D4D9-4AFD-989E-3F499348CA35}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {BF05F5B2-2F4B-4172-857E-E8765AA6F824} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /NetCoreLambda/ConfigurationService.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.Extensions.Configuration; 3 | 4 | namespace NetCoreLambda 5 | { 6 | public class ConfigurationService : IConfigurationService 7 | { 8 | public IEnvironmentService EnvService { get; } 9 | 10 | public ConfigurationService(IEnvironmentService envService) 11 | { 12 | EnvService = envService; 13 | } 14 | 15 | public IConfiguration GetConfiguration() 16 | { 17 | return new ConfigurationBuilder() 18 | .SetBasePath(Directory.GetCurrentDirectory()) 19 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 20 | .AddJsonFile($"appsettings.{EnvService.EnvironmentName}.json", optional: true) 21 | .AddEnvironmentVariables() 22 | .Build(); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /NetCoreLambda/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace NetCoreLambda 2 | { 3 | public static class Constants 4 | { 5 | public static class EnvironmentVariables 6 | { 7 | public const string AspnetCoreEnvironment = "ASPNETCORE_ENVIRONMENT"; 8 | } 9 | 10 | public static class Environments 11 | { 12 | public const string Production = "Production"; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /NetCoreLambda/EnvironmentService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using static NetCoreLambda.Constants; 3 | 4 | namespace NetCoreLambda 5 | { 6 | public class EnvironmentService : IEnvironmentService 7 | { 8 | public EnvironmentService() 9 | { 10 | EnvironmentName = Environment.GetEnvironmentVariable(EnvironmentVariables.AspnetCoreEnvironment) 11 | ?? Environments.Production; 12 | } 13 | 14 | public string EnvironmentName { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /NetCoreLambda/Function.cs: -------------------------------------------------------------------------------- 1 | using Amazon.Lambda.Core; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. 5 | [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] 6 | 7 | namespace NetCoreLambda 8 | { 9 | public class Function 10 | { 11 | // Configuration Service 12 | public IConfigurationService ConfigService { get; } 13 | 14 | public Function() 15 | { 16 | // Set up Dependency Injection 17 | var serviceCollection = new ServiceCollection(); 18 | ConfigureServices(serviceCollection); 19 | var serviceProvider = serviceCollection.BuildServiceProvider(); 20 | 21 | // Get Configuration Service from DI system 22 | ConfigService = serviceProvider.GetService(); 23 | } 24 | 25 | // Use this ctor from unit tests that can mock IConfigurationService 26 | public Function(IConfigurationService configService) 27 | { 28 | ConfigService = configService; 29 | } 30 | 31 | /// 32 | /// A simple function that takes a string and does a ToUpper 33 | /// 34 | /// 35 | /// 36 | /// 37 | public string FunctionHandler(string input, ILambdaContext context) 38 | { 39 | // Get config setting using input as a key 40 | return ConfigService.GetConfiguration()[input] ?? "None"; 41 | } 42 | 43 | private void ConfigureServices(IServiceCollection serviceCollection) 44 | { 45 | // Register services with DI system 46 | serviceCollection.AddTransient(); 47 | serviceCollection.AddTransient(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /NetCoreLambda/IConfigurationService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Configuration; 2 | 3 | namespace NetCoreLambda 4 | { 5 | public interface IConfigurationService 6 | { 7 | IConfiguration GetConfiguration(); 8 | } 9 | } -------------------------------------------------------------------------------- /NetCoreLambda/IEnvironmentService.cs: -------------------------------------------------------------------------------- 1 | namespace NetCoreLambda 2 | { 3 | public interface IEnvironmentService 4 | { 5 | string EnvironmentName { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /NetCoreLambda/NetCoreLambda.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.1 5 | true 6 | Lambda 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Always 23 | 24 | 25 | Always 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /NetCoreLambda/Program.cs: -------------------------------------------------------------------------------- 1 | namespace NetCoreLambda 2 | { 3 | public class Program 4 | { 5 | public static void Main(string[] args) 6 | { 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /NetCoreLambda/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "Mock Lambda Test Tool": { 4 | "commandName": "Executable", 5 | "commandLineArgs": "--port 5050", 6 | "executablePath": "C:\\Users\\Tony\\.dotnet\\tools\\dotnet-lambda-test-tool-2.1.exe", 7 | "workingDirectory": ".\\bin\\Debug\\netcoreapp2.1", 8 | "environmentVariables": { 9 | "ASPNETCORE_ENVIRONMENT": "Development" 10 | } 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /NetCoreLambda/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "env1": "dev-val1", 3 | "env2": "dev-val2", 4 | "env3": "dev-val3" 5 | } 6 | -------------------------------------------------------------------------------- /NetCoreLambda/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "env1": "val1", 3 | "env2": "val2", 4 | "env3": "val3" 5 | } 6 | -------------------------------------------------------------------------------- /NetCoreLambda/aws-lambda-tools-defaults.json: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "Information" : [ 4 | "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.", 5 | "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.", 6 | "dotnet lambda help", 7 | "All the command line options for the Lambda command can be specified in this file." 8 | ], 9 | "profile" : "default", 10 | "region" : "eu-west-1", 11 | "configuration" : "Release", 12 | "framework" : "netcoreapp2.1", 13 | "function-runtime" : "dotnetcore2.1", 14 | "function-memory-size" : 256, 15 | "function-timeout" : 30, 16 | "function-handler" : "NetCoreLambda::NetCoreLambda.Function::FunctionHandler", 17 | "environment-variables" : "\"ASPNETCORE_ENVIRONMENT\"=\"Staging\";\"env1\"=\"val1\";\"env2\"=\"val2\"", 18 | "function-name" : "NetCoreLambda", 19 | "function-role" : "arn:aws:iam::229130418844:role/aws-netcore-demo-DefaultFunctionRole-167PAFDM1ZB7O", 20 | "tracing-mode" : "PassThrough" 21 | } -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function with Dependency Injection and Configuration 2 | 3 | Demonstrates how to add .NET Core dependency injection and configuration services to an AWS Lambda Function project for .NET Core 2.1. 4 | 5 | ## Summary 6 | 7 | The idea behind this approach is to leverage the built-in configuration system of .NET Core, which can accept mulitple inputs that can override one another. This allows for use of an **appsettings.json** file with settings entries that can be overriden by environment variables applied when the Lambda Function is deployed. This allows for values that will be available when debugging the function locally, as well as values that can be set as part of a CICD pipeline. 8 | 9 | ## Prerequisites 10 | 11 | - Visual Studio 2017 or greater. 12 | - AWS Toolkit for Visual Studio 2017. 13 | - .NET Core 2.1 SDK 14 | 15 | ## Setup Steps 16 | 17 | 1. Open VS 2017 and create a new project. 18 | - Select **AWS Lambda Project with Tests**. 19 | 20 | 1. Add **version 2.1** following NuGet Packages 21 | - Microsoft.Extensions.Configuration 22 | - Microsoft.Extensions.Configuration.EnvironmentVariables 23 | - Microsoft.Extensions.Configuration.FileExtensions 24 | - Microsoft.Extensions.Configuration.Json 25 | - Microsoft.Extensions.DependencyInjection 26 | 27 | 1. Add an **appsettings.json** file to the project. 28 | - Add the following content: 29 | 30 | ```json 31 | { 32 | "env1": "val1", 33 | "env2": "val2", 34 | "env3": "val3" 35 | } 36 | ``` 37 | 38 | 1. Add an **appsettings.Development.json** file to the project. 39 | - Add the following content: 40 | 41 | ```json 42 | { 43 | "env1": "dev-val1", 44 | "env2": "dev-val2", 45 | "env3": "dev-val3" 46 | } 47 | ``` 48 | 49 | 1. Select both JSON files, open the Propeties window of Visual Studio, then set the `BuildAction` property to **Content** and the `Copy to Output Directory` property to **Copy always**. 50 | 51 | 1. Open the **aws-lambda-tools-defaults.json** file and add the following: 52 | 53 | ```json 54 | "environment-variables" : "\"ASPNETCORE_ENVIRONMENT\"=\"Development\";\"env1\"=\"val1\";\"env2\"=\"val2\"", 55 | ``` 56 | 57 | 1. Add an `environmentVariables` property to the **launchSettings.json** file. 58 | 59 | ```json 60 | "environmentVariables": { 61 | "ASPNETCORE_ENVIRONMENT": "Development" 62 | } 63 | ``` 64 | 65 | 1. Add a `Constants` class to the project. 66 | 67 | ```csharp 68 | public static class Constants 69 | { 70 | public static class EnvironmentVariables 71 | { 72 | public const string AspnetCoreEnvironment = "ASPNETCORE_ENVIRONMENT"; 73 | } 74 | 75 | public static class Environments 76 | { 77 | public const string Production = "Production"; 78 | } 79 | } 80 | ``` 81 | 82 | 1. Add a `IEnvironmentService` interface to the project. 83 | 84 | ```csharp 85 | public interface IEnvironmentService 86 | { 87 | string EnvironmentName { get; set; } 88 | } 89 | ``` 90 | 91 | 1. Add a `EnvironmentService` class to the project. 92 | 93 | ```csharp 94 | public class EnvironmentService : IEnvironmentService 95 | { 96 | public EnvironmentService() 97 | { 98 | EnvironmentName = Environment.GetEnvironmentVariable(EnvironmentVariables.AspnetCoreEnvironment) 99 | ?? Environments.Production; 100 | } 101 | 102 | public string EnvironmentName { get; set; } 103 | } 104 | ``` 105 | 106 | 1. Add a `IConfigurationService` interface to the project. 107 | 108 | ```csharp 109 | public interface IConfigurationService 110 | { 111 | IConfiguration GetConfiguration(); 112 | } 113 | ``` 114 | 115 | 1. Add a `ConfigurationService` class to the project. 116 | 117 | ```csharp 118 | public class ConfigurationService : IConfigurationService 119 | { 120 | public IConfiguration GetConfiguration() 121 | { 122 | return new ConfigurationBuilder() 123 | .SetBasePath(Directory.GetCurrentDirectory()) 124 | .AddJsonFile("appsettings.json") 125 | .AddEnvironmentVariables() 126 | .Build(); 127 | } 128 | } 129 | ``` 130 | 131 | 1. Add a `ConfigureServices` method to the `Function` class in order to register services with the DI system. 132 | 133 | ```csharp 134 | private void ConfigureServices(IServiceCollection serviceCollection) 135 | { 136 | // Register services with DI system 137 | serviceCollection.AddTransient(); 138 | serviceCollection.AddTransient(); 139 | } 140 | ``` 141 | 142 | 1. Add a property and two constructors to the `Function` class. 143 | 144 | ```csharp 145 | // Configuration Service 146 | public IConfigurationService ConfigService { get; } 147 | 148 | public Function() 149 | { 150 | // Set up Dependency Injection 151 | var serviceCollection = new ServiceCollection(); 152 | ConfigureServices(serviceCollection); 153 | var serviceProvider = serviceCollection.BuildServiceProvider(); 154 | 155 | // Get Configuration Service from DI system 156 | ConfigService = serviceProvider.GetService(); 157 | } 158 | 159 | // Use this ctor from unit tests that can mock IConfigurationService 160 | public Function(IConfigurationService configService) 161 | { 162 | ConfigService = configService; 163 | } 164 | ``` 165 | 166 | 1. Flesh out the `FunctionHandler` method to use the `ConfigService`. 167 | - Use the input parameter for the key used to retrieve a setting. 168 | 169 | ```csharp 170 | public string FunctionHandler(string input, ILambdaContext context) 171 | { 172 | // Get config setting using input as a key 173 | return ConfigService.GetConfiguration()[input] ?? "None"; 174 | } 175 | ``` 176 | 177 | ## Local Debugging 178 | 179 | 1. Press F5 to launch the Mock Lambda Test Tool and start debugging. 180 | 181 | 1. Enter `"env1"` into Function Input, and click Execute Function. 182 | - A value of `"val1"` should be returned base on the appsettings.json file. 183 | 184 | ## Unit Testing 185 | 186 | 1. Add the NuGet package for **Moq** to the Test project. 187 | 188 | 1. Add a reference to the **NetCoreLambda** project. 189 | 190 | 1. Mock `IConfiguration` (from Microsoft.Extensions.Configuration) to return the expected value. 191 | 192 | ```csharp 193 | var expected = "val1"; 194 | var mockConfig = new Mock(); 195 | mockConfig.Setup(p => p[It.IsAny()]).Returns(expected); 196 | ``` 197 | 198 | 1. Mock `IConfigurationService` to return the mock `IConfiguration`. 199 | 200 | ```csharp 201 | var mockConfigService = new Mock(); 202 | mockConfigService.Setup(p => p.GetConfiguration()).Returns(mockConfig.Object); 203 | ``` 204 | 205 | 1. Invoke the lambda function and confirm config value is returned. 206 | 207 | ```csharp 208 | var function = new Function(mockConfigService.Object); 209 | var result = function.FunctionHandler("env1", new TestLambdaContext()); 210 | Assert.Equal(expected, result); 211 | ``` 212 | 213 | ## Deployment 214 | 215 | 1. Right-click on the project in the Solutions Explorer and select Publish to AWS Lambda. 216 | 217 | 2. Under Configuration you can change the values for the environment variables, and these will override the values from the appsettings.json file published with the Lambda Function. 218 | 219 | ## Here are some steps to follow to get started from the command line: 220 | 221 | Once you have edited your template and code you can deploy your application using the [Amazon.Lambda.Tools Global Tool](https://github.com/aws/aws-extensions-for-dotnet-cli#aws-lambda-amazonlambdatools) from the command line. 222 | 223 | Install Amazon.Lambda.Tools Global Tools if not already installed. 224 | ``` 225 | dotnet tool install -g Amazon.Lambda.Tools 226 | ``` 227 | 228 | If already installed check if new version is available. 229 | ``` 230 | dotnet tool update -g Amazon.Lambda.Tools 231 | ``` 232 | 233 | Execute unit tests 234 | ``` 235 | cd "NetCoreLambda/test/NetCoreLambda.Tests" 236 | dotnet test 237 | ``` 238 | 239 | Deploy function to AWS Lambda 240 | ``` 241 | cd "NetCoreLambda/src/NetCoreLambda" 242 | dotnet lambda deploy-function 243 | ``` 244 | --------------------------------------------------------------------------------