Azure Developer CLIをAzure Cloud Shellから使ってみる

2023/08/12に公開

はじめに

この記事では Azure Developer CLIを学習していく記事です。主な内容としては実践したときのメモを中心に書きます。(忘れやすいことなど)
誤りなどがあれば書き直していく予定です。なお、内容につきましては2023年8月12日時点の調査内容で記載しております。あらかじめご了承ください。

Azure Developer CLIとは

それではさっそく Azure Developer CLIとは何かを公式を引用してみていきます。

Azure Developer CLI (azd) は、ローカル開発環境から Azure へのアプリケーションの取得にかかる時間を短縮するオープンソース ツールです。 azd では、ターミナル、エディター、統合開発環境 (IDE)、CI/CD (継続的インテグレーション/継続的デプロイ) のいずれで作業しているかに関係なく、ワークフローの主要なステージにマップされる開発者向けのベスト プラクティスのコマンドが提供されます。
Azure でアプリケーションを起動して実行するために必要なすべてのものを含む拡張可能なブループリント テンプレートを使用azdできます。 これらのテンプレートには、コード資産としての再利用可能なインフラストラクチャと、独自のアプリ コードに置き換えることができる概念実証アプリケーション コードが含まれます。 独自のテンプレートを作成したり、ビルドするテンプレートを見つけたりすることもできます。

公式:Azure Developer CLIとは

ざっくりというとAzureのリソースを管理するためのCLIツールです。Azureのリソースを管理するためのCLIツールというとAzure CLIがあります。

Azure CLIはAzureのリソースを管理するためのツールですが、 Azure Developer CLI(azd)は開発者の為のAzureソース管理ツールです。

なお、Azure CLIがインストールされていない状態でも Azure Developer CLI(azd)はインストール可能であり、Azure CLIとの併用も可能です。
もちろん、 Azure Developer CLI(azd)だけ利用することも可能です。

インストール方法

とてもありがたいことに公式でインストール方法について記載があります。

公式:Azure Developer CLIのインストール方法

インストール方法としてはGitHubからインストールする方法がありますが、今回はwingetからインストールする方法でインストールして利用します。ターミナルはPowrShellを利用します。

winget install microsoft.azd

インストールが完了すると日本語で以下のようなメッセージが表示されます。

見つかりました Azure Developer CLI [Microsoft.Azd] バージョン 1.2.100
このアプリケーションは所有者からライセンス供与されます。
Microsoft はサードパーティのパッケージに対して責任を負わず、ライセンスも付与しません。
ダウンロード中 https://github.com/Azure/azure-dev/releases/download/azure-dev-cli_1.2.0/azd-windows-amd64.msi
インストーラーハッシュが正常に検証されました
パッケージのインストールを開始しています...
インストールが完了しました

インストール後はターミナルを再起動するか新しく起動する必要があります。もしくはプロファイルを再読み込みする必要があります。

※インストールした設定を反映させるため
※VS Codeの場合は再起動する必要があります。

helpでサブコマンド見てみる

インストールが完了したら早速helpを見てみます。ぱっと見ではまだまだBetaが多いですが、単純なデプロイはできそうです。

azd --help

The Azure Developer CLI is an open-source tool that helps onboard and manage your application on Azure

Usage
  azd [command]

Commands
  Configure and develop your app
    auth        : Authenticate with Azure.
    config      : Manage azd configurations (ex: default Azure subscription, location).
    init        : Initialize a new application.
    restore     : Restores the application's dependencies. (Beta)
    template    : Find and view template details. (Beta)

  Manage Azure resources and app deployments
    deploy      : Deploy the application's code to Azure.
    down        : Delete Azure resources for an application.
    env         : Manage environments.
    package     : Packages the application's code to be deployed to Azure. (Beta)
    provision   : Provision the Azure resources for an application.
    up          : Provision Azure resources, and deploy your project with a single command.

  Monitor, test and release your app
    monitor     : Monitor a deployed application. (Beta)
    pipeline    : Manage and configure your deployment pipelines. (Beta)

  About, help and upgrade
    version     : Print the version number of Azure Developer CLI.

Flags
    -C, --cwd string    : Sets the current working directory.
        --debug         : Enables debugging and diagnostics logging.
        --docs          : Opens the documentation for azd in your web browser.
    -h, --help          : Gets help for azd.
        --no-prompt     : Accepts the default value instead of prompting, or it fails if there is no default.

