Skip to main content

AWS SAM

The AWS Serverless Application Model (SAM) is a framework for building serverless applications. AWS SAM is split into two parts, a shorthand syntax that adds additional resources on top of CloudFormation and a command line interface (CLI).

AWS SAM Template

Globals

The global sections of the SAM template allows default properties to be set across all Lambda functions. You can also override each of these properties at a specific function level.

template.yml

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
MemorySize: 1024
Architectures: [x86_64]
Runtime: dotnet6
Timeout: 30
Tracing: Active
Environment:
Variables:
PRODUCT_TABLE_NAME: !Ref Table
Resources:
GetProductsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./GetProducts/
Handler: GetProducts::GetProducts.Function::FunctionHandler
Events:
Api:
Type: HttpApi
Properties:
Path: /
Method: GET
Policies:
- DynamoDBReadPolicy:
TableName: !Ref DynamoDbTable
DynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH

Native AOT support

AWS SAM also provides support for compiling .NET 7 applications with native AOT. To do that, add an additional metadata property to the Lambda function definition and ensure the Runtime is set to provided.al2. Adding this additional metadata property tells SAM to use the Amazon.Lambda.Tools global CLI. For more information on native AOT, check out this walkthrough.

template.yml

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
MemorySize: 1024
Architectures: [x86_64]
Runtime: provided.al2
Timeout: 30
Tracing: Active
Environment:
Variables:
PRODUCT_TABLE_NAME: !Ref Table
Resources:
GetProductsFunction:
Type: AWS::Serverless::Function
Metadata:
BuildMethod: dotnet7
Properties:
CodeUri: ./GetProducts/
Handler: GetProducts::GetProducts.Function::FunctionHandler
Events:
Api:
Type: HttpApi
Properties:
Path: /
Method: GET
Policies:
- DynamoDBReadPolicy:
TableName: !Ref DynamoDbTable
DynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH

Function

Lambda functions are specified using a resource of type AWS::Serverless::Function. There are 8 supported resources that can be found in the AWS Docs. The CodeUri property tells the SAM CLI which folder the code for this function is stored under.

template.yml

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
MemorySize: 1024
Architectures: [x86_64]
Runtime: dotnet6
Timeout: 30
Tracing: Active
Environment:
Variables:
PRODUCT_TABLE_NAME: !Ref Table
Resources:
GetProductsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./GetProducts/
Handler: GetProducts::GetProducts.Function::FunctionHandler
Events:
Api:
Type: HttpApi
Properties:
Path: /
Method: GET
Policies:
- DynamoDBReadPolicy:
TableName: !Ref DynamoDbTable
DynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH

IAM permissions

SAM provides a set of pre-built policy templates to simplify IAM policies.

template.yml

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
MemorySize: 1024
Architectures: [x86_64]
Runtime: dotnet6
Timeout: 30
Tracing: Active
Environment:
Variables:
PRODUCT_TABLE_NAME: !Ref Table
Resources:
GetProductsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./GetProducts/
Handler: GetProducts::GetProducts.Function::FunctionHandler
Events:
Api:
Type: HttpApi
Properties:
Path: /
Method: GET
Policies:
- DynamoDBReadPolicy:
TableName: !Ref DynamoDbTable
DynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH

Globals

The global sections of the SAM template allows default properties to be set across all Lambda functions. You can also override each of these properties at a specific function level.

Native AOT support

AWS SAM also provides support for compiling .NET 7 applications with native AOT. To do that, add an additional metadata property to the Lambda function definition and ensure the Runtime is set to provided.al2. Adding this additional metadata property tells SAM to use the Amazon.Lambda.Tools global CLI. For more information on native AOT, check out this walkthrough.

Function

Lambda functions are specified using a resource of type AWS::Serverless::Function. There are 8 supported resources that can be found in the AWS Docs. The CodeUri property tells the SAM CLI which folder the code for this function is stored under.

IAM permissions

SAM provides a set of pre-built policy templates to simplify IAM policies.

template.yml
ExpandClose

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
MemorySize: 1024
Architectures: [x86_64]
Runtime: dotnet6
Timeout: 30
Tracing: Active
Environment:
Variables:
PRODUCT_TABLE_NAME: !Ref Table
Resources:
GetProductsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./GetProducts/
Handler: GetProducts::GetProducts.Function::FunctionHandler
Events:
Api:
Type: HttpApi
Properties:
Path: /
Method: GET
Policies:
- DynamoDBReadPolicy:
TableName: !Ref DynamoDbTable
DynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH

SAM CLI

The SAM CLI is used to build and deploy your serverless application.

The sam build command both compiles the application code and applies generates a CloudFormation ready template. Under the hood, the build command runs a foreach over all of the specified AWS::Serverless::Function resources, and compiles your .NET code. The compiled code is then output to the .aws-sam folder.

After running sam build, run sma deploy --guided to deploy the application to AWS.

If video is more your thing, there is an entire playlist on my YouTube channel going from AWS SAM basics through to complex CICD.