AWS .NET deployment tool でも新しくなったデプロイ機能が使えるようになったようなので使ってみた

2022.07.09

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

いわさです。

先日、AWS Toolkit for Visual Studio の新しくなったデプロイ機能「Publish to AWS」をご紹介しました。
.NET Core 3.1 以上のワークロードをAWSへデプロイする際にとても簡単にデプロイすることが出来る機能です。

ただ、AWS Toolkit for Visual Studio は Visual Studio の 拡張機能であり、現時点では Visual Studio for Mac では利用することが出来ません。
.NET は今やクロスプラットフォーム環境での開発ツールなので、macOSで利用したいという方も多いのではないでしょうか。私はそうです。

実は、今回のデプロイ機能と同じレベルの機能が .NET CLI を使った AWS .NET deployment toolで利用することが出来ます。

実はこちら、以前に似た機能を紹介させて頂いたことがあります。

今回.NET CLIでも出来るようになったのはこの機能がバージョンアップされたものになっています。
本日はこちらの使い勝手を確認してみたいと思います。

インストール

今回は以前の古いaws.deploy.cliがインストール済みのmacOS環境でセットアップから行ってみます。

% dotnet-aws --version
AWS .NET deployment tool for deploying .NET Core applications to AWS.
Project Home: https://github.com/aws/aws-dotnet-deploy

0.26.4+935f319371

最初の注意点として、以前の記事のaws.deploy.cliがインストール済みだと、新しいaw.deploy.toolsがインストール出来ません。
そのため、一度アンインストールしています。

% dotnet tool update -g aws.deploy.tools
ツール 'aws.deploy.tools' を更新できませんでした。原因:
ツール 'aws.deploy.tools' でシェル shim を作成できませんでした: コマンド 'dotnet-aws' は、別のツールからの既存のコマンドと競合します。
ツール 'aws.deploy.tools' をインストールできませんでした。

:

% dotnet tool uninstall -g aws.deploy.cli
ツール 'aws.deploy.cli' (バージョン '0.26.4') は正常にアンインストールされました。
% dotnet tool install -g aws.deploy.tools 
次のコマンドを使用してツールを呼び出せます。dotnet-aws
ツール 'aws.deploy.tools' (バージョン '1.0.3') が正常にインストールされました。
% dotnet-aws --version
AWS .NET deployment tool for deploying .NET Core applications to AWS.
Project Home: https://github.com/aws/aws-dotnet-deploy

1.0.3+48ef3bc4ee

ヘルプはこんな感じになっています。
いくつかパラメータはあるのですが、基本的にはdotnet-aws deployするということだけ覚えておき、あとは対話形式でデプロイ内容を調整することが出来ます。

% dotnet aws deploy --help
AWS .NET deployment tool for deploying .NET Core applications to AWS.
Project Home: https://github.com/aws/aws-dotnet-deploy

deploy:
  Inspect, build, and deploy the .NET project to AWS using the recommended AWS service.

Usage:
  dotnet-aws deploy [options]

Options:
  --profile <profile>                          AWS credential profile used to make calls to AWS.
  --region <region>                            AWS region to deploy the application to. For example, us-west-2.
  --project-path <project-path>                Path to the project to deploy. [default: /Users/iwasa.takahito/work/hoge0709dotnet]
  --application-name <application-name>        Name of the cloud application. If you choose to deploy via CloudFormation, this name will be used to identify the CloudFormation
                                               stack.
  --apply <apply>                              Path to the deployment settings file to be applied.
  -d, --diagnostics                            Enable diagnostic output.
  -s, --silent                                 Disable interactivity to execute commands without any prompts for user input.
  --deployment-project <deployment-project>    The absolute or relative path of the CDK project that will be used for deployment
  -?, -h, --help                               Show help and usage information

ASP.NET Core アプリ

まずは ASP.NET Core アプリ をデプロイしてみたいと思います。
dotnet newで新しくテンプレートから初期作成し、Viewの文言だけ変更しています。

% dotnet new mvc
テンプレート "ASP.NET Core Web App (Model-View-Controller)" が正常に作成されました。
このテンプレートには、Microsoft 以外のパーティのテクノロジが含まれています。詳しくは、https://aka.ms/aspnetcore/6.0-third-party-notices をご覧ください。

