.Net Core on Lambdaの開発環境をCloud9で作ってみた

西澤です。今回.Net Core on Lambdaの開発環境をCloud9で用意する機会がありましたので、手順をまとめておこうと思います。



Nameは任意のものを指定して、Next stepをクリックします。

今回の環境は、Platform=Amazon Linuxで手順を用意しました。それ以外のパラメータはコストをかけずに必要最低限のものが良いというところだったので、ほぼデフォルトのままで設定しています。

最後に確認をしたら、Create environmentします。


.Net Core 2.1インストール


cm-nishizawa.tetsunori:~/environment $ mkdir ~/downloads

cm-nishizawa.tetsunori:~/environment $ mkdir ~/dotnet

cm-nishizawa.tetsunori:~/environment $ cd ~/downloads

cm-nishizawa.tetsunori:~/downloads $ curl -O https://download.visualstudio.microsoft.com/download/pr/1ac0b9ff-cfb8-4ccc-a2e8-81188af12b54/c94d82d604ac0d16b26843f8fdade618/dotnet-sdk-2.1.700-linux-x64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  159M  100  159M    0     0  19.0M      0  0:00:08  0:00:08 --:--:-- 20.3M

cm-nishizawa.tetsunori:~/downloads $ tar zxf dotnet-sdk-2.1.*-linux-x64.tar.gz -C ~/dotnet/

cm-nishizawa.tetsunori:~/downloads $ ls -la ~/dotnet/
total 156
drwxr-xr-x  5 ec2-user ec2-user   4096 May 16 01:30 .
drwx------ 15 ec2-user ec2-user   4096 May 24 12:53 ..
-rwxr-xr-x  1 ec2-user ec2-user 105872 Apr 17 23:56 dotnet
drwxr-xr-x  3 ec2-user ec2-user   4096 May 16 01:30 host
-rw-r--r--  1 ec2-user ec2-user   1083 Apr 17 23:52 LICENSE.txt
drwxr-xr-x  3 ec2-user ec2-user   4096 May 16 01:30 sdk
drwxr-xr-x  5 ec2-user ec2-user   4096 May 16 01:30 shared
-rw-r--r--  1 ec2-user ec2-user  27700 Apr 17 23:52 ThirdPartyNotices.txt

cm-nishizawa.tetsunori:~/downloads $ cat << \EOF >> ~/.bash_profile
> export PATH=$PATH:$HOME/dotnet:$HOME/.dotnet/tools
> export DOTNET_ROOT=$HOME/dotnet
cm-nishizawa.tetsunori:~/downloads $ source ~/.bash_profile

cm-nishizawa.tetsunori:~/downloads $ dotnet --version

cm-nishizawa.tetsunori:~/environment/lambda_s3/src/lambda_s3 $ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   2.1.700
 Commit:    c2ef055a0f

Runtime Environment:
 OS Name:     amzn
 OS Version:  2018.03
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /home/ec2-user/dotnet/sdk/2.1.700/

Host (useful for support):
  Version: 2.1.11
  Commit:  d6a5616240