Use azd [command] --help to view examples and more information about a specific command.

Deploying a sample application
Initialize from a sample application by running the azd init --template [template name] command in an empty directory.
Then, run azd up to get the application up-and-running in Azure.

To view a curated list of sample templates, run azd template list.
To view all available sample templates, including those submitted by the azd community, visit: https://azure.github.io/awesome-azd.

Find a bug? Want to let us know how we're doing? Fill out this brief survey: https://aka.ms/azure-dev/hats.

バージョンを確認する

バージョンを確認することもできるようなので確認してみます。

azd version

実行結果

azd version 1.2.0 (commit 99ea7577f0df0df2ba34b677da189fafba18c0f7)

version はハイフンなしで実行できるようです。個人的にはとても良きポイントです。

Azure にログインする

認証はAzure CLIと同じように行います。ログインを実行するとブラウザが起動して認証画面が表示されます。

azd auth login

実行結果

Logged in to Azure.

デバイスコードを使ってログインする場合は以下のように実行します。

 azd auth login --use-device-code

Azure Cloud Shellでazdを利用する

ローカルでデプロイしても良いですが、ここではデプロイするにあたってAzure Cloud Shellでazdを利用する方法を紹介します。
Azure PortalからAzure Cloud Shellを起動します。

portal.png

バージョンを確認します。

azd version

実行結果

azd version 1.1.0 (commit ea9cb12575734ee6a5f99c4d415c1a51d6f32d3e)

以下のコマンドを実行するとAzure Cloud Shellでazdを利用できます。

azd init --template todo-csharp-cosmos-sql

デプロイ

デプロイは以下のコマンドを実行します。なお、デプロイには時間がかかる可能性があるため、時間に余裕があるときにやりましょう。

azd up

Bicepのダウンロードがはじまり、デプロイ作業が自動で進行します。
自動でリソースグループを作成したあと以下のサービスが構成されます。

  • App Service plan
  • Log Analytics workspace
  • Key Vault
  • Application Insights
  • Portal dashboard
  • App Service
  • Azure Cosmos DB

? Select an Azure Subscription to useが聞かれたら、サブスクリプションと1. Pay-As-You-Goを選択します。
? Select an Azure location to use: では9. (Asia Pacific) Japan East (japaneast)を選択します。

長い実行結果の後に以下の内容が表示されます。

Deploying services (azd deploy)

  |====== | Deploying service api (Uploading deployment package)
  (✓) Done: Deploying service api
  - Endpoint: https://XXXXX.azurewebsites.net/

  (✓) Done: Deploying service web
  - Endpoint: https://XXXXX.azurewebsites.net/

SUCCESS: Your application was provisioned and deployed to Azure in 11 minutes 19 seconds.

※11分19秒でデプロイが完了しました。

アプリを触ってみる

デプロイが完了すると以下2つのURLが表示されます。

  • ToDo APIアプリ
  • ToDo Webアプリ フロントエンド

ToDo Web アプリ フロントエンド

ToDo Webアプリ フロントエンドと記載されている方のURLから画面を開いてデータを登録してみます。
以下のような画面が表示されます。

todo-web.png

Add an itemでアイテムを登録します。アイテム名を入力してSaveをクリックします。

todo-web.png

ToDo API アプリ

エンドポイントが公開されてcurlなどで実行できるようになります。先ほど登録したToDoリストを取得してみます。

curl https://XXXXX.azurewebsites.net/lists

[{"id":"{id}","name":"My List","description":null,"createdDate":"2023-08-12T12:47:47.6751982+00:00","updatedDate":null}]

クリーンアップ

デプロイしたリソースを削除するには以下のコマンドを実行します。

azd down

y/Nを聞かれるのでyを入力します。

まとめ

今回はazdのインストールからデプロイまでを紹介しました。

  • Azure Developer CLI(azd)は開発者の為のAzureソース管理ツールである
  • Azure CLIと同じように使えて、Azureのリソースを管理できる
  • 利用する時はAzure CLIは不要であり、併用も可能である
  • インストールはwingetから可能
    • 他にもインストール方法があるが、今のところすぐに使いたい人はwingetを使うのが良い
  • azdを使うことで、Azureのリソースを簡単にデプロイできる
    • 初期化してupするだけで、Azureのリソースが作成される
    • クリーンアップはdownで実行できる

参考

Discussion