LoginSignup
6
8

More than 3 years have passed since last update.

macOS で Azure CLI をインストールして Linux VM を起動する

Last updated at Posted at 2018-12-29

はじめに

エンジニアの @Yasuhisa です。

この記事は Azure の公式ドキュメントに沿って macOS に Azure CLI をインストールする方法を紹介します。

Azure CLI をマシンにインストールすることで、コマンドラインツールから Azure API を実行できます。

環境構成

  • macOS Mojave : バージョン10.14(18A391)
  • iTerm2 : Build 3.2.6
  • Homebrew 1.8.6 : Homebrew/homebrew-core (git revision 3d44; last commit 2018-12-28)
  • xcode-select version 2354.
  • Azure無料試用版プランをサブスクリプション済み

設定手順

Azure CLI のインストール

Azure CLI のインストールには Homebrew を使用します。事前に brew コマンドが実行できるように準備してください。

ターミナルでコマンドを入力します。

$ brew install azure-cli

インストールに成功したら次のコマンドを実行してください。もし Mac で使用している Python のバージョンがデフォルト(2系、2.x.x)の場合は、エラーが出力されます。

$ az
/usr/local/bin/az: line 6: python3: command not found

Azure CLI を使用するためには、Python3 を使用可能にする必要があります。

Python3 (pyenv) のインストール

筆者は複数の Python バージョンを利用したいので、pyenv を使用して Python3 をインストールしました。

$ brew install pyenv
...(省略)

$ pyenv install 3.7.0
...(省略)

$ pyenv versions
>* system (set by /Users/arakawa.yasuhisa/.pyenv/version)
>  3.7.0

$ pyenv global 3.7.0

$ python -V
> 3.7.0

Python のバージョンが切り替わらない場合は、以下の記事が参考になります。

ログインシェルで zshfish を利用している方は、~/.zshrc~/.config/fish/config.fish も忘れずに変更しましょう。

その後、再度 az コマンドを実行すると Welcome のメッセージとともに使用可能なコマンドの一覧が表示されます。

$ az

     /\
    /  \    _____   _ _  ___ _
   / /\ \  |_  / | | | \'__/ _\
  / ____ \  / /| |_| | | |  __/
 /_/    \_\/___|\__,_|_|  \___|


Welcome to the cool new Azure CLI!

