

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

# 使用 .NET Core CLI 部署 AWS Lambda 專案
<a name="lambda-cli-publish"></a>

 AWS Toolkit for Visual Studio 包含 Visual Studio 的 AWS Lambda .NET Core 專案範本。您可以使用 .NET Core 命令列界面 (CLI) 部署 Visual Studio 中內建的 Lambda 函數。

**Topics**
+ [先決條件](#lambda-cli-prereqs)
+ [相關主題](#lambda-cli-related)
+ [列出透過 .NET Core CLI 可用的 Lambda 命令](#listing-the-lam-commands-available-through-the-cli)
+ [從 .NET Core CLI 發佈 .NET Core Lambda 專案](#publishing-a-net-core-lam-project-from-the-net-core-cli)

## 先決條件
<a name="lambda-cli-prereqs"></a>

使用 .NET Core CLI 部署 Lambda 函數之前，您必須符合下列先決條件：
+ 確定已安裝 Visual Studio 2015 Update 3。
+ 安裝適用於 [Windows 的 .NET Core](https://dotnet.microsoft.com/download#windowsvs2015)。
+ 設定 .NET Core CLI 以使用 Lambda。如需詳細資訊，請參閱《 *AWS Lambda 開發人員指南*》中的 .[NET Core CLI](https://docs.aws.amazon.com//lambda/latest/dg/csharp-package-cli.html)。
+ 安裝 Toolkit for Visual Studio。如需詳細資訊，請參閱[安裝 AWS Toolkit for Visual Studio](setup.md#install)。

## 相關主題
<a name="lambda-cli-related"></a>

當您使用 .NET Core CLI 部署 Lambda 函數時，下列相關主題會很有幫助：
+ 如需 Lambda 函數的詳細資訊，請參閱《 *AWS Lambda 開發人員指南*》中的[什麼是 AWS Lambda？](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)。
+ 如需在 Visual Studio 中建立 Lambda 函數的詳細資訊，請參閱 [AWS Lambda](lambda-index.md)。
+ 如需 Microsoft .NET Core 的詳細資訊，請參閱 Microsoft 線上文件中的 .[NET Core](https://docs.microsoft.com/en-us/dotnet/articles/core/)。

## 列出透過 .NET Core CLI 可用的 Lambda 命令
<a name="listing-the-lam-commands-available-through-the-cli"></a>

若要列出可透過 .NET Core CLI 取得的 Lambda 命令，請執行下列動作。

1. 開啟命令提示視窗，然後導覽至包含 Visual Studio .NET Core Lambda 專案的資料夾。

1. 輸入 `dotnet lambda --help`。

```
C:\Lambda\AWSLambda1\AWSLambda1>dotnet lambda --help AWS Lambda Tools for .NET Core functions
    Project Home: https://github.com/aws/aws-lambda-dotnet
    .
    Commands to deploy and manage Lambda functions:
    .
            deploy-function         Deploy the project to Lambda
            invoke-function         Invoke the function in Lambda with an optional input
            list-functions          List all of your Lambda functions
            delete-function         Delete a Lambda function
            get-function-config     Get the current runtime configuration for a Lambda function
            update-function-config  Update the runtime configuration for a Lambda function
    .
    Commands to deploy and manage AWS serverless applications using AWS CloudFormation:
    .
            deploy-serverless       Deploy an AWS serverless application
            list-serverless         List all of your AWS serverless applications
            delete-serverless       Delete an AWS serverless application
    .
    Other Commands:
    .
            package                 Package a Lambda project into a .zip file ready for deployment
    .
    To get help on individual commands, run the following:

            dotnet lambda help <command>
```

## 從 .NET Core CLI 發佈 .NET Core Lambda 專案
<a name="publishing-a-net-core-lam-project-from-the-net-core-cli"></a>

下列指示假設您已在 Visual Studio 中建立 AWS Lambda .NET Core 函數。

1. 開啟命令提示視窗，然後導覽至包含 Visual Studio .NET Core Lambda 專案的資料夾。

1. 輸入 `dotnet lambda deploy-function`。

1. 出現提示時，輸入要部署的函數名稱。它可以是新名稱或現有函數的名稱。

1. 出現提示時，輸入 AWS 區域 （將部署 Lambda 函數的區域）。

1. 出現提示時，選取或建立 Lambda 在執行函數時將擔任的 IAM 角色。

成功完成時，會顯示**建立的新 Lambda 函數**訊息。

```
C:\Lambda\AWSLambda1\AWSLambda1>dotnet lambda deploy-function
Executing publish command
... invoking 'dotnet publish', working folder 'C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish'
... publish: Publishing AWSLambda1 for .NETCoreApp,Version=v1.0
... publish: Project AWSLambda1 (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
... publish: Compiling AWSLambda1 for .NETCoreApp,Version=v1.0
... publish: Compilation succeeded.
... publish:     0 Warning(s)
... publish:     0 Error(s)
... publish: Time elapsed 00:00:01.2479713
... publish:
... publish: publish: Published to C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish
... publish: Published 1/1 projects successfully
Zipping publish folder C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish to C:\Lambda\AWSLambda1\AWSLamb
da1\bin\Release\netcoreapp1.0\AWSLambda1.zip
Enter Function Name: (AWS Lambda function name)
DotNetCoreLambdaTest
Enter AWS Region: (The region to connect to AWS services)
us-west-2
Creating new Lambda function
Select IAM Role that Lambda will assume when executing function:
    1) lambda_exec_LambdaCoreFunction
    2) *** Create new IAM Role ***
1
New Lambda function created
```

如果您部署現有的 函數，則部署函數只會向 AWS 區域要求 。

```
C:\Lambda\AWSLambda1\AWSLambda1>dotnet lambda deploy-function
Executing publish command
Deleted previous publish folder
... invoking 'dotnet publish', working folder 'C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish'
... publish: Publishing AWSLambda1 for .NETCoreApp,Version=v1.0
... publish: Project AWSLambda1 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
... publish: publish: Published to C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish
... publish: Published 1/1 projects successfully
Zipping publish folder C:\Lambda\AWSLambda1\AWSLambda1\bin\Release\netcoreapp1.0\publish to C:\Lambda\AWSLambda1\AWSLamb
da1\bin\Release\netcoreapp1.0\AWSLambda1.zip
Enter Function Name: (AWS Lambda function name)
DotNetCoreLambdaTest
Enter AWS Region: (The region to connect to AWS services)
us-west-2
Updating code for existing function
```

部署 Lambda 函數之後，即可開始使用。如需詳細資訊，請參閱[如何使用 AWS Lambda 的範例](https://docs.aws.amazon.com/lambda/latest/dg/use-cases.html)。

Lambda 會自動為您監控 Lambda 函數，並透過 Amazon CloudWatch 報告指標。若要監控和疑難排解 Lambda 函數，請參閱[使用 Amazon CloudWatch 疑難排解和監控 AWS Lambda 函數](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-functions.html)。