Using the Amazon Serverless Application Model (Amazon SAM) - Amazon Lambda
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).

Using the Amazon Serverless Application Model (Amazon SAM)

The Amazon Serverless Application Model (Amazon SAM) is a toolkit that helps streamline the process of building and running serverless applications on Amazon. You define the resources for your application in a YAML or JSON template and use the Amazon SAM command line interface (Amazon SAM CLI) to build, package, and deploy your applications. When you build a Lambda function from an Amazon SAM template, Amazon SAM automatically creates a .zip deployment package or container image with your function code and any dependencies you specify. Amazon SAM then deploys your function using an Amazon CloudFormation stack. To learn more about using Amazon SAM to build and deploy Lambda functions, see Getting started with Amazon SAM in the Amazon Serverless Application Model Developer Guide.

The following steps show you how to download, build, and deploy a sample .NET Hello World application using Amazon SAM. This sample application uses a Lambda function and an Amazon API Gateway endpoint to implement a basic API backend. When you send an HTTP GET request to your API Gateway endpoint, API Gateway invokes your Lambda function. The function returns a "hello world" message, along with the IP address of the Lambda function instance that processes your request.

When you build and deploy your application using Amazon SAM, behind the scenes the Amazon SAM CLI uses the dotnet lambda package command to package the individual Lambda function code bundles.

Prerequisites

.NET 8 SDK

Install the .NET 8 SDK and Runtime.

Amazon SAM CLI version 1.39 or later

To learn how to install the latest version of the Amazon SAM CLI, see Installing the Amazon SAM CLI.

Deploy a sample Amazon SAM application

  1. Initialize the application using the Hello world .NET template using the following command.

    sam init --app-template hello-world --name sam-app \ --package-type Zip --runtime dotnet8

    This command creates the following files and directories in your project directory.

    └── sam-app ├── README.md ├── events │   └── event.json ├── omnisharp.json ├── samconfig.toml ├── src │   └── HelloWorld │   ├── Function.cs │   ├── HelloWorld.csproj │   └── aws-lambda-tools-defaults.json ├── template.yaml └── test └── HelloWorld.Test ├── FunctionTest.cs └── HelloWorld.Tests.csproj
  2. Navigate into the directory containing the template.yaml file. This file is a tempate that defines the Amazon resources for your application, including your Lambda function and an API Gateway API.

    cd sam-app
  3. To build the source of your application, run the following command.

    sam build
  4. To deploy your application to Amazon, run the following command.

    sam deploy --guided

    This command packages and deploys your application with the following series of prompts. To accept the default options, press Enter.

    Note

    For HelloWorldFunction may not have authorization defined, is this okay?, be sure to enter y.

    • Stack Name: The name of the stack to deploy to Amazon CloudFormation. This name must be unique to your Amazon Web Services account and Amazon Web Services Region.

    • Amazon Web Services Region: The Amazon Web Services Region you want to deploy your app to.

    • Confirm changes before deploy: Select yes to manually review any change sets before Amazon SAM deploys application changes. If you select no, the Amazon SAM CLI automatically deploys application changes.

    • Allow SAM CLI IAM role creation: Many Amazon SAM templates, including the Hello world one in this example, create Amazon Identity and Access Management (IAM) roles to give your Lambda functions permission to access other Amazon Web Services. Select Yes to provide permission to deploy a Amazon CloudFormation stack that creates or modifies IAM roles.

    • Disable rollback: By default, if Amazon SAM encounters an error during creation or deployment of your stack, it rolls the stack back to the previous version. Select No to accept this default.

    • HelloWorldFunction may not have authorization defined, is this okay: Enter y.

    • Save arguments to samconfig.toml: Select yes to save your configuration choices. In the future, you can re-run sam deploy without parameters to deploy changes to your application.

  5. When the deployment of your application is complete, the CLI returns the Amazon Resource Name (ARN) of the Hello World Lambda function and the IAM role created for it. It also displays the endpoint of your API Gateway API. To test your application, open the endpoint in a browser. You should see a response similar to the following.

    {"message":"hello world","location":"34.244.135.203"}
  6. To delete your resources, run the following command. Note that the API endpoint you created is a public endpoint accessible over the internet. We recommend that you delete this endpoint after testing.

    sam delete

Next steps

To learn more about using Amazon SAM to build and deploy Lambda functions using .NET, see the following resources: