Earlier this month, the AWS Amplify team unveiled support for backend functions utilizing runtimes beyond the established Node.js framework. This exciting update allows users to develop backend functions in languages such as Python, Java, Go, and .NET Core for processing requests from their REST or GraphQL APIs, handling triggers from services like Amazon DynamoDB and Amazon Kinesis Data Streams, or even for executing scheduled tasks with the Amplify CLI’s newly introduced support for cron-based Lambdas. This article will specifically delve into .NET Core support, guiding you through the creation of a REST API backend using C# and deploying it within your AWS environment through Amplify.
For this tutorial, we will utilize a browser-hosted front-end application built with Angular. Amplify also accommodates frameworks like React and Vue, and similar steps can be followed for those libraries. For further details, refer to the Getting Started guide in Amplify’s documentation, under “Framework Support.”
Setting Up Our Application
Let’s begin by confirming that we have all necessary tools installed. Below are the dependencies we need to get our application up and running:
- AWS Amplify CLI (see Amplify CLI Documentation)
- .NET Core 3.1 SDK (see Download .NET Core)
- .NET Core Global Tools for Lambda (see AWS Extensions)
- .NET Core 3.1 Global Test Tool for Lambda (see Lambda Test Tool)
For this example, we’ll construct an Angular front-end application using the Angular Command Line Interface (CLI). If the Angular CLI is not installed, follow the guide at Setting Up Angular to install it. If you already have the latest version of the Amplify CLI, you can upgrade it with the command npm i -g @aws-amplify/cli
.
Let’s create our Angular application. After installing the CLI, generate a new application with the command:
ng new AmplifyDotnet --defaults
Next, let’s run the application locally to ensure everything is functioning properly. The Angular CLI will place the application’s components in a new sub-folder named “AmplifyDotnet.” Navigate to this folder with the command cd AmplifyDotnet
. All subsequent commands will be executed from this directory. To serve the application, use the command ng serve
. This will start a lightweight web server on your machine, allowing you to connect your web browser to the application.
By default, the application is served at http://localhost:4200, but the output from the ng serve
command will indicate if a different port is being used. Accessing this address in your web browser should display a welcome page confirming that our application is operational. Notably, the ng serve
command automatically detects any changes made to the application, so we’ll keep this command running.
Integrating a REST API into Our Application
Now that our front-end is up and running, let’s incorporate Amplify support into the project. With the previous command still running, open a new command window and navigate to the project folder to initialize Amplify:
- From the command prompt in our Angular project, execute
amplify init
. - You’ll be prompted to provide information about this project. For more details on project initialization, check the Amplify Documentation regarding the init process. For this tutorial, we’ll stick with the default values provided by the CLI, except for the environment name, which we’ll set as “dev.”
- After responding to the remaining prompts, Amplify will create an environment in AWS where we will add additional backend components later.
Creating a .NET Core Function
Amplify supports various types of cloud-based backend resources, including Amazon S3 buckets, Amazon DynamoDB tables, AWS Lambda functions, Amazon API Gateway endpoints, and more. These resource types are organized into categories via the Amplify CLI, each offering its own specific commands. For Lambda functions, we’ll use the function category and the add
command to create a new function:
amplify function add
You will be prompted for details regarding your function:
- Name for the resource being created. This human-readable name will help identify the resource in your Amplify project and generate a name for the AWS CloudFormation stack. I’ll use “ServerlessDotnet,” but feel free to choose another name.
- Name for your function, corresponding to the Lambda resource. By default, this uses the resource name, but it can be changed.
- Runtime for your function. It defaults to “NodeJS,” so let’s change it to “.NET Core 3.1.”
- The template for the function. Since we plan to expose this function via API Gateway, we’ll select the “Serverless” template.
- Whether we need access to other resources defined in this Amplify project. Since this will be a standalone REST API, we will answer “no.”
- Whether we want the function to be invoked on a recurring schedule; as this function will respond to HTTP requests to our API Gateway, we’ll answer “no.”
- Whether we’d like to edit the source for the Lambda handler; we’ll skip this for now, so we’ll also answer “no” here.
The function code will be generated based on the names, runtime, and template inputs, and will be located under an “amplify/backend/function” sub-folder named after the resource. Inside, there’s a “src” sub-folder containing a C# project; you can open this project in Visual Studio or Visual Studio Code to examine the Lambda handler’s source code. If you do review the code, you will notice that it uses strongly-typed classes for handling API Gateway requests, defined in the Amazon.Lambda.APIGatewayEvents NuGet package, and currently includes hard-coded responses.
For additional insights, check out another blog post here and for expert opinions, visit this authority on the subject. Lastly, if you’re looking for an excellent resource, don’t miss the Reddit thread here.
Located at Amazon IXD – VGT2, 6401 E Howdy Wells Ave, Las Vegas, NV 89115.
Leave a Reply