😎

Azure Developer CLI に入門する

2024/07/26に公開

ごきげんよう mihohoi です。Azure Developer CLI 使ってますか?私は知っていたものの、まだ実際に試せていませんでした。
今回ドキュメントを読み漁っていて「何でこんないいもの使わなかったんだ!」と自分にビンタしたい気持ちがあふれ出てきたので、学習ログという意味合いも含めて記事をしたためました。
なお、環境は Mac です。

まずドキュメントを見て、Azure Developer CLI のコマンドのイメージを描いてみました。
ファイルの構成としては、アプリを入れるための src フォルダ、テストコードを入れる tests フォルダ、インフラ情報を入れる infra フォルダがあり、それらをまとめる azure.yaml があります。
azd イメージ図

インストール方法

まずは使ってみよう、ということでインストールします。以下のコマンドでインストール完了です。

% brew tap azure/azd && brew install azd

azd コマンドが使用できるかを確認をするためにバージョンを確認します。

% azd version
azd version 1.9.5 (commit cd2b7af9995d358aab33c782614f801ac1997dde)

今回実施する内容

以下のクイックスタートを実施しながら中身を色々見ていきます。

■ クイックスタート
Azure Developer CLI の使用を開始する | Microsoft Learn

■ 前提条件に記載されている Node.js のサンプル
todo-nodejs-mongo/README.md at main · Azure-Samples/todo-nodejs-mongo (github.com)

プロジェクトを初期化する

init を使用してプロジェクトを初期化します。その際に今回使用する「テンプレート」を指定します。
テンプレートは環境を作る元、という理解です。アプリやインフラを作成するコード一式が入っています。

azd init --template todo-nodejs-mongo

template でどうやって作成するプロジェクトを指定するの?と思ったので、コマンドの help を確認しました。
どうやらプロジェクトの URI を指定するかすれば良さそうです。
今回は Azure Samples · GitHub 配下のアプリのため、リポジトリ名を指定するだけで大丈夫でした。

azd init のヘルプを確認します。template の他に locationsubscription なども指定できるようです。

% azd init -h

Initialize a new application in your current directory.

  • Running init without flags specified will prompt you to initialize using your existing code, or from a template.
  • To view all available sample templates, including those submitted by the azd community, visit: https://azure.github.io/awesome-azd.

Usage
  azd init [flags]

Flags
    -b, --branch string       	: The template branch to initialize from. Must be used with a template argument (--template or -t).
        --docs                	: Opens the documentation for azd init in your web browser.
    -e, --environment string  	: The name of the environment to use.
    -f, --filter strings      	: The tag(s) used to filter template results. Supports comma-separated values.
        --from-code           	: Initializes a new application from your existing code.
    -h, --help                	: Gets help for init.
    -l, --location string     	: Azure location for the new environment
    -s, --subscription string 	: Name or ID of an Azure subscription to use for the new environment
    -t, --template string     	: Initializes a new application from a template. You can use Full URI, <owner>/<repository>, or <repository> if it's part of the azure-samples organization.
()

プロジェクトを作成

では、改めて実施します。

% mkdir todo-nodejs-mongo
% cd todo-nodejs-mongo
% azd init --template todo-nodejs-mongo

Initializing an app to run on Azure (azd init)

  () Done: Initialized git repository
  () Done: Downloading template code to: /Users/miho/develop/Local/todo-nodejs-mongo

? Enter a new environment name: todo-mihohoi

SUCCESS: New project initialized!
You can view the template code in your directory: /Users/miho/develop/Local/todo-nodejs-mongo
Learn more about running 3rd party code on our DevHub: https://aka.ms/azd-third-party-code-notice

フォルダを作成し、移動した後にテンプレートを指定して実行します。すぐにローカルにダウンロードされました。
ls で確認すると、プロジェクトファイルが作成されているのがわかります。

% ls -l
total 3568
-rw-r--r--  1 miho  staff     1077  7 25 11:08 LICENSE
-rwxr-xr-x  1 miho  staff  1801462  7 25 11:08 NOTICE.txt
-rw-r--r--  1 miho  staff     7864  7 25 11:08 README.md
drwxr-xr-x  6 miho  staff      192  7 25 11:08 assets
-rw-r--r--  1 miho  staff     1510  7 25 11:08 azure.yaml
drwxr-xr-x  7 miho  staff      224  7 25 11:08 infra
-rw-r--r--  1 miho  staff     8165  7 25 11:08 openapi.yaml
drwxr-xr-x  4 miho  staff      128  7 25 11:08 src
drwxr-xr-x  8 miho  staff      256  7 25 11:08 tests

