注意
この記事の内容は2017年3月10日時点の物です。
内容が古くなっている場合があります。
Ⅰ. はじめに
タイトルの通りですが、
AWS Lambda
でC#
を動かし、エンドポイントにAmazon API Gateway
を使う方法です。
Ⅱ. ゴール
身長
と体重
を受け取り、BMI
と理想体重
を出力するAPIを作成することをゴールとします。
(画像のツールはPostmanです)
Ⅲ. 前提条件
- .NET Core SDKをインストールしている事
Ⅳ. 手順(AWS Toolkit for Visual Studioを利用する方法)
Windows 上で動作する VisualStudio が必要です。
Visual Studio Code
や VisualStudio for Mac
や JetBrains Rider
はこの記事を書いた時点ではサポートされていませんでした。
アドオンなども記事を書いた時点ではありませんでした。
1. AWS Toolkit for Visual Studioをインストールする
ダウンロード、インストールを完了させてください。
https://aws.amazon.com/jp/visualstudio/
2.プロジェクトを作成する
AWS Lambda Project (.NET Core)
を選択 して、プロジェクトを作成して下さい。
3. Empty Functionを選択
今回は簡単な計算アプリケーションの為、Empty Functionを選択します。
4. dotnet restoreする
プロジェクトディレクトリを開き以下のコマンドをコマンドプロンプトで実行します。
※2017/03/10追記
Visual Studio 2017では dotnet restoreをコマンドプロンプトで行う必要が無くなりました。
dotnet restore
Restore completed
と表示されればOKです。
<s>この手順は AWS Lambda のプロジェクト作成に限らず、 .NET Core プロジェクトを作成した後に必ず必要な手順です。 NuGet からインストール、アンインストールした場合や、 参照の追加、削除した場合にも必ず必要になります。 めんどくさいですね。 誰か良い物作って下さい(他力本願)</s>
5. プログラムを書く
VisualStudio に戻り以下のプログラムをコピペして下さい。
API Gatewayの統合リクエストの仕様に合わせたサンプルとなっています。
namespace
は 適宜合わせて下さい。
Function.cs
using System; using System.Collections.Generic; using System.Net; using Amazon.Lambda.Core; using Newtonsoft.Json; [assembly: LambdaSerializerAttribute(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] namespace AWSLambdaBmi { public class LambdaRequest { [JsonProperty(PropertyName = "body")] public string Body { get; set; } } public class LambdaResponse { [JsonProperty(PropertyName = "statusCode")] public HttpStatusCode StatusCode { get; set; } [JsonProperty(PropertyName = "headers")] public Dictionary<string, string> Headers { get; set; } [JsonProperty(PropertyName = "body")] public string Body { get; set; } } public class RequestParam { [JsonProperty(PropertyName = "hight")] public double Hight { get; set; } [JsonProperty(PropertyName = "weight")] public double Weight { get; set; } } public class ResponseParam { [JsonProperty(PropertyName = "bmi")] public double Bmi { get; set; } [JsonProperty(PropertyName = "ideal_body_weight")] public double IdealBodyWeight { get; set; } } public class Function { public LambdaResponse FunctionHandler(LambdaRequest input, ILambdaContext context) { var data = JsonConvert.DeserializeObject<RequestParam>(input.Body); // BMI = 体重kg / ( 身長(m) * (身長m) ) var bmi = data.Weight / Math.Pow(data.Hight / 100, 2.0); // 適正体重 = 身長(m) * 身長(m) * 22 var idealBodyWeight = Math.Pow(data.Hight / 100, 2.0) * 22; return new LambdaResponse { StatusCode = HttpStatusCode.OK, Headers = null, Body = JsonConvert.SerializeObject( new ResponseParam { Bmi = bmi, IdealBodyWeight = idealBodyWeight }) }; } } }
6. Lambdaの設定をする
作成したプログラムを AWS Lambda にアップロード前に設定を行います。
ほとんどの項目が自動的に入力されています。
Account Profile
とRegion
とFunction Name
の3つを設定した後に次に進んで下さい。
Account Profile
を間違って作成した場合など、情報を修正する場合はこちら
VisualStudioで設定したAWSのプロファイルを修正する
また、IAM
でグループやユーザーの作成を行っていない場合は、
任意のグループにAWSLambdaFullAccess
ポリシーをアタッチした後に、
任意のユーザーを作成して下さい。
※適切な権限割当についてはご自身で判断願います。
No | 項目名 | 必須? | 説明 |
---|---|---|---|
1 | Profile Name | 必須 | プロファイル名。 VisualStudio で表示される時の名前 |
2 | AccessKey | 必須 | アクセスキーID。 IAMで作成してコピペする。 |
3 | Secret Access Key | 必須 | シークレットアクセスキー。 IAMで作成してコピペする。 |
4 | Account Number | オプション | アカウントID。空でOK。 |
5 | Account Type | 必須 | アカウントタイプ。 基本は Standard AWS Account を選択する。 |
7. アップロードする
Role Name
とMemory(MB)
とTimeout(Secs)
の3つを選択してUpload
をクリックして下さい。
項目名 | 説明 |
---|---|
Memory(MB) | 最低の128MBで問題ありません。 次の節で紹介する Log をみて調整します。 |
Timeout(Secs) | 10秒に設定します。 初回起動時のビルドに時間がかかるので長めに設定します。 次の節で紹介する Log をみて調整します。 |
また、 IAM
でロールを作成していない場合は AWSLambdaFullAccess
ポリシーをアタッチしてロールを作成して下さい。
※適切な権限割当についてはご自身で判断願います。
※6 で設定したアカウントにロール作成権限もあれば、Role Name
コンボボックスから作成することも可能です。
8. Lambdaでテストする
作成した Lambdaをテストして結果を確認します。
また、画面下部に表示されているLogには、 実行時に使用されたメモリや実行時間が出力されています。 この結果を見てメモリやタイムアウトの設定を調整させます。
{ "body": "{\"height\": 168.0, \"weight\": 50.0}" }
実行結果
9. API Gatewayを追加する
AWS Lambda管理画面を開き、関数を選択した後にトリガーとしてAPI Gateway
を追加します。
※リンクは東京リージョンになっているので、海外リージョンでLambda関数を作成した場合は各自合わせて下さい。
10. HTTPでテストする
9 で作成したエンドポイントをテストします。
Content-Type
はapplication/json
です。
{ "hight" : 168.0, "weight" : 50.0 }
実行結果