LoginSignup
1
1

More than 1 year has passed since last update.

AWS LambdaにC#プロジェクトをデプロイする

Posted at

概要

AWS LambdaにC#プロジェクトをデプロイする場合、
Windows版のVisual StudioであればGUIからボタンぽちぽちで完了するので良いのですが(要AWS Toolkit for Visual Studio)、
Visual Studio for Macの場合はできません。
そのため、Macの場合はCUIからデプロイする必要があるので、本記事でその方法についてまとめておきます。

事前準備

●AWS CLIをインストール

brew install awscli

●Amazon.Lambda.Tools .NET Core Global Tool
.NETアプリケーションをAWS Lambdaへデプロイするために必要です。

dotnet tool install -g Amazon.Lambda.Tools

●プロファイル作成
ローカルPCからAWSへCLIで操作を行う場合、都度「プロファイル」を指定して接続する形になります。
※あらかじめAWSでCLIで使用するためのIAMを設定し、AccessKey及びSecretAcccessKeyを取得しておきます。

aws configure --profile {プロファイル名}
# 例: aws configure --profile user1

AWS Access Key ID: {設定したIAMから払い出されたキーをコピー}
AWS Secret Access Key: {同上}
Default region name: {東京の場合は ap-northeast-1 を指定}
Default output format: JSON

C#プロジェクトの準備

●設定ファイル
デプロイコマンドを実行する前に、C#プロジェクトに
「どのリージョン」に「どのプロファイル」を使用して、「どのLambda関数」にデプロイする、等といった設定ファイルをJSON形式で用意しておく必要があります。

ファイル名: aws-lambda-tools-defaults.json
階層: 「.csproj」ファイルと同階層に配置

{
  "Information": [
    "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
    "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
    "dotnet lambda help",
    "All the command line options for the Lambda command can be specified in this file."
  ],
  "configuration": "Release",
  "framework": "netcoreapp3.1",
  "profile": "default", // AWS CLI プロフィール
  "region": "ap-northeast-1", // AWSリージョン指定
  "function-runtime": "dotnetcore3.1", // Lambbda実行ランタイム
  "function-memory-size": 512, // Lambdaメモリ(MB)
  "function-timeout": 30, // Lambdaタイムアウト時間(seconds)
  "function-handler": "SearchConnpassLineBot::SearchConnpassLineBot.Function::FunctionHandler", // Lambda実行時にコールするメソッドを指定
  "function-name": "SearchConnpassLineBot", // Lambda名称
  "function-role": "arn:aws:iam::000000000000:role/MyLambdaRole" // AWSロール名
}

デプロイ方法

●デプロイコマンド
簡単です。
デプロイしたい「.csproj」ファイルがある改装で以下コマンドを実行します。

dotnet lambda deploy-function

以上でデプロイ完了です!
お疲れ様でした!

1
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
1
1