

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# AWS SAM CLI
<a name="using-sam-cli"></a>

AWS Serverless Application Model 命令列界面 (AWS SAM CLI) 是一種命令列工具，用於本機開發和測試無伺服器應用程式。 AWS SAM CLI 可讓您在本機建置、轉換、部署、除錯、封裝、初始化和同步無伺服器應用程式，然後再部署到雲端。

AWS SAM CLI 適用於使用不同架構和基礎設施做為程式碼 (IaC) 工具定義的無伺服器應用程式，支援程度不盡相同：
+ **AWS SAM 範本** – 透過完整的功能集提供原生支援，包括本機測試、偵錯、封裝和部署功能。
+ **AWS CDK 應用程式** – 在您使用 cdk synth 命令將 AWS CDK 應用程式合成至 CloudFormation 範本後，支援 Lambda 函數的本機測試。
+ **CloudFormation 範本** – 提供直接相容性，因為 AWS SAM 延伸 CloudFormation支援標準 CloudFormation 範本中定義的無伺服器資源。
+ **Terraform 應用程式** – 為 Lambda 函數的建置和本機測試提供有限的支援。要求您產生代表 Terraform 定義 Lambda 函數的 AWS SAM 範本成品。

如需最全面的功能支援和簡化的開發人員體驗，建議使用原生 AWS SAM 範本。

