LoginSignup
6
1

More than 5 years have passed since last update.

初級:AWS Lambda + .NET C# の最初の一歩

Posted at

仕事で AWS Lambda + .NET C# の案件があったのでまずは環境構築を。
手元の環境は Mac で作業。
aws cli のインストールと profile の設定は既にされてるものとして進めます。
https://docs.aws.amazon.com/ja_jp/streams/latest/dev/kinesis-tutorial-cli-installation.html

まずは下記URLから .NET Core の SDKのダウンロード&インストール。
https://dotnet.microsoft.com/download

以下全てターミナルで作業。

まずは正常にインストールできたか確認。

$ dotnet --version
2.1.500

できてたら、続いて AWS Lamda の .NET Core グローバル ツールをインストール。

$ dotnet tool install -g Amazon.Lambda.Tools
次のコマンドを使用してツールを呼び出せます。dotnet-lambda
ツール 'amazon.lambda.tools' (バージョン '3.1.1') が正常にインストールされました。

次にテンプレートをインストール。

$ dotnet new -i Amazon.Lambda.Templates
  /Users/hogehoge/.templateengine/dotnetcli/v2.1.500/scratch/restore.csproj のパッケージを復元しています...
  Amazon.Lambda.Templates 3.4.0 をインストールしています。
  MSBuild ファイル /Users/hogehoge/.templateengine/dotnetcli/v2.1.500/scratch/obj/restore.csproj.nuget.g.props を生成しています。
  MSBuild ファイル /Users/hogehoge/.templateengine/dotnetcli/v2.1.500/scratch/obj/restore.csproj.nuget.g.targets を生成しています。
  /Users/hogehoge/.templateengine/dotnetcli/v2.1.500/scratch/restore.csproj の復元が 4.81 sec で完了しました。

使用法: new [options]

オプション:
  -h, --help          このコマンドのヘルプを表示します。
  -l, --list          指定された名前を含むテンプレートを一覧表示します。名前を指定しない場合は、すべてのテンプレートが一覧表示されます。
  -n, --name          作成する出力の名前。名前を指定しない場合は、現在のディレクトリの名前が使用されます。
  -o, --output        生成する出力を配置する場所。
  -i, --install       ソースまたはテンプレート パックをインストールします。
  -u, --uninstall     ソースまたはテンプレート パックをアンインストールします。
  --nuget-source      インストール中に使用する NuGet ソースを 1 つ指定します。
  --type              使用可能な種類に基づいてテンプレートをフィルター処理します。定義済みの値は、"project"、"item"、"other" です。
  --force             既存のファイルが変更されるとしても、強制的にコンテンツを生成します。
  -lang, --language   言語に基づいてテンプレートをフィルター処理し、作成するテンプレートの言語を指定します。


