

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

# 將 Lambda 函數匯入為元件 (AWS CLI)
<a name="import-lambda-function-cli"></a>

使用 [CreateComponentVersion](https://docs.aws.amazon.com/greengrass/v2/APIReference/API_CreateComponentVersion.html) 操作從 Lambda 函數建立元件。當您呼叫此操作時，請指定 `lambdaFunction`以匯入 Lambda 函數。

**Topics**
+ [步驟 1：定義 Lambda 函數組態](#create-lambda-function-configuration-cli)
+ [步驟 2：建立 Lambda 函數元件](#create-lambda-component-cli)

## 步驟 1：定義 Lambda 函數組態
<a name="create-lambda-function-configuration-cli"></a>

1. 建立名為 的檔案`lambda-function-component.json`，然後將下列 JSON 物件複製到 檔案。將 取代`lambdaArn`為要匯入之 Lambda 函數的 ARN。

   ```
   {
     "lambdaFunction": {
       "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1"
     }
   }
   ```
**重要**  
您必須指定包含要匯入之函數版本的 ARN。您不能使用 `$LATEST` 之類的版本別名。

1. （選用） 指定元件的名稱 (`componentName`)。如果您省略此參數， 會使用 Lambda 函數的名稱 AWS IoT Greengrass 建立元件。

   ```
   {
     "lambdaFunction": {
       "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
       "componentName": "com.example.HelloWorldLambda"
     }
   }
   ```

1. （選用） 指定元件的版本 (`componentVersion`)。如果您省略此參數， 會使用 Lambda 函數的 版本 AWS IoT Greengrass 建立元件，做為有效的語意版本。例如，如果您的函數版本為 `3`，則元件版本會變成 `3.0.0`。
**注意**  
<a name="component-version-uniqueness-para"></a>您上傳的每個元件版本都必須是唯一的。請務必上傳正確的元件版本，因為您在上傳之後無法編輯。  
<a name="semver-para"></a>AWS IoT Greengrass 使用元件的語意版本。語意版本遵循 *major*.*minor*.*patch* 號碼系統。例如，版本`1.0.0`代表元件的第一個主要版本。如需詳細資訊，請參閱[語意版本規格](https://semver.org/)。

   ```
   {
     "lambdaFunction": {
       "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
       "componentName": "com.example.HelloWorldLambda",
       "componentVersion": "1.0.0"
     }
   }
   ```

1. （選用） 指定此 Lambda 函數支援的平台。每個平台都包含識別平台的屬性映射。所有核心裝置都有作業系統 (`os`) 和架構 () 的屬性`architecture`。Core AWS IoT Greengrass 軟體可能會新增其他平台屬性。您也可以在將 [Greengrass 核元件](greengrass-nucleus-component.md)部署至核心裝置時指定自訂平台屬性。請執行下列操作：

   1. 將平台清單 (`componentPlatforms`) 新增至 中的 Lambda 函數`lambda-function-component.json`。

      ```
      {
        "lambdaFunction": {
          "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
          "componentName": "com.example.HelloWorldLambda",
          "componentVersion": "1.0.0",
          "componentPlatforms": [
          
          ]
        }
      }
      ```

   1. 將每個支援的平台新增至清單。每個平台都有一個易於`name`識別的 和屬性映射。下列範例指定此函數支援執行 Linux 的 x86 裝置。

      ```
      {
        "name": "Linux x86",
        "attributes": {
          "os": "linux",
          "architecture": "x86"
        }
      }
      ```

      您的 `lambda-function-component.json`可能包含類似下列範例的文件。

      ```
      {
        "lambdaFunction": {
          "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
          "componentName": "com.example.HelloWorldLambda",
          "componentVersion": "1.0.0",
          "componentPlatforms": [
            {
              "name": "Linux x86",
              "attributes": {
                "os": "linux",
                "architecture": "x86"
              }
            }
          ]
        }
      }
      ```

1. （選用） 指定 Lambda 函數的元件相依性。當您部署 Lambda 函數元件時，部署會包含要讓函數執行的這些相依性。
**重要**  <a name="import-v1-lambda-note"></a>
若要匯入您建立以在 AWS IoT Greengrass V1 上執行的 Lambda 函數，您必須為函數使用的功能定義個別元件相依性，例如秘密、本機陰影和串流管理員。將這些元件定義為[硬相依性](component-recipe-reference.md)，以便在相依性變更狀態時重新啟動 Lambda 函數元件。如需詳細資訊，請參閱[匯入 V1 Lambda 函數](set-up-v2-test-device.md#run-v1-lambda-functions)。

   請執行下列操作：

   1. 將元件相依性 (`componentDependencies`) 的映射新增至 中的 Lambda 函數`lambda-function-component.json`。

      ```
      {
        "lambdaFunction": {
          "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
          "componentName": "com.example.HelloWorldLambda",
          "componentVersion": "1.0.0",
          "componentPlatforms": [
            {
              "name": "Linux x86",
              "attributes": {
                "os": "linux",
                "architecture": "x86"
              }
            }
          ],
          "componentDependencies": {
            
          }
        }
      }
      ```

   1. 將每個元件相依性新增至映射。指定元件名稱做為金鑰，並使用下列參數指定物件：
      + `versionRequirement` – 識別元件相依性相容版本的 npm 樣式語意版本限制。您可以指定單一版本或一系列版本。如需語意版本限制的詳細資訊，請參閱 [npm 轉換器計算器](https://semver.npmjs.com/)。
      + `dependencyType` – （選用） 相依性的類型。請選擇下列項目：
        + `SOFT` – 如果相依性變更狀態，Lambda 函數元件不會重新啟動。
        + `HARD` – 如果相依性變更狀態，Lambda 函數元件會重新啟動。

        預設值為 `HARD`。

      下列範例指定此 Lambda 函數取決於[串流管理員元件](stream-manager-component.md)第一個主要版本中的任何版本。當串流管理員重新啟動或更新時，Lambda 函數元件會重新啟動。

      ```
      {
        "aws.greengrass.StreamManager": {
          "versionRequirement": "^1.0.0",
          "dependencyType": "HARD"
        }
      }
      ```

      您的 `lambda-function-component.json`可能包含類似下列範例的文件。

      ```
      {
        "lambdaFunction": {
          "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
          "componentName": "com.example.HelloWorldLambda",
          "componentVersion": "1.0.0",
          "componentPlatforms": [
            {
              "name": "Linux x86",
              "attributes": {
                "os": "linux",
                "architecture": "x86"
              }
            }
          ],
          "componentDependencies": {
            "aws.greengrass.StreamManager": {
              "versionRequirement": "^1.0.0",
              "dependencyType": "HARD"
            }
          }
        }
      }
      ```

1. （選用） 設定 Lambda 函數參數以用來執行函數。您可以設定選項，例如環境變數、訊息事件來源、逾時和容器設定。請執行下列操作：

   1. 將 Lambda 參數物件 (`componentLambdaParameters`) 新增至 中的 Lambda 函數`lambda-function-component.json`。

      ```
      {
        "lambdaFunction": {
          "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
          "componentName": "com.example.HelloWorldLambda",
          "componentVersion": "1.0.0",
          "componentPlatforms": [
            {
              "name": "Linux x86",
              "attributes": {
                "os": "linux",
                "architecture": "x86"
              }
            }
          ],
          "componentDependencies": {
            "aws.greengrass.StreamManager": {
              "versionRequirement": "^1.0.0",
              "dependencyType": "HARD"
            }
          },
          "componentLambdaParameters": {
          
          }
        }
      }
      ```

   1. （選用） 指定 Lambda 函數訂閱工作訊息的事件來源。您可以指定事件來源，將此函數訂閱本機發佈/訂閱訊息和 AWS IoT Core MQTT 訊息。從事件來源接收訊息時，會呼叫 Lambda 函數。
**注意**  
若要將此函數訂閱其他 Lambda 函數或元件的訊息，請在部署此 Lambda 函數元件時部署[舊版訂閱路由器](legacy-subscription-router-component.md)元件。部署舊版訂閱路由器元件時，請指定 Lambda 函數使用的訂閱。

      請執行下列操作：

      1. 將事件來源清單 (`eventSources`) 新增至 Lambda 函數參數。

         ```
         {
           "lambdaFunction": {
             "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
             "componentName": "com.example.HelloWorldLambda",
             "componentVersion": "1.0.0",
             "componentPlatforms": [
               {
                 "name": "Linux x86",
                 "attributes": {
                   "os": "linux",
                   "architecture": "x86"
                 }
               }
             ],
             "componentDependencies": {
               "aws.greengrass.StreamManager": {
                 "versionRequirement": "^1.0.0",
                 "dependencyType": "HARD"
               }
             },
             "componentLambdaParameters": {
               "eventSources": [
               
               ]
             }
           }
         }
         ```

      1. 將每個事件來源新增至清單。每個事件來源都有下列參數：
         + `topic` – 要訂閱訊息的主題。
         + `type` – 事件來源的類型。您可以從以下選項中選擇：
           + `PUB_SUB` - 訂閱本機發佈/訂閱訊息。

             如果您使用 [Greengrass nucleus](greengrass-nucleus-component.md) v2.6.0 或更新版本，以及 [Lambda Manager](lambda-manager-component.md) v2.2.5 或更新版本，您可以在指定此類型`topic`時使用 中的 MQTT 主題萬用字元 (`+` 和 `#`)。
           + `IOT_CORE` – 訂閱 AWS IoT Core MQTT 訊息。

             當您指定此類型`topic`時，您可以在 中使用 MQTT 主題萬用字元 (`+` 和 `#`)。

           下列範例會針對符合主題篩選條件`hello/world/+`的主題訂閱 AWS IoT Core MQTT。

           ```
           {
             "topic": "hello/world/+",
             "type": "IOT_CORE"
           }
           ```

           您的 `lambda-function-component.json`可能看起來類似下列範例。

           ```
           {
             "lambdaFunction": {
               "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
               "componentName": "com.example.HelloWorldLambda",
               "componentVersion": "1.0.0",
               "componentPlatforms": [
                 {
                   "name": "Linux x86",
                   "attributes": {
                     "os": "linux",
                     "architecture": "x86"
                   }
                 }
               ],
               "componentDependencies": {
                 "aws.greengrass.StreamManager": {
                   "versionRequirement": "^1.0.0",
                   "dependencyType": "HARD"
                 }
               },
               "componentLambdaParameters": {
                 "eventSources": [
                   {
                     "topic": "hello/world/+",
                     "type": "IOT_CORE"
                   }
                 ]
               }
             }
           }
           ```

   1. （選用） 在 Lambda 函數參數物件中指定下列任何參數：
      + `environmentVariables` – Lambda 函數執行時可用的環境變數映射。
      + `execArgs` – 執行時要傳遞給 Lambda 函數的引數清單。
      + `inputPayloadEncodingType` – Lambda 函數支援的承載類型。您可以從以下選項中選擇：
        +  `json` 
        +  `binary` 

        預設：`json`
      + `pinned` – 是否固定 Lambda 函數。預設值為 `true`。<a name="lambda-function-lifecycle-type"></a>
        + 固定 （或長期） Lambda 函數會在 AWS IoT Greengrass 啟動時啟動，並在自己的容器中持續執行。
        + 非固定 （或隨需） Lambda 函數只會在接收工作項目時啟動，並在閒置達指定的閒置時間上限後結束。如果函數有多個工作項目，則 AWS IoT Greengrass Core 軟體會建立函數的多個執行個體。

        使用 `maxIdleTimeInSeconds`來設定函數的閒置時間上限。
      + `timeoutInSeconds` – Lambda 函數在逾時之前可執行的秒數上限。預設為 3 秒。
      + `statusTimeoutInSeconds` – Lambda 函數元件將狀態更新傳送至 Lambda 管理員元件的間隔，以秒為單位。此參數僅適用於固定函數。預設值為 60 秒。
      + `maxIdleTimeInSeconds` – 在 AWS IoT Greengrass 核心軟體停止其程序之前，非固定 Lambda 函數可以閒置的時間上限，以秒為單位。預設值為 60 秒。
      + `maxInstancesCount` – 非固定 Lambda 函數可同時執行的執行個體數目上限。預設值為 100 個執行個體。
      + `maxQueueSize` – Lambda 函數元件的訊息佇列大小上限。Core AWS IoT Greengrass 軟體會將訊息存放在 FIFO first-in-first-out佇列中，直到可以執行 Lambda 函數來取用每個訊息為止。預設值為 1，000 則訊息。

      您的 `lambda-function-component.json`可能包含類似下列範例的文件。

      ```
      {
        "lambdaFunction": {
          "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
          "componentName": "com.example.HelloWorldLambda",
          "componentVersion": "1.0.0",
          "componentPlatforms": [
            {
              "name": "Linux x86",
              "attributes": {
                "os": "linux",
                "architecture": "x86"
              }
            }
          ],
          "componentDependencies": {
            "aws.greengrass.StreamManager": {
              "versionRequirement": "^1.0.0",
              "dependencyType": "HARD"
            }
          },
          "componentLambdaParameters": {
            "eventSources": [
              {
                "topic": "hello/world/+",
                "type": "IOT_CORE"
              }
            ],
            "environmentVariables": {
              "LIMIT": "300"
            },
            "execArgs": [
              "-d"
            ],
            "inputPayloadEncodingType": "json",
            "pinned": true,
            "timeoutInSeconds": 120,
            "statusTimeoutInSeconds": 30,
            "maxIdleTimeInSeconds": 30,
            "maxInstancesCount": 50,
            "maxQueueSize": 500
          }
        }
      }
      ```

   1. （選用） 設定 Lambda 函數的容器設定。根據預設，Lambda 函數會在 AWS IoT Greengrass Core 軟體內的隔離執行期環境中執行。您也可以選擇執行 Lambda 函數作為程序，無需任何隔離。如果您在容器中執行 Lambda 函數，您可以設定容器的記憶體大小，以及 Lambda 函數可用的系統資源。請執行下列操作：

      1. 將 Linux 程序參數物件 (`linuxProcessParams`) 新增至 中的 Lambda 參數物件`lambda-function-component.json`。

         ```
         {
           "lambdaFunction": {
             "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
             "componentName": "com.example.HelloWorldLambda",
             "componentVersion": "1.0.0",
             "componentPlatforms": [
               {
                 "name": "Linux x86",
                 "attributes": {
                   "os": "linux",
                   "architecture": "x86"
                 }
               }
             ],
             "componentDependencies": {
               "aws.greengrass.StreamManager": {
                 "versionRequirement": "^1.0.0",
                 "dependencyType": "HARD"
               }
             },
             "componentLambdaParameters": {
               "eventSources": [
                 {
                   "topic": "hello/world/+",
                   "type": "IOT_CORE"
                 }
               ],
               "environmentVariables": {
                 "LIMIT": "300"
               },
               "execArgs": [
                 "-d"
               ],
               "inputPayloadEncodingType": "json",
               "pinned": true,
               "timeoutInSeconds": 120,
               "statusTimeoutInSeconds": 30,
               "maxIdleTimeInSeconds": 30,
               "maxInstancesCount": 50,
               "maxQueueSize": 500,
               "linuxProcessParams": {
               
               }
             }
           }
         }
         ```

      1. （選用） 指定 Lambda 函數是否在容器中執行。將 `isolationMode` 參數新增至程序參數物件，然後從下列選項中選擇：
         + `GreengrassContainer` – Lambda 函數會在容器中執行。
         + `NoContainer` – Lambda 函數會以無任何隔離的程序執行。

         預設值為 `GreengrassContainer`。

      1. （選用） 如果您在容器中執行 Lambda 函數，您可以設定記憶體數量和系統資源，例如磁碟區和裝置，以便可供容器使用。請執行下列操作：

         1. 將容器參數物件 (`containerParams`) 新增至 中的 Linux 程序參數物件`lambda-function-component.json`。

            ```
            {
              "lambdaFunction": {
                "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
                "componentName": "com.example.HelloWorldLambda",
                "componentVersion": "1.0.0",
                "componentPlatforms": [
                  {
                    "name": "Linux x86",
                    "attributes": {
                      "os": "linux",
                      "architecture": "x86"
                    }
                  }
                ],
                "componentDependencies": {
                  "aws.greengrass.StreamManager": {
                    "versionRequirement": "^1.0.0",
                    "dependencyType": "HARD"
                  }
                },
                "componentLambdaParameters": {
                  "eventSources": [
                    {
                      "topic": "hello/world/+",
                      "type": "IOT_CORE"
                    }
                  ],
                  "environmentVariables": {
                    "LIMIT": "300"
                  },
                  "execArgs": [
                    "-d"
                  ],
                  "inputPayloadEncodingType": "json",
                  "pinned": true,
                  "timeoutInSeconds": 120,
                  "statusTimeoutInSeconds": 30,
                  "maxIdleTimeInSeconds": 30,
                  "maxInstancesCount": 50,
                  "maxQueueSize": 500,
                  "linuxProcessParams": {
                    "containerParams": {
                    
                    }
                  }
                }
              }
            }
            ```

         1. （選用） 新增 `memorySizeInKB` 參數以指定容器的記憶體大小。預設值為 16，384 KB (16 MB)。

         1. （選用） 新增 `mountROSysfs` 參數，以指定容器是否可以從裝置`/sys`資料夾讀取資訊。預設值為 `false`。

         1. （選用） 設定容器化 Lambda 函數可存取的本機磁碟區。當您定義磁碟區時， AWS IoT Greengrass 核心軟體會將來源檔案掛載到容器內的目的地。請執行下列操作：

            1. 將磁碟區清單 (`volumes`) 新增至容器參數。

               ```
               {
                 "lambdaFunction": {
                   "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
                   "componentName": "com.example.HelloWorldLambda",
                   "componentVersion": "1.0.0",
                   "componentPlatforms": [
                     {
                       "name": "Linux x86",
                       "attributes": {
                         "os": "linux",
                         "architecture": "x86"
                       }
                     }
                   ],
                   "componentDependencies": {
                     "aws.greengrass.StreamManager": {
                       "versionRequirement": "^1.0.0",
                       "dependencyType": "HARD"
                     }
                   },
                   "componentLambdaParameters": {
                     "eventSources": [
                       {
                         "topic": "hello/world/+",
                         "type": "IOT_CORE"
                       }
                     ],
                     "environmentVariables": {
                       "LIMIT": "300"
                     },
                     "execArgs": [
                       "-d"
                     ],
                     "inputPayloadEncodingType": "json",
                     "pinned": true,
                     "timeoutInSeconds": 120,
                     "statusTimeoutInSeconds": 30,
                     "maxIdleTimeInSeconds": 30,
                     "maxInstancesCount": 50,
                     "maxQueueSize": 500,
                     "linuxProcessParams": {
                       "containerParams": {
                         "memorySizeInKB": 32768,
                         "mountROSysfs": true,
                         "volumes": [
                         
                         ]
                       }
                     }
                   }
                 }
               }
               ```

            1. 將每個磁碟區新增至清單。每個磁碟區都有下列參數：
               + `sourcePath` – 核心裝置上的來源資料夾路徑。
               + `destinationPath` – 容器中目的地資料夾的路徑。
               + `permission` – （選用） 從容器存取來源資料夾的許可。您可以從以下選項中選擇：
                 + `ro` – Lambda 函數具有來源資料夾的唯讀存取權。
                 + `rw` – Lambda 函數具有來源資料夾的讀寫存取權。

                 預設值為 `ro`。
               + `addGroupOwner` – （選用） 是否要將執行 Lambda 函數元件的系統群組新增為來源資料夾的擁有者。預設值為 `false`。

               您的 `lambda-function-component.json`可能包含類似下列範例的文件。

               ```
               {
                 "lambdaFunction": {
                   "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
                   "componentName": "com.example.HelloWorldLambda",
                   "componentVersion": "1.0.0",
                   "componentPlatforms": [
                     {
                       "name": "Linux x86",
                       "attributes": {
                         "os": "linux",
                         "architecture": "x86"
                       }
                     }
                   ],
                   "componentDependencies": {
                     "aws.greengrass.StreamManager": {
                       "versionRequirement": "^1.0.0",
                       "dependencyType": "HARD"
                     }
                   },
                   "componentLambdaParameters": {
                     "eventSources": [
                       {
                         "topic": "hello/world/+",
                         "type": "IOT_CORE"
                       }
                     ],
                     "environmentVariables": {
                       "LIMIT": "300"
                     },
                     "execArgs": [
                       "-d"
                     ],
                     "inputPayloadEncodingType": "json",
                     "pinned": true,
                     "timeoutInSeconds": 120,
                     "statusTimeoutInSeconds": 30,
                     "maxIdleTimeInSeconds": 30,
                     "maxInstancesCount": 50,
                     "maxQueueSize": 500,
                     "linuxProcessParams": {
                       "containerParams": {
                         "memorySizeInKB": 32768,
                         "mountROSysfs": true,
                         "volumes": [
                           {
                             "sourcePath": "/var/data/src",
                             "destinationPath": "/var/data/dest",
                             "permission": "rw",
                             "addGroupOwner": true
                           }
                         ]
                       }
                     }
                   }
                 }
               }
               ```

         1. （選用） 設定容器化 Lambda 函數可存取的本機系統裝置。請執行下列操作：

            1. 將系統裝置清單 (`devices`) 新增至容器參數。

               ```
               {
                 "lambdaFunction": {
                   "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
                   "componentName": "com.example.HelloWorldLambda",
                   "componentVersion": "1.0.0",
                   "componentPlatforms": [
                     {
                       "name": "Linux x86",
                       "attributes": {
                         "os": "linux",
                         "architecture": "x86"
                       }
                     }
                   ],
                   "componentDependencies": {
                     "aws.greengrass.StreamManager": {
                       "versionRequirement": "^1.0.0",
                       "dependencyType": "HARD"
                     }
                   },
                   "componentLambdaParameters": {
                     "eventSources": [
                       {
                         "topic": "hello/world/+",
                         "type": "IOT_CORE"
                       }
                     ],
                     "environmentVariables": {
                       "LIMIT": "300"
                     },
                     "execArgs": [
                       "-d"
                     ],
                     "inputPayloadEncodingType": "json",
                     "pinned": true,
                     "timeoutInSeconds": 120,
                     "statusTimeoutInSeconds": 30,
                     "maxIdleTimeInSeconds": 30,
                     "maxInstancesCount": 50,
                     "maxQueueSize": 500,
                     "linuxProcessParams": {
                       "containerParams": {
                         "memorySizeInKB": 32768,
                         "mountROSysfs": true,
                         "volumes": [
                           {
                             "sourcePath": "/var/data/src",
                             "destinationPath": "/var/data/dest",
                             "permission": "rw",
                             "addGroupOwner": true
                           }
                         ],
                         "devices": [
                         
                         ]
                       }
                     }
                   }
                 }
               }
               ```

            1. 將每個系統裝置新增至清單。每個系統裝置都有下列參數：
               + `path` – 核心裝置上的系統裝置的路徑。
               + `permission` – （選用） 從容器存取系統裝置的許可。您可以從以下選項中選擇：
                 + `ro` – Lambda 函數具有系統裝置的唯讀存取權。
                 + `rw` – Lambda 函數具有系統裝置的讀寫存取權。

                 預設值為 `ro`。
               + `addGroupOwner` – （選用） 是否要將執行 Lambda 函數元件的系統群組新增為系統裝置的擁有者。預設值為 `false`。

            您的 `lambda-function-component.json`可能包含類似下列範例的文件。

            ```
            {
              "lambdaFunction": {
                "lambdaArn": "arn:aws:lambda:region:account-id:function:HelloWorld:1",
                "componentName": "com.example.HelloWorldLambda",
                "componentVersion": "1.0.0",
                "componentPlatforms": [
                  {
                    "name": "Linux x86",
                    "attributes": {
                      "os": "linux",
                      "architecture": "x86"
                    }
                  }
                ],
                "componentDependencies": {
                  "aws.greengrass.StreamManager": {
                    "versionRequirement": "^1.0.0",
                    "dependencyType": "HARD"
                  }
                },
                "componentLambdaParameters": {
                  "eventSources": [
                    {
                      "topic": "hello/world/+",
                      "type": "IOT_CORE"
                    }
                  ],
                  "environmentVariables": {
                    "LIMIT": "300"
                  },
                  "execArgs": [
                    "-d"
                  ],
                  "inputPayloadEncodingType": "json",
                  "pinned": true,
                  "timeoutInSeconds": 120,
                  "statusTimeoutInSeconds": 30,
                  "maxIdleTimeInSeconds": 30,
                  "maxInstancesCount": 50,
                  "maxQueueSize": 500,
                  "linuxProcessParams": {
                    "containerParams": {
                      "memorySizeInKB": 32768,
                      "mountROSysfs": true,
                      "volumes": [
                        {
                          "sourcePath": "/var/data/src",
                          "destinationPath": "/var/data/dest",
                          "permission": "rw",
                          "addGroupOwner": true
                        }
                      ],
                      "devices": [
                        {
                          "path": "/dev/sda3",
                          "permission": "rw",
                          "addGroupOwner": true
                        }
                      ]
                    }
                  }
                }
              }
            }
            ```

1. （選用） 為元件新增標籤 (`tags`)。如需詳細資訊，請參閱[標記您的 AWS IoT Greengrass Version 2 資源](tag-resources.md)。

## 步驟 2：建立 Lambda 函數元件
<a name="create-lambda-component-cli"></a>

1. 執行下列命令，從 建立 Lambda 函數元件`lambda-function-component.json`。

   ```
   aws greengrassv2 create-component-version --cli-input-json file://lambda-function-component.json
   ```

   如果請求成功，回應看起來與下列範例類似。

   ```
   {
     "arn": "arn:aws:greengrass:region:123456789012:components:com.example.HelloWorldLambda:versions:1.0.0",
     "componentName": "com.example.HelloWorldLambda",
     "componentVersion": "1.0.0",
     "creationTimestamp": "Mon Dec 15 20:56:34 UTC 2020",
     "status": {
       "componentState": "REQUESTED",
       "message": "NONE",
       "errors": {}
     }
   }
   ```

   `arn` 從輸出複製 ，以在下一個步驟中檢查元件的狀態。

1. 當您建立元件時，其狀態為 `REQUESTED`。然後， AWS IoT Greengrass 驗證元件可部署。您可以執行下列命令來查詢元件狀態，並確認元件可部署。將 取代`arn`為上一個步驟的 ARN。

   ```
   aws greengrassv2 describe-component \
     --arn "arn:aws:greengrass:region:account-id:components:com.example.HelloWorldLambda:versions:1.0.0"
   ```

   如果元件驗證，回應會指出元件狀態為 `DEPLOYABLE`。

   ```
   {
     "arn": "arn:aws:greengrass:region:account-id:components:com.example.HelloWorldLambda:versions:1.0.0",
     "componentName": "com.example.HelloWorldLambda",
     "componentVersion": "1.0.0",
     "creationTimestamp": "2020-12-15T20:56:34.376000-08:00",
     "publisher": "AWS Lambda",
     "status": {
       "componentState": "DEPLOYABLE",
       "message": "NONE",
       "errors": {}
     },
     "platforms": [
       {
         "name": "Linux x86",
         "attributes": {
           "architecture": "x86",
           "os": "linux"
         }
       }
     ]
   }
   ```

   元件為 之後`DEPLOYABLE`，您可以將 Lambda 函數部署至核心裝置。如需詳細資訊，請參閱[將 AWS IoT Greengrass 元件部署至裝置](manage-deployments.md)。