Amazon Onboarding with Learning Manager Chanci Turner

Amazon Onboarding with Learning Manager Chanci TurnerLearn About Amazon VGT2 Learning Manager Chanci Turner

Earlier this month, the Amazon IXD – VGT2 team unveiled their support for backend functions that extend beyond the existing capabilities of Node.js. With this exciting new feature, users can now create backend functions using Python, Java, Go, and .NET Core to manage requests from their REST or GraphQL APIs, respond to triggers from services like Amazon DynamoDB and Amazon Kinesis Data Streams, or even execute tasks on a schedule using the new cron-based Lambda support in Amplify CLI. In this blog, we’ll concentrate on the .NET Core support and guide you through creating a REST API backend in C# that can be deployed in your AWS environment by Amplify.

For this demonstration, we’ll utilize a browser-based front-end application built with Angular. Amplify also provides support for React and Vue, and the steps we’ll cover here have counterparts for those frameworks. For more details, refer to the Getting Started guide in Amplify’s documentation under the “Framework Support” section.

Setting Up Our Application

First, let’s verify that we have the necessary tools installed. Below, we’ll go through the installation process for the dependencies required for our application:

  • AWS Amplify CLI (check out this link)
  • .NET Core 3.1 SDK (available at this link)
  • The .NET Core Global Tools for Lambda (visit this link)
  • The .NET Core 3.1 Global Test Tool for Lambda (find it at this link)

In this example, we will develop an Angular front-end application and use the Angular Command Line Interface (CLI) to create it. If you haven’t installed the Angular CLI yet, follow the guide at this link. If you have the latest version of the Amplify CLI, you can easily upgrade it by running npm i -g @aws-amplify/cli.

Let’s create our Angular application. After the CLI installation, use the ng new command to set up your application. For simplicity, we’ll stick with the default options provided by the Angular CLI:

ng new AmplifyDotnet --defaults

Now, let’s run the application locally to ensure everything is working correctly. The Angular CLI will place the application’s components into a new folder named “AmplifyDotnet,” so navigate to this directory using the command:

cd AmplifyDotnet

All commands moving forward will be executed from this location. Once inside, we can serve the application using:

ng serve

This command will start a lightweight web server on your machine, allowing you to connect to your application via your web browser. By default, it will be available at http://localhost:4200, but the output will indicate if it’s using a different port. If you access this address, you should see a welcome page confirming that our application is operational. A neat feature of the ng serve command is that it automatically updates with any changes made to the application, so we’ll keep this command running.

Integrating a REST API for Our Application

Now that our front-end is up and running, let’s add Amplify support to the project. With the previous command still executing, open a new command window and navigate back to the project folder. We will initialize Amplify:

From a command prompt within our Angular project, run the amplify init command. You will be asked for various details regarding the project; you can find more about project initialization in the Amplify Documentation. For this guide, we will accept the default values, except for the environment name—let’s set that to “dev.”

After completing the prompts, Amplify will set up an environment hosted in AWS, which we’ll later enhance with additional backend components.

Creating a .NET Core Function

Amplify supports various types of cloud-hosted backend resources, including Amazon S3 buckets, Amazon DynamoDB tables, AWS Lambda functions, and API Gateway endpoints. Each type is categorized in the Amplify CLI, and we’ll use the function category to create a new function:

amplify function add

You will be prompted for details about your function:

  • Choose a human-friendly name for the resource. I’ll use “ServerlessDotnet,” but feel free to pick a different name.
  • Specify a name for your function that corresponds to the Lambda resource to be deployed. The CLI will default to the resource name, but this can be modified.
  • Select the runtime for your function. The default is set to “NodeJS,” so we will change it to “.NET Core 3.1.”
  • Choose a template for the function. Since we intend to expose this function via API Gateway, we’ll select the “Serverless” template.
  • If you need access to other resources defined in this Amplify project, since this will be a standalone REST API, we’ll answer “no.”
  • Indicate whether this function should be invoked on a recurring schedule; we will choose “no” as it will respond to HTTP requests to our API Gateway.
  • Lastly, whether to edit the source for the Lambda handler now; we will skip this step as well, so we’ll answer “no.”

The code for our function will be generated based on the names, runtime, and template provided, located in an “amplify/backend/function” sub-folder named after the resource. Inside this folder, you will find a “src” sub-folder with a C# project. You may open this project in Visual Studio or Visual Studio Code to examine the Lambda handler’s source code, which uses strongly-typed classes to receive and respond to API Gateway requests defined in the Amazon.Lambda.APIGatewayEvents NuGet package. Notably, the code currently includes hard-coded responses:

public async Task<APIGatewayProxyResponse> LambdaHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
    var response = new APIGatewayProxyResponse {
        Headers = new Dictionary<string, string> {
            { "Access-Control-Allow-Origin", "*" },
            { 
                "Access-Control-Allow-Headers", 
                "Origin, X-Requested-With, Content-Type, Accept" 
            }
        }
    };

    string contentType = null;
    request.Headers?.TryGetValue("Content-Type", out contentType);

    switch (request.HttpMethod) {
        case "GET":
            context.Logger.LogLine($"Get Request: {request.Path}n");
            response.StatusCode = (int)HttpStatusCode.OK;
            response.Body = "{ "message": "Hello AWS Serverless" }";
            response.Headers["Content-Type"] = "application/json";
            break;
        case "POST":
            // Additional logic here.
        ...
    }
}

For more insights into data science and related fields, you can explore this blog post to keep you engaged. Additionally, if you’re interested in workplace culture, the SHRM provides authoritative insights on how to enhance it. For a better understanding of how fulfillment centers train associates, refer to this excellent resource.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *