

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

# 設定 Lambda 函數逾時
<a name="configuration-timeout"></a>

Lambda 會在逾時之前執行程式碼一段設定的時間。*逾時*為 Lambda 函數可以執行的最長時間，以秒為單位。此設定的預設值為 3 秒，但您可以調整此值，以 1 秒為單位，最大值為 900 秒 (15 分鐘)。

此頁面說明如何及何時更新 Lambda 函數的逾時設定。

**Topics**
+ [

## 確定 Lambda 函數的適當逾時值
](#configuration-timeout-use-cases)
+ [

## 設定逾時 (主控台)
](#configuration-timeout-console)
+ [

## 設定逾時 (AWS CLI)
](#configuration-timeout-cli)
+ [

## 設定逾時 (AWS SAM)
](#configuration-timeout-sam)

## 確定 Lambda 函數的適當逾時值
<a name="configuration-timeout-use-cases"></a>

如果逾時值接近函數的平均持續時間，則函數意外逾時的風險更高。函數的持續時間會有所不同，這取決於資料傳輸和處理量，以及函數與之互動的任何服務的延遲。逾時的一些常見原因包括：
+ Amazon Simple Storage Service (Amazon S3) 中的下載量比平均水平更大或耗時更長。
+ 函數會向另一個服務提出請求，這需要更長的時間才能回應。
+ 提供給函數的參數要求函數具有更高的運算複雜性，這會導致調用時間更長。

測試應用程式時，請確保測試可準確反映資料的大小和數量以及真實的參數值。為了方便起見，測試通常會使用小型範例，但您應該使用工作負載合理預期上限的資料集。

## 設定逾時 (主控台)
<a name="configuration-timeout-console"></a>

可以在 Lambda 主控台中設定函數逾時。

**若要修改函數的逾時**

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

1. 選擇一個函數。

1. 選擇**組態**索引標簽，然後選擇**一般組態**。  
![\[\]](http://docs.aws.amazon.com/zh_tw/lambda/latest/dg/images/configuration-tab.png)

1. 在**一般組態**中，選擇**編輯**。

1. 對於**逾時**，設定介於 1 到 900 秒 (15 分鐘) 之間的值。

1. 選擇**儲存**。

## 設定逾時 (AWS CLI)
<a name="configuration-timeout-cli"></a>

可以使用 [update-function-configuration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/update-function-configuration.html) 命令，以秒為單位設定逾時值。下列範例命令會將函數逾時增加到 120 秒 (2 分鐘)。

**Example**  

```
aws lambda update-function-configuration \
  --function-name my-function \
  --timeout 120
```

## 設定逾時 (AWS SAM)
<a name="configuration-timeout-sam"></a>

可以使用 [AWS Serverless Application Model](https://docs.aws.amazon.com//serverless-application-model/latest/developerguide/serverless-getting-started.html ) 來設定函數的逾時值。更新 `template.yaml` 檔案中的 [Timeout](https://docs.aws.amazon.com//serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-timeout) 屬性，然後執行 [sam 部署](https://docs.aws.amazon.com//serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html)。

**Example template.yaml**  

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Application Model template describing your function.
Resources:
  my-function:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Description: ''
      MemorySize: 128
      Timeout: 120
      # Other function properties...
```