LoginSignup
1
1

More than 3 years have passed since last update.

.NET Core Serverless WebAppをMacからAWSへデプロイ

Posted at

概要

タイトル通りですが、.NET Core Serverless WebAppをMacからAWSへデプロイします。
WindowsでしたらAWS ToolKit for VisualStudioがあるのでGUIでポチポチできますが、
AWS ToolKit for VisualStudio(Mac ver)が無いので、
Macからデプロイするためにはコマンドでなんとかかんとかがんばる必要があります。

必要ツールインストール

それではまず事前準備で必要なツールをインストールします。

●AWS CLI
AWSへデプロイするのでもちろん必要になってきます。

brew install awscli

●.NETアプリケーションを操作するために必要です。

brew install dotnet-sdk

●Amazon.Lambda.Templates
テンプレートからプロジェクトを作成することができます。
便利なので入れておきます。

dotnet new -i Amazon.Lambda.Templates

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

dotnet tool install -g Amazon.Lambda.Tools

AWS CLIプロファイル設定

続いてAWS CLIのプロファイル設定を行います。
ターミナルからAWSへデプロイする際に使用するAWS Accesskey等の設定です。
あらかじめAWSでIAMを作成しておきます。

●プロファイル作成

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

プロジェクト作成

それでは準備が整ったので、プロジェクトを作成します。
Amazon.Lambda.Templatesをインストールしたので、テンプレートから作成します。
プロジェクトを作成したい任意の場所で以下コマンドを実行します。
※WindowsのVisualStudioだとあらかじめ組み込まれています。

dotnet new serverless.AspNetCoreWebApp --name {Lambda関数名} --region {リージョン名} --profile {プロファイル名}
# 例: dotnet new serverless.AspNetCoreWebApp --name MyWebApp --region ap-northeast-1 --profile user1

少し待つと、プロジェクトフォルダが作成されます。
VisualStudio for Macで.csprojを開いてそのままデバッグするとサンプルページが表示されます。

デプロイ

ではプロジェクトの作成ができたのでいよいいよAWSへデプロイします。
ただ、その前に自動生成されたプロジェクト内の「aws-lambda-tools-defaults.json」ファイルにアップロード先S3バケット名とCloudFormationのスタック名を追記しておきます。

{
  "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."
  ],
  "profile": "user1",
  "region": "ap-northeast-1",
  "configuration": "Release",
  "framework": "netcoreapp3.1",
  "s3-prefix": "MyAspNetApp/",
  "template": "serverless.template",
  "template-parameters": "",
  "s3-bucket": {あらかじめAWSにS3を作成しておく必要があります。},
  "stack-name": {AWS CloudFormationのスタック名を指定します。S3と違い自動作成してくれます。}
}

それでは、準備万端なのでデプロイコマンドを実行します。
※以下コマンドは.csprojと同階層で実行して下さい。

dotnet lambda deploy-serverless

無事デプロイが完了すると、こんな感じでデプロイ先URLが表示されます。

ApiURL  https://xxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/

やった!お疲れ様でした!

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