Use `az --version` to display the current version.
Here are the base commands:

    account           : Manage Azure subscription information.
    acr               : Manage private registries with Azure Container Registries.
    acs               : Manage Azure Container Services.
    ad                : Manage Azure Active Directory Graph entities needed for Role Based Access
                       Control.
    advisor           : Manage Azure Advisor.
    aks               : Manage Azure Kubernetes Services.
    ams               : Manage Azure Media Services resources.
    appservice        : Manage App Service plans.
    backup            : Manage Azure Backups.
    batch             : Manage Azure Batch.
    batchai           : Manage Batch AI resources.
    billing           : Manage Azure Billing.
    bot               : Manage Microsoft Bot Services.
    cdn               : Manage Azure Content Delivery Networks (CDNs).
    cloud             : Manage registered Azure clouds.
    cognitiveservices : Manage Azure Cognitive Services accounts.
    configure         : Manage Azure CLI configuration. This command is interactive.
    consumption       : Manage consumption of Azure resources.
    container         : Manage Azure Container Instances.
    cosmosdb          : Manage Azure Cosmos DB database accounts.
    deployment        : Manage Azure Resource Manager deployments at subscription scope.
    disk              : Manage Azure Managed Disks.
    dla               : (PREVIEW) Manage Data Lake Analytics accounts, jobs, and catalogs.
    dls               : (PREVIEW) Manage Data Lake Store accounts and filesystems.
    dms               : Manage Azure Data Migration Service (DMS) instances.
    eventgrid         : Manage Azure Event Grid topics and subscriptions.
    eventhubs         : Manage Azure Event Hubs namespaces, eventhubs, consumergroups and geo
                       recovery configurations - Alias.
    extension         : Manage and update CLI extensions.
    feature           : Manage resource provider features.
    feedback          : Send feedback to the Azure CLI Team!
    find              : Find Azure CLI commands.
    functionapp       : Manage function apps.
    group             : Manage resource groups and template deployments.
    hdinsight         : Manage HDInsight clusters.
    identity          : Managed Service Identities.
    image             : Manage custom virtual machine images.
    interactive       : Start interactive mode. Installs the Interactive extension if not installed
                       already.
    iot               : Manage Internet of Things (IoT) assets.
    iotcentral        : Manage IoT Central assets.
    keyvault          : Manage KeyVault keys, secrets, and certificates.
    lab               : Manage Azure DevTest Labs.
    lock              : Manage Azure locks.
    login             : Log in to Azure.
    logout            : Log out to remove access to Azure subscriptions.
    managedapp        : Manage template solutions provided and maintained by Independent Software
                       Vendors (ISVs).
    maps              : Manage Azure Maps.
    mariadb           : Manage Azure Database for MariaDB servers.
    monitor           : Manage the Azure Monitor Service.
    mysql             : Manage Azure Database for MySQL servers.
    network           : Manage Azure Network resources.
    policy            : Manage resource policies.
    postgres          : Manage Azure Database for PostgreSQL servers.
    provider          : Manage resource providers.
    redis             : Manage dedicated Redis caches for your Azure applications.
    relay             : Manage Azure Relay Service namespaces, WCF relays, hybrid connections, and
                       rules.
    reservations      : Manage Azure Reservations.
    resource          : Manage Azure resources.
    role              : Manage user roles for access control with Azure Active Directory and service
                       principals.
    search            : Manage Azure Search services, admin keys and query keys.
    servicebus        : Manage Azure Service Bus namespaces, queues, topics, subscriptions, rules
                       and geo-disaster recovery configuration alias.
    sf                : Manage and administer Azure Service Fabric clusters.
    sig               : Manage shared image gallery.
    signalr           : Manage Azure SignalR Service.
    snapshot          : Manage point-in-time copies of managed disks, native blobs, or other
                       snapshots.
    sql               : Manage Azure SQL Databases and Data Warehouses.
    storage           : Manage Azure Cloud Storage resources.
    tag               : Manage resource tags.
    vm                : Manage Linux or Windows virtual machines.
    vmss              : Manage groupings of virtual machines in an Azure Virtual Machine Scale Set
                       (VMSS).
    webapp            : Manage web apps.

この記事の執筆時点(2018/12/28)では、70種類のサービスを操作できるコマンドがありました。 az コマンドが実行できることを確認したら Azure へサインインします。

$ az login

すると既定のブラウザが立ち上がり、サインイン画面が表示されます。ターミナルはそのままの状態で大丈夫です。

01.png

サインインしたいアカウントを選択するとブラウザで下記の画面が表示され、ターミナルに応答があります。

02.png

$ az login
Port '8400' is taken with error '[Errno 48] Address already in use'. Trying with the next one
Note, we have launched a browser for you to login. For old experience with device code, use "az login --use-device-code"
You have logged in. Now let us find all the subscriptions to which you have access...
[
  {
    "cloudName": "AzureCloud",
    "id": "***",
    "isDefault": true,
    "name": "無料試用版",
    "state": "Enabled",
    "tenantId": "***",
    "user": {
      "name": "***",
      "type": "user"
    }
  }
]

以上で Azure CLI のセットアップが完了しました。早速、Azure CLI でリソースの操作ができるかを試します。

Azure CLI を試す

クイック スタート - Azure CLI を使用して Linux VM を作成する を参考にして、Linux VM を起動します。

サンプルのコマンドがドキュメント内に記載されていますので、そのままコマンドラインにコピー & ペーストします。

$ az group create --name myResourceGroup --location eastus
{
  "id": "/subscriptions/**/resourceGroups/myResourceGroup",
  "location": "eastus",
  "managedBy": null,
  "name": "myResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null
}

$ az vm create \
  --resource-group myResourceGroup \
  --name myVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

- Running .. と出力され、しばらく待つと下記の出力があり Linux VM が起動しました。

{
  "fqdns": "",
  "id": "/subscriptions/***/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
  "location": "eastus",
  "macAddress": "***",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "***",
  "resourceGroup": "myResourceGroup",
  "zones": ""
}

Azure のポータルを確認しても、正常にリソースが作成されていることが確認できました。

03.png

確認後、不要であればリソースの削除を忘れないようにしてください。

以上で Azure CLI の動作確認は完了です。

Homebrew でインストールできる点や、日本語のドキュメント・チュートリアルが整っていた点が好印象でした。

6
8
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
6
8