テンプレート                                                    短い名前                                    言語                タグ                                   
---------------------------------------------------------------------------------------------------------------------------------------------------------
Order Flowers Chatbot Tutorial                            lambda.OrderFlowersChatbot              [C#]              AWS/Lambda/Function                  
Lambda Detect Image Labels                                lambda.DetectImageLabels                [C#], F#          AWS/Lambda/Function                  
Lambda Empty Function                                     lambda.EmptyFunction                    [C#], F#          AWS/Lambda/Function                  
Lex Book Trip Sample                                      lambda.LexBookTripSample                [C#]              AWS/Lambda/Function                  
Lambda Simple DynamoDB Function                           lambda.DynamoDB                         [C#], F#          AWS/Lambda/Function                  
Lambda Simple Kinesis Firehose Function                   lambda.KinesisFirehose                  [C#]              AWS/Lambda/Function                  
Lambda Simple Kinesis Function                            lambda.Kinesis                          [C#], F#          AWS/Lambda/Function                  
Lambda Simple S3 Function                                 lambda.S3                               [C#], F#          AWS/Lambda/Function                  
Lambda Simple SQS Function                                lambda.SQS                              [C#]              AWS/Lambda/Function                  
Lambda ASP.NET Core Web API                               serverless.AspNetCoreWebAPI             [C#], F#          AWS/Lambda/Serverless                
Lambda ASP.NET Core Web Application with Razor Pages      serverless.AspNetCoreWebApp             [C#]              AWS/Lambda/Serverless                
Serverless Detect Image Labels                            serverless.DetectImageLabels            [C#], F#          AWS/Lambda/Serverless                
Lambda DynamoDB Blog API                                  serverless.DynamoDBBlogAPI              [C#]              AWS/Lambda/Serverless                
Lambda Empty Serverless                                   serverless.EmptyServerless              [C#], F#          AWS/Lambda/Serverless                
Lambda Giraffe Web App                                    serverless.Giraffe                      F#                AWS/Lambda/Serverless                
Serverless Simple S3 Function                             serverless.S3                           [C#], F#          AWS/Lambda/Serverless                
Step Functions Hello World                                serverless.StepFunctionsHelloWorld      [C#], F#          AWS/Lambda/Serverless                
Console Application                                       console                                 [C#], F#, VB      Common/Console                       
Class library                                             classlib                                [C#], F#, VB      Common/Library                       
Unit Test Project                                         mstest                                  [C#], F#, VB      Test/MSTest                          
NUnit 3 Test Project                                      nunit                                   [C#], F#, VB      Test/NUnit                           
NUnit 3 Test Item                                         nunit-test                              [C#], F#, VB      Test/NUnit                           
xUnit Test Project                                        xunit                                   [C#], F#, VB      Test/xUnit                           
Razor Page                                                page                                    [C#]              Web/ASP.NET                          
MVC ViewImports                                           viewimports                             [C#]              Web/ASP.NET                          
MVC ViewStart                                             viewstart                               [C#]              Web/ASP.NET                          
ASP.NET Core Empty                                        web                                     [C#], F#          Web/Empty                            
ASP.NET Core Web App (Model-View-Controller)              mvc                                     [C#], F#          Web/MVC                              
ASP.NET Core Web App                                      razor                                   [C#]              Web/MVC/Razor Pages                  
ASP.NET Core with Angular                                 angular                                 [C#]              Web/MVC/SPA                          
ASP.NET Core with React.js                                react                                   [C#]              Web/MVC/SPA                          
ASP.NET Core with React.js and Redux                      reactredux                              [C#]              Web/MVC/SPA                          
Razor Class Library                                       razorclasslib                           [C#]              Web/Razor/Library/Razor Class Library
ASP.NET Core Web API                                      webapi                                  [C#], F#          Web/WebAPI                           
global.json file                                          globaljson                                                Config                               
NuGet Config                                              nugetconfig                                               Config                               
Web Config                                                webconfig                                                 Config                               
Solution File                                             sln                                                       Solution                             

Examples:
    dotnet new mvc --auth Individual
    dotnet new console 
    dotnet new --help

作業ディレクトリに移動しましょう。

$ cd ~/git

インストールできたら、プロジェクトを作ります。
※ profile と region は各自の環境に合わせて変更

$ dotnet new lambda.EmptyFunction --name MyFunction --profile hogehoge --region ap-northeast-1
テンプレート "Lambda Empty Function" が正常に作成されました。

ソースのディレクトリに移動

$ cd MyFunction/src/MyFunction

デプロイしてみましょう
※実行ロールは各自の環境に合わせて変更 【実行ロール作成方法

$ dotnet lambda deploy-function MyFunction --function-role piyopiyo
Amazon Lambda Tools for .NET Core applications (3.1.1)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Executing publish command
... invoking 'dotnet publish', working folder '/Users/hogehoge/git/MyFunction/src/MyFunction/bin/Release/netcoreapp2.1/publish'
... Disabling compilation context to reduce package size. If compilation context is needed pass in the "/p:PreserveCompilationContext=false" switch.
... publish: .NET Core 向け Microsoft (R) Build Engine バージョン 15.9.20+g88f5fadfbe
... publish: Copyright (C) Microsoft Corporation.All rights reserved.
... publish:   /Users/hogehoge/git/MyFunction/src/MyFunction/MyFunction.csproj のパッケージを復元しています...
... publish:   runtime.linux-x64.Microsoft.NETCore.DotNetAppHost 2.1.0 をインストールしています。
... publish:   runtime.linux-x64.Microsoft.NETCore.DotNetHostResolver 2.1.0 をインストールしています。
... publish:   runtime.linux-x64.Microsoft.NETCore.DotNetHostPolicy 2.1.0 をインストールしています。
... publish:   runtime.linux-x64.Microsoft.NETCore.App 2.1.0 をインストールしています。
... publish:   /Users/hogehoge/git/MyFunction/src/MyFunction/MyFunction.csproj の復元が 7.58 sec で完了しました。
... publish:   MyFunction -> /Users/hogehoge/git/MyFunction/src/MyFunction/bin/Release/netcoreapp2.1/rhel.7.2-x64/MyFunction.dll
... publish:   MyFunction -> /Users/hogehoge/git/MyFunction/src/MyFunction/bin/Release/netcoreapp2.1/publish/
Changed permissions on published dll (chmod +r MyFunction.dll).
Changed permissions on published dll (chmod +r Newtonsoft.Json.dll).
Changed permissions on published dll (chmod +r Amazon.Lambda.Core.dll).
Changed permissions on published dll (chmod +r Amazon.Lambda.Serialization.Json.dll).
Zipping publish folder /Users/hogehoge/git/MyFunction/src/MyFunction/bin/Release/netcoreapp2.1/publish to /Users/hogehoge/git/MyFunction/src/MyFunction/bin/Release/netcoreapp2.1/MyFunction.zip
... zipping:   adding: MyFunction.dll (deflated 57%)
... zipping:   adding: MyFunction.deps.json (deflated 81%)
... zipping:   adding: Newtonsoft.Json.dll (deflated 60%)
... zipping:   adding: Amazon.Lambda.Core.dll (deflated 57%)
... zipping:   adding: Amazon.Lambda.Serialization.Json.dll (deflated 56%)
... zipping:   adding: MyFunction.pdb (deflated 28%)
... zipping:   adding: MyFunction.runtimeconfig.json (deflated 23%)
Created publish archive (/Users/hogehoge/git/MyFunction/src/MyFunction/bin/Release/netcoreapp2.1/MyFunction.zip).
Creating new Lambda function MyFunction
New Lambda function created

終わったなら、とりあえず aws cli を使って確認してみる。
※ profile と region は各自の環境に合わせて変更

$ aws lambda list-functions --profile hogehoge --region ap-northeast-1 | grep FunctionName.*MyFunction
            "FunctionName": "MyFunction",

実行してみる。

$ dotnet lambda invoke-function MyFunction --payload "Just Checking If Everything is OK"
Amazon Lambda Tools for .NET Core applications (3.1.1)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Payload:
"JUST CHECKING IF EVERYTHING IS OK" 

Log Tail:
START RequestId: e2948e27-f306-11e8-a33c-bba7e089cad8 Version: $LATEST
END RequestId: e999999-f9999-9999-a999-b999999
REPORT RequestId: e999999-f9999-9999-a999-b999999    Duration: 915.41 ms    Billed Duration: 1000 ms     Memory Size: 256 MB    Max Memory Used: 54 MB

動いた。ハッピー。

参考
AWS ドキュメント » AWS Lambda » 開発者ガイド » Lambda 関数 » Lambda 関数のビルド » デプロイパッケージの作成 » デプロイパッケージの作成 (C#) » .NET Core CLI

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1