

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

# 在主控台中，測試 Lambda 函數。
<a name="testing-functions"></a>

您可以藉由使用測試事件調用函數，在主控台中測試您的 Lambda 函數。一個*測試事件*是函數的 JSON 輸入。如果您的函數不需要輸入，則該事件可以是空白的文件 `({})`。

當您在主控台執行測試時，Lambda 會與測試事件同步調用函數。函數執行期會將 JSON 事件轉換為物件，並將其傳遞給程式碼的處理常式方法以進行處理。

**建立測試事件**  
您必須先建立私有或可共用的測試事件，才能在主控台中測試。

## 使用測試事件調用函數
<a name="invoke-with-event"></a>

**若要測試函數**

1. 開啟 Lambda 主控台中的[函數頁面](https://console.aws.amazon.com/lambda/home#/functions)。

1. 選擇要測試的函數名稱。

1. 選擇**測試**標籤。

1. 在**測試事件**下，選擇**建立新事件**或**編輯已儲存事件**，然後選擇您要使用的已儲存事件。

1. (選擇性) - 選擇事件 JSON 的**範本**。

1. 選擇**測試**。

1. 若要檢閱測試結果，在 **Execution result** (執行結果) 下，展開 **Details** (詳細資訊)。

若要在不儲存測試事件的情況下調用您的函數，選擇**測試**，然後再儲存。這會建立一個未儲存的測試事件，而 Lambda 僅會在工作階段期間保留該事件。

對於 Node.js、Python 和 Ruby 執行時期，您也可以在**程式碼**索引標籤上存取現有的已儲存和未儲存的測試事件。使用**測試事件**區段來建立、編輯和執行測試。

## 建立私有測試事件
<a name="creating-private-events"></a>

只有事件建立者才能使用私有測試事件，且其不需要額外的許可即可使用。每個函數您可以建立並儲存最多 10 個私有測試事件。

**若要建立私有測試事件**

1. 開啟 Lambda 主控台中的[函數頁面](https://console.aws.amazon.com/lambda/home#/functions)。

1. 選擇要測試的函數名稱。

1. 選擇**測試**標籤。

1. 在 **Test event** (測試事件) 下，執行以下動作：

   1. 選擇 **Template** (範本)。

   1. 輸入該測試的 **Name** (名稱)。

   1. 在文字輸入方塊中，輸入 JSON 測試事件。

   1. 在 **Event sharing settings** (事件共用設定) 下，選擇 **Private** (私有)。

1. 選擇**儲存變更**。

對於 Node.js、Python 和 Ruby 執行時期，您也可以在**程式碼**索引標籤上建立測試事件。使用**測試事件**區段來建立、編輯和執行測試。

## 建立可共用測試事件
<a name="creating-shareable-events"></a>

可共用的測試事件是您可以與相同 AWS 帳戶中的其他使用者共用的測試事件。您可以編輯其他使用者的可共用測試事件，並使用它們來調用您的函數。

Lambda 將可共用測試事件以結構描述的形式，儲存在名為 `lambda-testevent-schemas` 的 [Amazon EventBridge (CloudWatch Events) 結構描述登錄檔](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-schema-registry.html)中。由於 Lambda 利用此登錄檔來存放和呼叫您建立的可共用測試事件，我們建議您不要編輯此登錄檔或使用 `lambda-testevent-schemas` 名稱建立登錄檔。

若要查看、共用和編輯可共用測試事件，您必須具有以下所有[EventBridge (CloudWatch Events) 結構描述登錄檔 API 操作](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/operations.html)的許可：
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname.html#CreateRegistry](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname.html#CreateRegistry)
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname.html#CreateSchema](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname.html#CreateSchema)
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname.html#DeleteSchema](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname.html#DeleteSchema)
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname-version-schemaversion.html#DeleteSchemaVersion](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname-version-schemaversion.html#DeleteSchemaVersion)
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname.html#DescribeRegistry](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname.html#DescribeRegistry)
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname.html#DescribeSchema](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname.html#DescribeSchema)
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-discover.html#GetDiscoveredSchema](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-discover.html#GetDiscoveredSchema)
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname-versions.html#ListSchemaVersions](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname-versions.html#ListSchemaVersions)
+ [https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname.html#UpdateSchema](https://docs.aws.amazon.com/eventbridge/latest/schema-reference/v1-registries-name-registryname-schemas-name-schemaname.html#UpdateSchema)

請注意，儲存對可共用測試事件所做的編輯將覆蓋該事件。

如果您無法建立、編輯或查看可共用測試事件，請檢查您的帳戶是否具有執行這些操作所需要的許可。如果您擁有所需要的許可，但仍然無法存取可共用測試事件，請檢查任何可能會限制對 EventBridge (CloudWatch Events) 登錄檔的存取的[資源型政策](access-control-resource-based.md)。

**若要建立可共用測試事件**

1. 開啟 Lambda 主控台中的[函數頁面](https://console.aws.amazon.com/lambda/home#/functions)。

1. 選擇要測試的函數名稱。

1. 選擇**測試**標籤。

1. 在 **Test event** (測試事件) 下，執行以下動作：

   1. 選擇 **Template** (範本)。

   1. 輸入該測試的 **Name** (名稱)。

   1. 在文字輸入方塊中，輸入 JSON 測試事件。

   1. 在 **Event sharing settings** (事件共用設定) 下，選擇 **Shareable** (可共用)。

1. 選擇**儲存變更**。

**搭配 使用可共用的測試事件 AWS Serverless Application Model。**  
您可以使用 AWS SAM 叫用可共用的測試事件。請參閱《[AWS Serverless Application Model 開發人員指南](https://docs.aws.amazon.com//serverless-application-model/latest/developerguide/using-sam-cli-remote-test-event.html)》中的 [https://docs.aws.amazon.com//serverless-application-model/latest/developerguide/using-sam-cli-remote-test-event.html](https://docs.aws.amazon.com//serverless-application-model/latest/developerguide/using-sam-cli-remote-test-event.html)。

## 刪除可共用測試事件結構描述
<a name="deleting-test-schemas"></a>

當您刪除可共用測試事件時，Lambda 會將其從 `lambda-testevent-schemas` 登錄檔中移除。如果您是從登錄檔移除最後一個可共用測試事件，則 Lambda 會刪除登錄檔。

如果刪除函數，Lambda 不會刪除任何關聯的可共用測試事件結構描述。您必須從 [EventBridge (CloudWatch Events) 主控台](https://console.aws.amazon.com/events)手動清理這些資源。