.NET Core SDKs installed:
  2.1.700 [/home/ec2-user/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.11 [/home/ec2-user/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.11 [/home/ec2-user/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.11 [/home/ec2-user/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:

Amazon Linux用のパッケージは存在しなかった為、下記サイトから、Lambdaのランタイムとして対応している.Net Core 2.1の最新のものから、NET Core Binaries: x64をダウンロードしました。



cm-nishizawa.tetsunori:~/downloads $ dotnet tool install -g Amazon.Lambda.Tools

Welcome to .NET Core!
Learn more about .NET Core: https://aka.ms/dotnet-docs
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs

The .NET Core tools collect usage data in order to help us improve your experience. The data is anonymous and doesn't include command-line arguments. The data is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

A command is running to populate your local package cache to improve restore speed and enable offline access. This command takes up to one minute to complete and only runs once.
Decompressing 100% 9382 ms
Expanding 100% 30250 ms

Successfully installed the ASP.NET Core HTTPS Development Certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only). For establishing trust on other platforms refer to the platform specific documentation.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
You can invoke the tool using the following command: dotnet-lambda
Tool 'amazon.lambda.tools' (version '3.2.3') was successfully installed.


cm-nishizawa.tetsunori:~/downloads $ dotnet new -i Amazon.Lambda.Templates
Getting ready...
  Restore completed in 1.78 sec for /home/ec2-user/.templateengine/dotnetcli/v2.1.700/scratch/restore.csproj.

Usage: new [options]

  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.

Templates                                                 Short Name                                        Language          Tags                                 
Order Flowers Chatbot Tutorial                            lambda.OrderFlowersChatbot                        [C#]              AWS/Lambda/Function                  
Lambda Custom Runtime Function                            lambda.CustomRuntimeFunction                      [C#], F#          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 Application Load Balancer Function          lambda.SimpleApplicationLoadBalancerFunction      [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                             

    dotnet new mvc --auth Individual
    dotnet new webapi 
    dotnet new --help



Lambdaが.Net Core2.1に対応しました!!

cm-nishizawa.tetsunori:~/downloads $ cd ~/environment/

cm-nishizawa.tetsunori:~/environment $ dotnet new lambda.S3 --name lambda_s3
The template "Lambda Simple S3 Function" was created successfully.

cm-nishizawa.tetsunori:~/environment $ cd lambda_s3/src/lambda_s3/

cm-nishizawa.tetsunori:~/environment/lambda_s3/src/lambda_s3 $ ls -l
total 24
-rw-rw-r-- 1 ec2-user ec2-user  706 May 24 13:51 aws-lambda-tools-defaults.json
drwxrwxr-x 3 ec2-user ec2-user 4096 May 24 14:09 bin
-rw-rw-r-- 1 ec2-user ec2-user 2438 May 24 13:51 Function.cs
-rw-rw-r-- 1 ec2-user ec2-user  581 May 24 13:51 lambda_s3.csproj
drwxrwxr-x 3 ec2-user ec2-user 4096 May 24 14:09 obj
-rw-rw-r-- 1 ec2-user ec2-user 2381 May 24 13:51 Readme.md

cm-nishizawa.tetsunori:~/environment/lambda_s3/src/lambda_s3 $ dotnet lambda deploy-function lambda_s3
Amazon Lambda Tools for .NET Core applications (3.2.3)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Executing publish command
Deleted previous publish folder
... invoking 'dotnet publish', working folder '/home/ec2-user/environment/lambda_s3/src/lambda_s3/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: Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core
... publish: Copyright (C) Microsoft Corporation. All rights reserved.
... publish:   Restore completed in 102.68 ms for /home/ec2-user/environment/lambda_s3/src/lambda_s3/lambda_s3.csproj.
... publish:   lambda_s3 -> /home/ec2-user/environment/lambda_s3/src/lambda_s3/bin/Release/netcoreapp2.1/rhel.7.2-x64/lambda_s3.dll
... publish:   lambda_s3 -> /home/ec2-user/environment/lambda_s3/src/lambda_s3/bin/Release/netcoreapp2.1/publish/
Changed permissions on published file (chmod +rx AWSSDK.S3.dll).
Changed permissions on published file (chmod +rx Amazon.Lambda.S3Events.dll).
Changed permissions on published file (chmod +rx Newtonsoft.Json.dll).
Changed permissions on published file (chmod +rx lambda_s3.pdb).
Changed permissions on published file (chmod +rx lambda_s3.runtimeconfig.json).
Changed permissions on published file (chmod +rx lambda_s3.deps.json).
Changed permissions on published file (chmod +rx Amazon.Lambda.Serialization.Json.dll).
Changed permissions on published file (chmod +rx lambda_s3.dll).
Changed permissions on published file (chmod +rx AWSSDK.Core.dll).
Changed permissions on published file (chmod +rx Amazon.Lambda.Core.dll).
Zipping publish folder /home/ec2-user/environment/lambda_s3/src/lambda_s3/bin/Release/netcoreapp2.1/publish to /home/ec2-user/environment/lambda_s3/src/lambda_s3/bin/Release/netcoreapp2.1/lambda_s3.zip
... zipping:   adding: AWSSDK.S3.dll (deflated 65%)
... zipping:   adding: Amazon.Lambda.S3Events.dll (deflated 46%)
... zipping:   adding: Newtonsoft.Json.dll (deflated 60%)
... zipping:   adding: lambda_s3.pdb (deflated 32%)
... zipping:   adding: lambda_s3.runtimeconfig.json (deflated 23%)
... zipping:   adding: lambda_s3.deps.json (deflated 81%)
... zipping:   adding: Amazon.Lambda.Serialization.Json.dll (deflated 49%)
... zipping:   adding: lambda_s3.dll (deflated 53%)
... zipping:   adding: AWSSDK.Core.dll (deflated 67%)
... zipping:   adding: Amazon.Lambda.Core.dll (deflated 46%)
Created publish archive (/home/ec2-user/environment/lambda_s3/src/lambda_s3/bin/Release/netcoreapp2.1/lambda_s3.zip).
Updating code for existing function lambda_s3


ちなみに、--region指定なしでもCloud9環境が動作する環境のリージョンが指定され、--profile指定なしでAWS Management ConsoleにログインしたIAM権限の一時クレデンシャルにて動作することが確認できました!

cm-nishizawa.tetsunori:~/environment/lambda_s3/src/lambda_s3 $ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************YO5K shared-credentials-file    
secret_key     ****************h1kV shared-credentials-file    
    region                us-west-2      config-file    ~/.aws/config


今回はこれまであまり触れたことの無かった".Net Core on Lambda"の開発環境を作ることになったのですが、ローカル環境に依存せず共有または環境を再現できるようにしておきたかったので、Cloud9がぴったりハマりました。