**Topics**
+ [

## 如何 AWS SAM CLI記錄命令
](#using-sam-cli-documentation)
+ [

# 設定 AWS SAM CLI
](using-sam-cli-configure.md)
+ [

# AWS SAM CLI 核心命令
](using-sam-cli-corecommands.md)
+ [

# 使用 進行本機測試 AWS SAM CLI
](using-sam-cli-local-testing.md)

## 如何 AWS SAM CLI記錄命令
<a name="using-sam-cli-documentation"></a>

AWS SAM CLI 命令會以下列格式記錄：
+ **提示** – 預設會記錄Linux提示，並顯示為 (`$ `)。對於Windows特定命令，會使用 (`> `) 做為提示。鍵入命令時，請不要包含該提示。
+ **目錄** – 命令必須從特定的目錄執行時，該目錄名稱會顯示在提示符號的前方。
+ **使用者輸入** – 於命令列輸入的命令文字採用 **user input** 格式。
+ **可取代的文字** – 變數文字，例如檔案名稱和參數，會格式化為*可取代的文字*。在需要特定鍵盤輸入的多行命令或命令中，鍵盤輸入也可以顯示為可取代的文字。例如， *ENTER*。
+ **輸出** – 傳回做為命令回應的輸出格式為 `computer output`。

下列`sam deploy`命令和輸出為範例：

```
$ sam deploy --guided --template template.yaml

Configuring SAM deploy
======================

    Looking for config file [samconfig.toml] :  Found
    Reading default arguments  :  Success

    Setting default arguments for 'sam deploy'
    =========================================
    Stack Name [sam-app]: ENTER
    AWS Region [us-west-2]: ENTER
    #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
    Confirm changes before deploy [y/N]: ENTER
    #SAM needs permission to be able to create roles to connect to the resources in your template
    Allow SAM CLI IAM role creation [Y/n]: ENTER
    #Preserves the state of previously provisioned resources when an operation fails
    Disable rollback [y/N]: ENTER
    HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
    Save arguments to configuration file [Y/n]: ENTER
    SAM configuration file [samconfig.toml]: ENTER
    SAM configuration environment [default]: ENTER
```

1. `sam deploy --guided --template template.yaml` 是您在命令列輸入的命令。

1. **sam deploy --guided --template** 應照原樣提供。

1. *template.yaml* 可以取代為您的特定檔案名稱。

1. 輸出從 開始`Configuring SAM deploy`。

1. 在輸出中， *ENTER* 和 *y* 表示您提供的可取代值。

# 設定 AWS SAM CLI
<a name="using-sam-cli-configure"></a>

的好處之一 AWS SAM 是，它透過移除重複性任務來最佳化開發人員的時間。 AWS SAM CLI 包含名為 `samconfig` 的組態檔案。根據預設， AWS SAM CLI不需要對 進行組態，但您可以更新組態檔案，以允許 參考組態檔案中的自訂參數 AWS SAM ，以使用較少的參數執行命令。下表中的範例示範如何最佳化命令：


| 原始的 | 使用 最佳化 `samconfig` | 
| --- | --- | 
| **sam build --cached --parallel --use-containers** | **sam build** | 
| **sam local invoke --env-vars locals.json** | **sam local invoke** | 
| **sam local start-api --env-vars locals.json --warm-containers EAGER** | **sam local start-api** | 

 AWS SAM CLI 提供一組命令，協助開發人員建立、開發和部署無伺服器應用程式。這些命令都可以根據應用程式和開發人員的偏好設定，使用選用的旗標來設定。如需詳細資訊，請參閱 [AWS SAM CLI GitHub 中的內容](https://github.com/aws/aws-sam-cli)

本節中的主題說明如何建立 [AWS SAM CLI 組態檔案](serverless-sam-cli-config.md)並自訂其預設設定，以最佳化無伺服器應用程式的開發時間。

**Topics**
+ [

## 如何建立您的組態檔案 ( `samconfig` 檔案）
](#using-sam-cli-configure-create)
+ [

## 設定專案設定
](#using-sam-cli-configure-project)
+ [

## 設定登入資料和基本設定
](#using-sam-cli-configure-basic)

## 如何建立您的組態檔案 ( `samconfig` 檔案）
<a name="using-sam-cli-configure-create"></a>

組態檔案 AWS SAM CLI（檔案名稱 `samconfig`) 是文字檔案，通常使用 TOML 結構，但也可以在 YAML 中。使用 AWS Quick Start 範本時，會在您執行 **sam init**命令時建立此檔案。您可以在使用 **sam deploy -\$1-guided**命令部署應用程式時更新此檔案。

部署完成後，`default`如果您使用預設值， `samconfig` 檔案會包含名為 的設定檔。當您重新執行**deploy**命令時， 會從此設定檔 AWS SAM 套用儲存的組態設定。

`samconfig` 檔案的優點是，除了部署命令之外， AWS SAM 還存放任何其他可用命令的組態設定。除了在新部署時建立的這些值之外，您還可以在 `samconfig` 檔案中設定許多屬性，以使用 簡化開發人員工作流程的其他層面 AWS SAM CLI。

## 設定專案設定
<a name="using-sam-cli-configure-project"></a>

您可以在組態檔案中指定專案特定的設定，例如 AWS SAM CLI命令參數值，以搭配 使用 AWS SAM CLI。如需此組態檔案的詳細資訊，請參閱 [AWS SAM CLI 組態檔案](serverless-sam-cli-config.md)。

### 使用組態檔案
<a name="using-sam-cli-configure-project-using"></a>

組態檔案由環境、命令和參數值構成。如需詳細資訊，請參閱[組態檔案基本概念](serverless-sam-cli-config.md#serverless-sam-cli-config-basics)。

**設定新的環境**

1. 在組態檔案中指定您的新環境。

   以下是指定新`prod`環境的範例：

------
#### [ TOML ]

   ```
   [prod.global.parameters]
   ```

------
#### [ YAML ]

   ```
   prod:
     global:
       parameters:
   ```

------

1. 在組態檔案的參數區段中，將參數值指定為鍵值對。

   以下是為`prod`環境指定應用程式堆疊名稱的範例。

------
#### [ TOML ]

   ```
   [prod.global.parameters]
   stack_name = "prod-app"
   ```

------
#### [ YAML ]

   ```
   prod:
     global:
       parameters:
         stack_name: prod-app
   ```

------

1. 使用 `--config-env`選項來指定要使用的環境。

   以下是範例：

   ```
   $ sam deploy --config-env "prod"
   ```

**設定參數值**

1. 指定 AWS SAM CLI您要為其設定參數值的命令。若要設定所有 AWS SAM CLI命令的參數值，請使用 `global`識別符。

   以下是指定`default`環境`sam deploy`命令參數值的範例：

------
#### [ TOML ]

   ```
   [default.deploy.parameters]
   confirm_changeset = true
   ```

------
#### [ YAML ]

   ```
   default:
     deploy:
       parameters:
         confirm_changeset: true
   ```

------

   以下是為`default`環境中所有 AWS SAM CLI命令指定參數值的範例：

------
#### [ TOML ]

   ```
   [default.global.parameters]
   stack_name = "sam-app"
   ```

------
#### [ YAML ]

   ```
   default:
     global:
       parameters:
         stack_name: sam-app
   ```

------

1. 您也可以指定參數值，並透過互動式流程修改您的組態檔案 AWS SAM CLI。

   以下是`sam deploy --guided`互動式流程的範例：

   ```
   $ sam deploy --guided
   
   Configuring SAM deploy
   ======================
   
       Looking for config file [samconfig.toml] :  Found
       Reading default arguments  :  Success
   
       Setting default arguments for 'sam deploy'
       =========================================
       Stack Name [sam-app]: ENTER
       AWS Region [us-west-2]: ENTER
       #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
       Confirm changes before deploy [Y/n]: n
       #SAM needs permission to be able to create roles to connect to the resources in your template
       Allow SAM CLI IAM role creation [Y/n]: ENTER
       #Preserves the state of previously provisioned resources when an operation fails
       Disable rollback [y/N]: ENTER
       HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
       Save arguments to configuration file [Y/n]: ENTER
       SAM configuration file [samconfig.toml]: ENTER
       SAM configuration environment [default]: ENTER
   ```

如需詳細資訊，請參閱[建立和修改組態檔案](serverless-sam-cli-config.md#serverless-sam-cli-config-using)。

### 範例
<a name="using-sam-cli-configure-project-examples"></a>

#### 基本TOML範例
<a name="using-sam-cli-configure-project-examples-toml"></a>

以下是`samconfig.toml`組態檔案的範例：

```
...
version = 0.1

[default]
[default.global]
[default.global.parameters]
stack_name = "sam-app"

[default.build.parameters]
cached = true
parallel = true

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true

[default.sync.parameters]
watch = true

[default.local_start_api.parameters]
warm_containers = "EAGER"

[prod]
[prod.sync]
[prod.sync.parameters]
watch = false
```

#### 基本YAML範例
<a name="using-sam-cli-configure-project-examples-yaml"></a>

以下是`samconfig.yaml`組態檔案的範例：

```
version 0.1
default:
  global:
    parameters:
      stack_name: sam-app
  build:
    parameters:
      cached: true
      parallel: true
  deploy:
    parameters:
      capabilities: CAPABILITY_IAM
      confirm_changeset: true
      resolve_s3: true
  sync:
    parameters:
      watch: true
  local_start_api:
    parameters:
      warm_containers: EAGER
prod:
  sync:
    parameters:
      watch: false
```

## 設定登入資料和基本設定
<a name="using-sam-cli-configure-basic"></a>

使用 AWS Command Line Interface (AWS CLI) 來設定基本設定，例如 AWS 登入資料、預設區域名稱和預設輸出格式。設定完成後，您可以將這些設定與 搭配使用 AWS SAM CLI。若要進一步了解，請參閱*AWS Command Line Interface 《 使用者指南*》中的下列內容：
+ [組態基本概念](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html)
+ [組態和登入資料檔案設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)
+ [的具名設定檔 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html)
+ [使用已啟用 IAM Identity Center 的具名設定檔](https://docs.aws.amazon.com/cli/latest/userguide/sso-using-profile.html)

如需快速設定說明，請參閱 [步驟 5：使用 AWS CLI 設定 AWS 登入資料](prerequisites.md#prerequisites-configure-credentials)。

# AWS SAM CLI 核心命令
<a name="using-sam-cli-corecommands"></a>

AWS SAM CLI 有一些基本命令，您可用來建立、建置、測試、部署和同步無伺服器應用程式。下表列出這些命令，並提供每個命令的詳細資訊連結。

如需命令的完整清單 AWS SAM CLI，請參閱 [AWS SAM CLI 命令參考](serverless-sam-cli-command-reference.md)。


| 命令 | 它的功能 | 相關主題 | 
| --- | --- | --- | 
| **sam build** | 為開發人員工作流程中的後續步驟準備應用程式，例如本機測試或部署至 AWS 雲端。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/serverless-application-model/latest/developerguide/using-sam-cli-corecommands.html)  | 
| **sam deploy** | 使用 將應用程式部署至 AWS 雲端 CloudFormation。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/serverless-application-model/latest/developerguide/using-sam-cli-corecommands.html)  | 
| **sam init** | 提供初始化和建立新的無伺服器應用程式的選項。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/serverless-application-model/latest/developerguide/using-sam-cli-corecommands.html)  | 
| **sam local** | 提供子命令以在本機測試無伺服器應用程式。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/serverless-application-model/latest/developerguide/using-sam-cli-corecommands.html)  | 
| **sam remote invoke** | 提供與 AWS 雲端中支援 AWS 的資源互動的方法。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/serverless-application-model/latest/developerguide/using-sam-cli-corecommands.html)  | 
| **sam remote test-event** | 提供存取和管理 AWS Lambda 函數可共用測試事件的方法。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/serverless-application-model/latest/developerguide/using-sam-cli-corecommands.html)  | 
| **sam sync** | 提供快速將本機應用程式變更同步至 AWS 雲端的選項。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/serverless-application-model/latest/developerguide/using-sam-cli-corecommands.html)  | 

# 使用 進行本機測試 AWS SAM CLI
<a name="using-sam-cli-local-testing"></a>

可讓您在本機測試不同基礎設施的無伺服器應用程式，即程式碼 AWS SAM CLI(IaC) 工具。本指南說明如何使用 AWS SAM CLI搭配各種 IaC 架構進行本機測試。

使用 AWS SAM CLI 進行本機測試的主要優點包括：
+ **快速開發** - 在不部署至 的情況下測試程式碼變更 AWS
+ **成本效益** - 開發和測試而不會產生 AWS 費用
+ **離線功能** - 在沒有網際網路連線的情況下處理應用程式
+ **簡化偵錯** - 使用偵錯工具在本機逐步完成 Lambda 函數程式碼
+ **實際測試** - 使用 AWS 服務的本機模擬測試您的應用程式

## 在本機測試 AWS SAM 應用程式
<a name="using-sam-cli-local-testing-sam"></a>

如需有關測試使用 AWS SAM 範本定義的應用程式的資訊，請參閱本指南中的[測試和偵錯無伺服器應用程式](serverless-test-and-debug.md)。

## 在本機測試 CloudFormation 範本
<a name="using-sam-cli-local-testing-cfn"></a>

若要搭配 使用 AWS SAM CLI的本機測試功能 CloudFormation，請將 AWS SAM 轉換新增至您的 CloudFormation 範本。如需詳細資訊，請參閱 [AWS SAM 範本結構](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy.html)：
+ 將 AWS SAM 轉換新增至 CloudFormation 範本
+ 了解範本相容性
+ 探索無伺服器資源語法

## 在本機測試 AWS CDK 應用程式
<a name="using-sam-cli-local-testing-cdk"></a>

您可以在使用 AWS cdk synth 命令將 CDK 應用程式合成至 CloudFormation 範本之後，使用 AWS SAM CLI 在本機測試 AWS CDK 應用程式。

如需詳細資訊，請參閱《[AWS CDK 開發人員指南》中的使用 本機測試和建置 CDK 應用程式 AWS SAM CLI](https://docs.aws.amazon.com/cdk/v2/guide/testing-locally.html)。 AWS 其中包含：
+ [本機測試入門](https://docs.aws.amazon.com/cdk/v2/guide/testing-locally.html#testing-locally-getting-started)
+ [使用 本機測試 AWS CDK 應用程式 AWS SAM](https://docs.aws.amazon.com/cdk/v2/guide/testing-locally.html#testing-locally-sam)

## 在本機測試 Terraform 應用程式
<a name="using-sam-cli-local-testing-terraform"></a>

支援 Terraform AWS SAM CLI專案和 Terraform Cloud。您可以使用它來執行本機偵錯和測試：Lambda 函數和 layer，以及 Amazon API Gateway HTTP 和 REST APIs。

若要設定您的環境並了解所有可用的功能，請參閱本指南中的 [Terraform Support](terraform-support.md)。其中包含：
+ [的 Terraform 支援入門 AWS SAM CLI](gs-terraform-support.md)
+ [使用 AWS SAM CLI搭配 Terraform 進行本機偵錯和測試](using-samcli-terraform.md)