先ほど指定した「環境名」はどこに保存されるんだろうか、確認してみましょう。

% ls -l .azure
total 8
-rw-r--r--  1 miho  staff   49  7 25 11:08 config.json
drwxr-xr-x  4 miho  staff  128  7 25 11:08 todo-mihohoi

% cat .azure/todo-mihohoi/.env
AZURE_ENV_NAME="todo-mihohoi"

なるほど、.azure フォルダに保存されていました。.env ファイルに指定した環境名が保存されているようです。
この「環境名」は、Azure リソースグループ名の接頭辞としても使われるそうです。
あまり長い名前は良くなさそうですし、dev や test などを設定するのが良さそうです。

Azure Developer CLI FAQ | Microsoft Learn

では Azure 環境にログインし、実行します。途中で対象のサブスクリプションとリージョン ロケーションを聞かれるので選択します。

% azd auth login
Logged in to Azure.

% azd up
  () Done: Downloading Bicep
? Select an Azure Subscription to use:  2. Visual Studio Enterprise Subscription (xxxxxxxxxxxxxxxxxxxxxx)
? Select an Azure location to use:  9. (Asia Pacific) Japan East (japaneast)
Note: Running custom 'up' workflow from azure.yaml

Provisioning Azure resources (azd provision)
Provisioning Azure resources can take some time.

Subscription: Visual Studio Enterprise Subscription (xxxxxxxxxxxxxxxxxxxxxx)
Location: Japan East

  You can view detailed progress in the Azure Portal:
  

  () Done: Resource group: rg-todo-mihohoi
  () Done: App Service plan: plan-aitjaq62jxfn2
  () Done: Key Vault: kv-aitjaq62jxfn2
  () Done: Log Analytics workspace: log-aitjaq62jxfn2
  () Done: Application Insights: appi-aitjaq62jxfn2
  () Done: Portal dashboard: dash-aitjaq62jxfn2
  () Done: Azure Cosmos DB: cosmos-aitjaq62jxfn2
  () Done: App Service: app-web-aitjaq62jxfn2
  () Done: App Service: app-api-aitjaq62jxfn2

Deploying services (azd deploy)

  () Done: Deploying service api
  - Endpoint: https://app-api-aitjaq62jxfn2.azurewebsites.net/

  () Done: Deploying service web
  - Endpoint: https://app-web-aitjaq62jxfn2.azurewebsites.net/


SUCCESS: Your up workflow to provision and deploy to Azure completed in 12 minutes 17 seconds.

かかった経過時間まで書いてくれている…これはうれしい機能!!

Azure portal を確認すると、リソースグループまとめて作成されていました。
Azure portal

App Service も確認すると、アプリもデプロイされていました。
デプロイされた TODO アプリ

削除

一通り、どんな動きをするかが確認できたので、作成されたリソースを削除します。

% azd down

Deleting all resources and deployed code on Azure (azd down)
Local application code is not deleted when running 'azd down'.

  Resource group(s) to be deleted:

    • rg-todo-mihohoi: https://portal.azure.com/#@/resource/subscriptions/xxxxxxxxxxxx/resourceGroups/rg-todo-mihohoi/overview

? Total resources to delete: 9, are you sure you want to continue? Yes
Deleting your resources can take some time.

  () Done: Deleting resource group: rg-todo-mihohoi

  Warning: The following operation will delete 1 Key Vault.
These resources have soft delete enabled allowing them to be recovered for a period or time after deletion. During this period, their names may not be reused. In the future, you can use the argument --purge to skip this confirmation.

? Would you like to permanently delete these resources instead, allowing their names to be reused? Yes

  () Done: Purging Key Vault: kv-aitjaq62jxfn2

SUCCESS: Your application was removed from Azure in 27 minutes 47 seconds.

削除は思ったよりも時間がかかりました。

まとめ

冒頭の図でも記載していますが、azd up でアプリとインフラ、deploy でアプリ、provision でインフラと使い分けることが出来るので、まずはインフラだけ作っておいて、後でアプリを作ろうという方法もとれそうです。本末転倒ですけど。
とにかくアプリと一緒にデプロイできるというのが良いですね。サンプルファイルを配布するときに大いに役立ちそうです。

ただ、別の悩みもありそうなので、そこはこれから作っていくときに悩んでいこうと思います。
https://twitter.com/07JP27/status/1816007804811149364

参考

https://learn.microsoft.com/ja-jp/azure/developer/azure-developer-cli/install-azd?tabs=winget-windows%2Cbrew-mac%2Cscript-linux&pivots=os-mac

GitHubで編集を提案
Microsoft (有志)

Discussion