:

% sudo vi Views/Home/Index.cshtml 
% cat Views/Home/Index.cshtml 
@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome iwasa!!</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

では、dotnet aws deployを試してみます。
試してみるとまず最初にわかるのが、前回の AWS Toolkit for Visual Studio のデプロイオプションと全く同じということです。
Elastic Beanstalk (Linux or Windows)、 ECS on Fargate、 App Runner から選択出来ます。

今回は Elastic Beanstalk へデプロイしてみました。

% dotnet aws deploy --profile hoge
AWS .NET deployment tool for deploying .NET Core applications to AWS.
Project Home: https://github.com/aws/aws-dotnet-deploy

Configuring AWS Credentials from Profile hoge.
Configuring AWS region using AWS SDK region search to ap-northeast-1.
Recommended Deployment Option
-----------------------------
1: ASP.NET Core App to AWS Elastic Beanstalk on Linux
This ASP.NET Core application will be built and deployed to AWS Elastic Beanstalk on Linux. Recommended if you want to deploy your application directly to EC2 hosts, not as a container image.

Additional Deployment Options
------------------------------
2: ASP.NET Core App to AWS Elastic Beanstalk on Windows
This ASP.NET Core application will be built and deployed to AWS Elastic Beanstalk on Windows. Recommended if you do not want to deploy your application as a container image.

3: ASP.NET Core App to Amazon ECS using AWS Fargate
This ASP.NET Core application will be deployed to Amazon Elastic Container Service (Amazon ECS) with compute power managed by AWS Fargate compute engine. If your project does not contain a Dockerfile, it will be automatically generated, otherwise an existing Dockerfile will be used. Recommended if you want to deploy your application as a container image on Linux.

4: ASP.NET Core App to AWS App Runner
This ASP.NET Core application will be built as a container image on Linux and deployed to AWS App Runner, a fully managed service for web applications and APIs. If your project does not contain a Dockerfile, it will be automatically generated, otherwise an existing Dockerfile will be used. Recommended if you want to deploy your web application as a Linux container image on a fully managed environment.

Choose deployment option (recommended default: 1)

ただし、今回のツールでも AWS Toolkit for Visual Studio と同じで、AWS CDKベースになるため、Node.jsのバージョン要件を満たす必要があります。
私の環境では Node.js バージョンが古かったため以下のエラーが発生しました。

Install Node.js 14.17.0 or later and restart your IDE/Shell. The latest Node.js LTS version is recommended. This deployment option uses the AWS CDK, which requires Node.js version higher than your current installation (14.15.4). 
You can install the missing Node.js dependency from: https://nodejs.org/en/download/

:

For more information, please visit our troubleshooting guide https://aws.github.io/aws-dotnet-deploy/troubleshooting-guide/.
If you are still unable to solve this issue and believe this is an issue with the tooling, please cut a ticket https://github.com/aws/aws-dotnet-deploy/issues/new/choose.
% node --version
v14.15.4

Node.jsをアップデートし、やり直します。

Current settings (select number to change its value)
----------------------------------------------------
1. Elastic Beanstalk Application: hoge0709dotnet
2. Environment Name: 
    Environment Name: hoge0709dotnet-dev
3. Environment Type: SingleInstance
4. Application IAM Role: *** Create new ***
5. Service IAM Role: *** Create new ***
6. Environment Variables: 

Enter 'more' to display Advanced settings. 
Or press 'Enter' to deploy:


******************************
Creating deployment zip bundle
------------------------------
Using the dotnet CLI build the project and zip the publish artifacts.
******************************

:

********************
AWS Resource Details
********************

Application Endpoint:
    Id: hoge0709dotnet-dev
    Type: AWS::ElasticBeanstalk::Environment
    Endpoint: http://hoge0709dotnet-dev.eba-kucdpm2c.ap-northeast-1.elasticbeanstalk.com/

今度は無事デプロイに成功しました。
リソース出力としてエンドポイントURLが出力されているのでアクセスしてみましょう。

デプロイされたWebアプリケーションへアクセスすることが出来ました。
また、以下のように Elastic Beanstalk リソース一式が作成されていることも確認出来ました。

