> For the complete documentation index, see [llms.txt](https://mamawhocode.gitbook.io/aws/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mamawhocode.gitbook.io/aws/services/compute/sam.md).

# SAM

Serverless Application Model: extension of CloudFormation

[Docs](https://docs.aws.amazon.com/serverless-application-model/) |

## Overview

* A tool kit for building and running serverless applications, contains 2 parts:
  * [SAM template](#sam-template)
  * [SAM CLI](#sam-cli)
* Can use for deploy serverless application: run locally, package, or deploy to the cloud.
* Benefits:
  * Construct serverless applications in fewer lines of code.
  * The development time is significantly reduced.

## Features

### SAM template

* SAM will translate this CloudFormation syntax to CloudFormation stack that create Lambda and API Gateway, Dynamo table.
* SAM template has `Transform: ‘AWS::Serverless-2016–10–31’` line in the template.

<figure><img src="https://2259236002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fuh9xZDZ53qGqmMCM44PU%2Fuploads%2FMxZ3obum2XzADQknaBix%2Fimage.png?alt=media&amp;token=4d708616-9428-4cb3-8400-17d822cfdedb" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="success" %}
SAM resources can be written alongside the native CloudFormation resources. You can also use intrinsic functions and pseudo parameters in a SAM template just like you would in a native CloudFormation template.
{% endhint %}

### SAM CLI

#### Initialization

* `sam init`: initialize the project

#### Build & Deploy

* `sam package`: packages your AWS SAM application source code, dependencies and resources into a deployment package (.zip file). The .zip file is then uploaded to the S3 bucket that you specify. After that, it returns a copy of your AWS SAM template, ***replacing references*** to local artifacts with the Amazon S3 location where the artifacts are uploaded.
* `sam build`: build app to `.aws-sam` directory.
* `sam deploy`: will call `sam package` ***internally***, so don't need to call it seperately.

{% hint style="info" %}
Here, we use the *<mark style="color:red;">**sam deploy --guided**</mark>* command to deploy our application through an interactive flow. The AWS SAM CLI guides us through configuring our application's deployment settings, transforms our template into AWS CloudFormation, and deploys to AWS CloudFormation to create our resources.
{% endhint %}

* `sam pipeline init --bootstrap`: to configure a CI/CD deployment pipeline for our application.
* `cdk synth`: it reads your CDK app code and generates an CloudFormation template.

{% hint style="info" %}
SAM is built-in with CodeDeploy to help ensure safe Lambda deployments.
{% endhint %}

#### Testing

* `sam local invoke`: perform *<mark style="color:red;background-color:green;">**local**</mark>* <mark style="color:red;">debugging</mark> and test.
* `sam local start-api`: starts a local instance of API Gateway to test HTTP endpoints.
* `sam remote invoke`: to test a *<mark style="background-color:yellow;">**deployed**</mark>* <mark style="background-color:yellow;"></mark><mark style="background-color:yellow;">Lambda function</mark> in the cloud.

#### Monitor & troubleshooting

* `sam list`: to view our deployed resources.
* `sam logs`: to view our functions' logs.

#### Sync to cloud

* `sam sync --watch`: to have the AWS SAM CLI watch for local changes. This command is typically used for quick syncing of local changes to AWS and is more suitable for rapid *<mark style="color:red;">**development testing**</mark>*.

## Trivia

* SAM template has `Transform: ‘AWS::Serverless-2016–10–31’` line in the template.
