https://github.com/aidansteele/demo-serverless-aspnetcore
ASP.Net Core 3.1 on AWS Lambda demo
https://github.com/aidansteele/demo-serverless-aspnetcore
api-gateway aspnetcore aws aws-lambda serverless
Last synced: 12 months ago
JSON representation
ASP.Net Core 3.1 on AWS Lambda demo
- Host: GitHub
- URL: https://github.com/aidansteele/demo-serverless-aspnetcore
- Owner: aidansteele
- Created: 2020-04-03T03:01:54.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-08-27T01:21:09.000Z (over 2 years ago)
- Last Synced: 2025-03-28T18:54:08.032Z (about 1 year ago)
- Topics: api-gateway, aspnetcore, aws, aws-lambda, serverless
- Language: C#
- Size: 26.4 KB
- Stars: 24
- Watchers: 3
- Forks: 2
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ASP.Net Core 3.1 on AWS Lambda demo
As of the end of March 2020, AWS Lambda [supports ASP.Net Core 3.1][lambda-support].
As of mid-March 2020, API Gateway [HTTP APIs become generally available][http-api-ga].
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`](.github/workflows/ci.yml): This is the [GitHub Actions][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][resharper-action] 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][aws-action])
* Use CloudFormation to deploy the Lambda function and HTTP API
* [`src/HelloWorld/Program.cs`](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`](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`](test/HelloWorld.Tests/TestValuesController.cs):
This file demonstrates [ASP.Net Core integration tests][anc-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`](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`][sam-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:

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][aws-action]
to setup your secrets in GitHub. You'll know it's done correctly when your secrets
look like this:

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:

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!
[lambda-support]: https://aws.amazon.com/blogs/compute/announcing-aws-lambda-supports-for-net-core-3-1/
[http-api-ga]: https://aws.amazon.com/blogs/compute/building-better-apis-http-apis-now-generally-available/
[actions]: https://github.com/features/actions
[aws-action]: https://github.com/aws-actions/configure-aws-credentials
[anc-tests]: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1
[sam-function]: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
[resharper-action]: https://github.com/glassechidna/resharper-action