CDKによって CloudFormation スタックが作成されているので、削除時は CloudFormation から対象スタックを削除することが出来ます。
逆に、CloudFormation スタックを削除してしまうと Elastic Beanstalk リソースも削除されてしまうので、ご注意ください。必要に応じてスタックの削除保護もしておきましょう。

コンソールアプリ

もうひとつ、続いてコンソールアプリもやってみます。
こちらも前回と同様に、シンプルなHelloWorld出力するだけのアプリケーションになっています。

% dotnet new console
テンプレート "コンソール アプリ" が正常に作成されました。

作成後の操作を処理しています...
/Users/iwasa.takahito/work/hoge0709dotnet2/hoge0709dotnet2.csproj で ' dotnet restore ' を実行しています...
  復元対象のプロジェクトを決定しています...
  /Users/iwasa.takahito/work/hoge0709dotnet2/hoge0709dotnet2.csproj を復元しました (79 ms)。
正常に復元されました。

% cat Program.cs 
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

デプロイオプションはやはり AWS Toolkit for Visual Studio と同じようで、サービスかスケジュールタスクの2択でどちらも ECS on Fargate です。

% dotnet aws deploy --profile hoge
AWS .NET deployment tool for deploying .NET Core applications to AWS.
Project Home: https://github.com/aws/aws-dotnet-deploy

Configuring AWS Credentials from Profile hoge.
Configuring AWS region using AWS SDK region search to ap-northeast-1.
Recommended Deployment Option
-----------------------------
1: Service on Amazon Elastic Container Service (ECS) using AWS Fargate
This .NET Console application will be built using a Dockerfile and deployed as a service to Amazon Elastic Container Service (Amazon ECS) with compute power managed by AWS Fargate compute engine. If your project does not contain a Dockerfile it will be automatically generated, otherwise an existing Dockerfile will be used. Recommended if you want to deploy a service as a container image on Linux.

Additional Deployment Options
------------------------------
2: Scheduled Task on Amazon Elastic Container Service (ECS) using AWS Fargate
This .NET Console application will be built using a Dockerfile and deployed as a scheduled task to Amazon Elastic Container Service (Amazon ECS) with compute power managed by AWS Fargate compute engine. If your project does not contain a Dockerfile it will be automatically generated, otherwise an existing Dockerfile will be used. Recommended if you want to deploy a scheduled task as a container image on Linux.

Choose deployment option (recommended default: 1)

:

スケジュールタスクをデプロイしてみましょう。
ECSがデプロイ先ですが、DockerFileも自動で作成されました。

:

Enter value (default rate(1 hour). Type <reset> to reset.): 
rate(1 minute)

Current settings (select number to change its value)
----------------------------------------------------
1. ECS Cluster: hoge0709dotnet2
2. Application IAM Role: *** Create new ***
3. Task Schedule: rate(1 minute)
4. Virtual Private Cloud (VPC): *** Default ***
5. Environment Variables: 
6. ECR Repository Name: hoge0709dotnet2

Enter 'more' to display Advanced settings. 
Or press 'Enter' to deploy:


*************************
Creating deployment image
-------------------------
Using the docker CLI to perform a docker build to create a container image.
*************************

Generating Dockerfile...

:

上記ではスケジュール式のみ変更しています。
CloudWatch Logs から ECSリソース名でロググループを検索すると、以下のように1分置きの出力が確認出来ています。良いですね。

リソースを確認してみると、こちらも前回と同様に EventBridgeでタスクスケジュールが構成されていました。

さいごに

本日は AWS .NET deployment tool の新しいデプロイ機能を試してみました。

個人的には App2Container と少しかぶってる部分があるので棲み分けが気になるところです。
.NET Core 3.1以上かつAWSがデプロイ先であればこちらを使って良い気がしています。AWS以外がデプロイ先だったり、.NET Frameworkの場合は App2Container を替わりに使うのが良さそうかなと。

あと、SAM CLIもそうなのですが、コマンドの細かいところやオプションなど気にせずにとりあえず dotnet aws deploy だけすれば、あとは対話でどうにかなるパターンなので学習コスト低く使い始めれるのがありがたいです。