This repository provides you code samples along with the guidance how you can build serverless .NET APIs on AWS and secure them with Amazon Cognito.
This repository contains sample .NET applications that can be deployed as Lambda functions and exposed as APIs using the Amazon API Gateway REST API.
The following AWS services are used while building serverless .NET APIs on AWS:
- Amazon API Gateway
- AWS Lambda Function
- Amazon Cognito
- AWS CDK (used for the deployment)
- Benefits of building serverless .NET APIs
- Architecture
- Directory Structure
- How to deploy
- API Gateway routes
- How to Test
Below are some of the primary benefits of building serverless .NET APIs on AWS:
- Cost-effective APIs
- Completely serverless, no maintainence overhead
- Highly available
- Highly scalable
- Secured with Cognito
Here's what you need to install to use the AWS CDK.
- Node.js 14.15.0 or later
- AWS CLI
- AWS CDK Toolkit
- .NET 6.0 or later, available here
After having all the above prerequisites, you must establish how the AWS CDK authenticates with AWS. To learn how to do this, refer authentication with AWS.
- Run
git clone <repo-url>
to clone the repository. - Run
cd ./cdk/src
to navigate to the CDK application project. - Run
dotnet restore
to install all the necessary dependencies. - If this is your first time deploying the CDK stack in the AWS account, run
cdk bootstrap
. - Run
dotnet build
to compile the application and ensure there are no build errors. - Run
cd..
to return one level back wherecdk.json
file is present. - Run
cdk deploy
to deploy your CDK stack to AWS. - Confirm the changes when prompted. Type "y" to proceed with the deployment.
- Once the deployment is complete, monitor the AWS CloudFormation events to ensure that the stack creation/update was successful.
After the deployment, you can access the following endpoints to check the behavior of the serverless .NET APIs running on the AWS cloud.
https://<random-text>.execute-api.<region>.amazonaws.com/<stage-name>/
|-- aspnet/
| |-- anonymouse-example/{proxy+}
| |-- authentication-example/{proxy+}
| |-- authorization-example/{proxy+}
| `-- custom-scopes-authorization-example/{proxy+}
`-- dotnet/
|-- anonymouse-example
`-- authorization-example
- Use ThunderClient extension in VS Code to test the APIs.
- Import the
collections
andenvironment variables
in ThunderClient fromthunder-client
folder. - Update the environment variable values with their actual values.
- Save everything.
- You're good to go!