Tutorial: Build and Test a Serverless Application with Amazon Lambda - Amazon Toolkit with Amazon Q
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Tutorial: Build and Test a Serverless Application with Amazon Lambda

You can build a serverless Lambda application by using an Amazon Toolkit for Visual Studio template. The Lambda project templates include one for an Amazon Serverless Application, which is the Amazon Toolkit for Visual Studio implementation of the Amazon Serverless Application Model (Amazon SAM). Using this project type you can develop a collection of Amazon Lambda functions and deploy them with any necessary Amazon resources as a whole application, using Amazon CloudFormation to orchestrate the deployment.

For prerequisites and information about setting up the Amazon Toolkit for Visual Studio, see Using the Amazon Lambda Templates in the Amazon Toolkit for Visual Studio.

Create a New Amazon Serverless Application Project

Amazon Serverless Application projects create Lambda functions with a serverless Amazon CloudFormation template. Amazon CloudFormation templates enable you to define additional resources such as databases, add IAM roles, and deploy multiple functions at one time. This differs from Amazon Lambda projects, which focus on developing and deploying a single Lambda function.

The following procedure describes how to create a new Amazon Serverless Application Project.

  1. From Visual Studio expand the File menu, expand New, then choose Project.

  2. In the New Project dialog box, ensure that the Language, Platform, and Project type drop-down boxes are set to "All ..." and enter aws lambda in the Search field.

  3. Select the Amazon Serverless Application with Tests (.NET Core - C#) template.


    It's possible that the Amazon Serverless Application with Tests (.NET Core - C#) template may not populate at the top of the results.

  4. Click Next to open the Configure your new project dialog.

  5. From the Configure your new project dialog, enter ServerlessPowertools for the Name, then complete the remaining fields to your preference. Choose the Create button to proceed to the Select Blueprint dialog.

  6. From the Select Blueprint dialog choose the Powertools for Amazon Lambda blueprint, and then choose Finish to create the Visual Studio project.

Reviewing the Serverless Application files

The following sections provide a detailed look at three Serverless Application files created for your project:

  1. serverless.template

  2. Functions.cs

  3. aws-lambda-tools-defaults.json

1. serverless.template

A serverless.template file is an Amazon CloudFormation template for declaring your Serverless functions and other Amazon resources. The file included with this project contains a declaration for a single Lambda function that will be exposed through the Amazon API Gateway as an HTTP *Get* operation. You can edit this template to customize the existing function or add more functions and other resources that are required by your application.

The following is an example of a serverless.template file:

{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Description": "An AWS Serverless Application.", "Resources": { "Get": { "Type": "AWS::Serverless::Function", "Properties": { "Architectures": [ "x86_64" ], "Handler": "ServerlessPowertools::ServerlessPowertools.Functions::Get", "Runtime": "dotnet8", "CodeUri": "", "MemorySize": 512, "Timeout": 30, "Role": null, "Policies": [ "AWSLambdaBasicExecutionRole" ], "Environment": { "Variables": { "POWERTOOLS_SERVICE_NAME": "ServerlessGreeting", "POWERTOOLS_LOG_LEVEL": "Info", "POWERTOOLS_LOGGER_CASE": "PascalCase", "POWERTOOLS_TRACER_CAPTURE_RESPONSE": true, "POWERTOOLS_TRACER_CAPTURE_ERROR": true, "POWERTOOLS_METRICS_NAMESPACE": "ServerlessGreeting" } }, "Events": { "RootGet": { "Type": "Api", "Properties": { "Path": "/", "Method": "GET" } } } } } }, "Outputs": { "ApiURL": { "Description": "API endpoint URL for Prod environment", "Value": { "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/" } } } }

Notice that many of the ...AWS:: Serverless::Function... declaration fields are similar to the fields of a Lambda project deployment. Powertools Logging, Metrics and Tracing are configured through the following environment variables:

  • POWERTOOLS_SERVICE_NAME=ServerlessGreeting






For definitions and additional details about the environment variables, see the Powertools for Amazon Lambda references website.

2. Functions.cs

Functions.cs is a class file containing a C# method that's mapped to a single function declared in the template file. The Lambda function responds to HTTP Get methods from API Gateway. The following is an example of the Functions.cs file:

public class Functions { [Logging(LogEvent = true, CorrelationIdPath = CorrelationIdPaths.ApiGatewayRest)] [Metrics(CaptureColdStart = true)] [Tracing(CaptureMode = TracingCaptureMode.ResponseAndError)] public APIGatewayProxyResponse Get(APIGatewayProxyRequest request, ILambdaContext context) { Logger.LogInformation("Get Request"); var greeting = GetGreeting(); var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Body = greeting, Headers = new Dictionary (string, string) { { "Content-Type", "text/plain" } } }; return response; } [Tracing(SegmentName = "GetGreeting Method")] private static string GetGreeting() { Metrics.AddMetric("GetGreeting_Invocations", 1, MetricUnit.Count); return "Hello Powertools for AWS Lambda (.NET)"; } }

3. aws-lambda-tools-defaults.json

aws-lambda-tools-defaults.json provides the default values for the Amazon deployment wizard inside Visual Studio and the Amazon Lambda commands added to the .NET Core CLI. The following is an example of the aws-lambda-tools-defaults.json file included with this project:

{ "profile": "Default", "region": "us-east-1", "configuration": "Release", "s3-prefix": "ServerlessPowertools/", "template": "serverless.template", "template-parameters": "" }

Deploying the Serverless Application

To deploy your serverless application complete the following steps

  1. From the Solution Explorer, open the context menu for (right click) your project and choose Publish to Amazon Lambda to open the Publish Amazon Serverless Application dialog.

  2. From the Publish Amazon Serverless Application dialog, enter a name for the Amazon CloudFormation stack container in the Stack Name field.

  3. In the S3 Bucket field, choose an Amazon S3 bucket that your application bundle will upload to or choose the New... button and enter the name of a new Amazon S3 bucket. Then choose Publish to publish to deploy your application.


    Your Amazon CloudFormation stack and Amazon S3 Bucket must exist in the same Amazon region. The remaining settings for your project are defined in the serverless.template file.

    Image of the Publish Amazon Serverless Application dialog.
  4. The Stack view window opens during the publishing process, when deployment is complete the Status field displays: CREATE_COMPLETE.

    Image of the deployment stack view window in visual studio.

Test the Serverless Application

When the stack creation is complete, you can view your application using the Amazon Serverless URL. If you've completed this tutorial without adding any additional functions or parameters, accessing your Amazon serverless URL displays the following phrase in your web browser: Hello Powertools for AWS Lambda (.NET).