Lab 3: .NET on Serverless

Author: Kirk Davis, Dean Suzuki (4/22/20)

Background

In this lab, you will create a simple ASP.NET Core Razor Pages web application and deploy it to AWS Lambda (the AWS serverless platform) using the AWS Toolkit for Visual Studio. The AWS Toolkit leverages the AWS Serverless Application Model (AWS SAM) to create the lambda function and an Amazon API Gateway API as a proxy layer in front of the web app, plus required IAM permissions.

Step 1: Create an AWS Serverless Web Project

Follow the steps below to create and customize an ASP.NET Web Project in Visual Studio using the AWS Serverless Application template.

Note: the steps below are for Visual Studio 2019. Perform the equivalent steps in your version of Visual Studio if using Visual Studio 2017.

  1. In Visual Studio, open the New Project dialog.

  2. Select the AWS Serverless Application (.Net Core - C#) template. To make it easier to find, filter the list to C# for the language, All platforms and Cloud to help find the correct project type. See screenshot below. then click Next.

    Note: be sure not to click the F# version of the template, unless you desire to do this lab in F#.

    New project dialog

  3. Enter a name and location for your project, or accept the defaults, then click Create.

  4. In the next dialog (‘Select Blueprint’), select the ASP.NET Core Web App blueprint, then click Finish to generate the project. Blueprint selector dialog

Step 2: Deploy to AWS Lambda

Follow the steps below to deploy the ASP.NET Core Web application to AWS Lambda

  1. Right-click your project in Solution Explorer, and select Publish to AWS Lambda to launch the publishing wizard. See the figure below. Context menu publish item

  2. Ensure the Account profile to use drop-down and Region drop-down are set to the profile (e.g. default) and region (US East (N. Virigina)) you are using for today’s labs.

    a. For the CloudFormation Stack type Serverless-<your-first-name>

    b. Click on the New button for creating a new S3 bucket and name give it a name.

    Note: S3 bucket names must be globally unique; try using <your-full-name>-bucket (all lowercase only!)

    c. Click Publish and wait for the process to finish.

  3. When the STATUS changes to CREATE_COMPLETE, press Copy the AWS Serverless URL as you can see in the picture below. Visual Studio Toolkit Deploy Pane

  4. Paste the URL copied on the previous step on a browser and you should see the following result: webpage screenshot

Step 3: Check the AWS resources created

For this lab, we’ll access the AWS Management Console to check the resources published by the Visual Studio using the CloudFormation SAM template.

  1. In the AWS Management Console, navigate to the CloudFormation console.

  2. Select the stack with the name you chose when initially deploying, and select the tab Resources to verify what resources were created by clicking on the links at the Physical ID. You will be redirected to the Lambda Function, IAM Role and API Gateway resources that were created by the CloudFormation Stack launched by the visual studio, like the example below: CloudFormation resources view

Step 4: Change a Razor page and Re-deploy the lambda function

With your ASP.NET Core Web deployed on lambda, you can start customizing it using .Net Razor pages.

For this lab, we’ll change the index.cshtml and index.cshtml.cs to render information on the landing page. To edit these files, go to the Solution Explorer on the right and expand Pages.

  1. Add the code below to the index.cshtml file, right after the <h2>Congratulations!!! Your ASP.NET Core Web Application is now Serverless</h2>

    <h2> @Model.Message </h2>
    
  2. Replace the IndexModel class at the index.cshtml.cs file with the following code:

    public class IndexModel : PageModel
    {
        public string Message { get; private set; } = "PageModel in C#";
    
        public void OnGet()
        {
            Message += $" Server time is { DateTime.Now }";
        }
    }
    
  3. Save the two updated files.

  4. Re-deploy the application following the steps of the Step 2 – Deploy to AWS Lambda. Wait until the STATUS changes to UPDATE_COMPLETE, and refresh or open the Wep App URL (The URL can be found at the AWS Serverless URL on the form).

Congratulations

You have created and deployed a serverless ASP.NET Web application using Lambda.

Step 5: Remove all the Resources deployed

Finally, lets remove all the resources created by the CloudFormation stack launched by Visual Studio.

  1. In the AWS Management console, navigate to CloudFormation console.

  2. On the CloudFormation console, click on the Stack Name for your serverless ASP.NET Core Razor Pages application.

  3. Click on the Actions DropDownButton and select Delete Stack. Confirm that you want to delete it on the next dialog.