Coder Social home page Coder Social logo

aspapilambdatest's Introduction

ASP.Net Core 3.1 on AWS Lambda demo

logo

As of the end of March 2020, AWS Lambda supports ASP.Net Core 3.1. As of mid-March 2020, API Gateway HTTP APIs become generally available. The combination of these two releases means that the best way (in my opinion!) of writing, deploying and running serverless web apps in the cloud is now even better.

My favourite pattern for architecting a serverless .Net website is to put a regular ASP.Net Core website into a Lambda function wholesale. This means that developers can do local development, unit tests, integration tests the exact same way they know and love and take advantage of serverless infrastructure.

This repo contains everything you need to take the standard ASP.Net Core "web API" template and continuously deploy it to AWS Lambda. Here's what's been added:

Additions to standard template

  • .github/workflows/ci.yml: This is the GitHub Actions pipeline for building and deploying this project to AWS Lambda. The steps are:

    • Setting up .Net SDK and AWS Lambda CLI
    • Run unit and integration tests
    • Run ReSharper checks and reports on PRs
    • Build and package app into a zip file suitable for upload to AWS
    • Log into AWS (this requires you to configure AWS creds in GitHub)
    • Use CloudFormation to deploy the Lambda function and HTTP API
  • src/HelloWorld/Program.cs: This file has been refactored to support the slightly different way that an ASP.Net Core app is started in Lambda. You shouldn't need to touch this file at all, except for changing logging.

  • src/HelloWorld/Startup.cs: The only change to this file is to add a (trivial) dependency-injected IValuesService to demonstrate integration testing in the test project.

  • test/HelloWorld.Tests/TestValuesController.cs: This file demonstrates ASP.Net Core integration tests in the style made possible by Microsoft.AspNetCore.Mvc.Testing. A mock IValuesService is injected. This shows that tests don't have to be written any differently just because the app is hosted in Lambda.

  • serverless.yml: This file contains the entirety of the serverless infrastructure needed to host the website. The key to the file's conciseness is the AWS::Serverless::Function that can magic up an API.

So what should I do?

First, you'll want to create your own copy of this template repo by clicking this button on the top right of this page:

Use this template

Once your repo has been created, the first run in GitHub Actions will unfortunately fail because you haven't yet setup secrets. You'll want to follow this AWS guide to setup your secrets in GitHub. You'll know it's done correctly when your secrets look like this:

Example of well-configured secrets

Finally, once your secrets are configured correctly your pipeline will run successfully. PRs have will run unit tests and building, but only the master branch will get deployed. To access your website, go to your Action's logs, click the arrow next to the Deploy step and look for the ApiUrl output. It should look something like this:

Example output

You can then navigate to that URL in your browser - and add /api/values onto the end of the URL to see the fruits of your labour!

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.