GithubHelp home page GithubHelp logo

peterhe2000 / dotnet-core-aws-lambda-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kenhalbert/dotnet-core-aws-lambda-example

0.0 2.0 0.0 12 KB

A simple AWS Lambda function written in C#, with a unit test project. This was written to demonstrate some patterns that I've found to be useful in C# Lambda functions and to serve as a starting point for developers who are new to developing C# Lambda functions and want to know how to solve problems like bootstrapping, dependency injection, and configuration in a serverless C# application.

C# 100.00%

dotnet-core-aws-lambda-example's Introduction

dotnet-core-aws-lambda-example

A simple AWS Lambda function written in C#, with a unit test project. This was written to demonstrate some patterns I've found to be useful in C# Lambda functions, and to serve as a starting point for developers who are new to developing C# Lambda functions and want to know how to solve problems like bootstrapping, dependency injection, and configuration in a serverless C# application.

Here are the most important patterns demonstrated here:

  1. Using the default constructor of the Function class to bootstrap any services the function depends on. I like to do things this way because it lets me use an overloaded constructor to inject mocks in unit tests.
  2. Using the .NET Core configuration API (Microsoft.Extensions.Configuration) to read from both a local config file and Lambda environment variables to build the function's configuration object.

Setup

Prerequisites

Make sure you've installed a version of the .NET Core SDK that supports netstandard1.4 (for the Lambda function project) and netcoreapp1.1 (for the unit test project).

Building and deploying the Lambda function

  1. Navigate to the directory the Lambda function's .csproj file is in (./DotnetCoreLambdaExample) and run dotnet restore
  2. In the same directory, run dotnet publish -o lambda
  3. Navigate to ./DotnetCoreLambdaExample/lambda and zip all the contents of the directory. Keep the zip file somewhere safe for the next step...
  4. Create a new Lambda function in the AWS Lambda console
    • When prompted to provide a zip file containing your Lambda's code, upload the zip file from step 3
    • Specify the handler as DotnetCoreLambdaExample::DotnetCoreLambdaExample.Function::Handler
    • Optionally provide a value for the MessageToReturn environment variable the code uses. The value you set here will be included in the result of the Lambda invocation

Running the Lambda function in AWS

After following the steps above to build & deploy the Lambda function, invoke it in the AWS Lambda Console using this event JSON: { "myProperty": "Hello world!" }

Running the unit tests

  1. Navigate to the directory the test project's .csproj file is in (./DotnetCoreLambdaExample.Tests) and run dotnet restore
  2. In the same directory, run dotnet test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.