5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Azure】Template specsを用いてストレージアカウントを作成する。

Posted at

1. はじめに

1-1 ご挨拶

初めまして、井村と申します。
お仕事でTemplate specsを利用することになりました。初めて触るリソースの為、動作確認と備忘録として投稿します。

1-2 対象読者

  • Azureに興味がある

1-3 Template specsとは?

Azure上にARMテンプレートを格納するサービスです。ARMテンプレートはAzureで利用できるIaCです。Template specsを利用するメリットとしてはAzure内で他組織へ共有できる、Azure RBACで適切な権限を付与して運用できる、バージョン管理ができる点が挙げられます。
※Bicepでもok!

1-4 システム構成と検証の流れ

template-spec.png

上記がシステム構成図です。左がTemplate specs、右がストレージアカウントです。今回はそれぞれにリソースグループを作成します。

mslearnよりワークフロー図を拝借します。

re.png

  1. ローカルでARMテンプレートを作成。
  2. Template specsを作成
  3. Template specsからストレージアカウントをデプロイ

となります。本記事は上記に加え、エクスポート、バージョンアップも検証します。

2. 構築

2-1 ARMテンプレートの作成

ARMテンプレートは以下コードになります。ストレージアカウントを作成するものになります。デスクトップ上に作業フォルダとして「ts」フォルダを作成しARMテンプレートを格納します。

mainTemplate.json
mainTemplate.json

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS",
        "Premium_LRS"
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[concat('stimura', uniquestring(resourceGroup().id))]",
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      }
    }
  ]
}

2-2 リソースグループの作成

以下コマンドでリソースグループを作成します。Azure CLIを本項番以降でも度々利用していますが、変数は宣言している前提となります。

Azure CLI
# 変数宣言
tsName="createStorageAccount"
rgSou="rg-ts-imura"
rgDes="rg-imura"
lc="japaneast"

# Azureへログイン
az login

# template specs保管用リソースグループの作成
az group create --name $rgSou --location $lc

# ストレージアカウント保管用リソースグループの作成
az group create --name $rgDes --location $lc

正常終了後、Auzre上にリソースグループが作成されます。

リソースグループ.png

2-3 Template specsの作成

以下コマンドでAzure上にTemplate specsを作成します。

Azure CLI
# template specsの作成
az ts create --name $tsName --version "1.0" --resource-group $rgSou --location $lc --template-file "./mainTemplate.json" --version-description "ストレージアカウント作成" --display-name "ログ格納用ストレージアカウント"

正常終了後、Azure上にTemplate specsが作成されます。

1_deploy.png

左ペインのバージョンを押すと以下画面に遷移します。

2_v1.png

2-4 ストレージアカウントの作成

以下コマンドでAzure上のTemplate specsを用いてストレージアカウントをデプロイします。
Template specsを利用するためにはリソースIDを指定する必要があります。その為、事前にリソースIDを取得し、利用します。

Azure CLI
# template specsのリソースIDの取得およびデプロイ
id=$(az ts show --name $tsName --resource-group $rgSou --version "1.0" --query "id")

az deployment group create --resource-group $rgDes --template-spec $id

正常終了後、Azure上にストレージアカウントが作成されます。

3_st.png

3. 検証

それでは検証を始めます。Azure上のTemplate specsはバージョン1.0で作成しています。バージョン1.0をローカル(構築とは違う作業フォルダ)にダウンロードし、ARMテンプレートを修正。バージョン2.0としてTemplate specsを作成します。

3-1 ARMテンプレートのダウンロードおよび修正

作業用フォルダ「new_ts」にARMテンプレートをダウンロードします。ダウンロードしたARMテンプレートを修正します。修正内容はストレージアカウントにタグを追加しています。

Azure CLI
# template specsのエクスポート
az ts export --output-folder /c/Users/user/Desktop/new_ts --name $tsName --resource-group $rgSou --version 1.0

mainTemplate.json
mainTemplate.json

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS",
        "Premium_LRS"
      ]
    },
    "resourceTags": {
      "type": "object",
      "defaultValue": {
        "Environment": "Dev"
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[concat('stimura', uniquestring(resourceGroup().id))]",
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "tags": "[parameters('resourceTags')]"
    }
  ]
}

3-2 Template specsの作成

以下コマンドでTemplate specsを作成します。バージョンは2.0です。

Azure CLI
# template specsの作成
az ts create --name $tsName --version "2.0" --resource-group $rgSou --location $lc --template-file "./mainTemplate.json" --version-description "タグ追加"

正常終了後、Azure上にTemplate specsが作成されます。

4_create.png

左ペインのバージョンを押すと以下画面に遷移します。バージョン管理が出来ています。

5_create.png

3-3 ストレージアカウントの作成

バージョン2.0のTemplate specsを用いてストレージアカウントをデプロイします。

Azure CLI
# template specsのリソースIDの取得およびデプロイ
id=$(az ts show --name $tsName --resource-group $rgSou --version "2.0" --query "id")

az deployment group create --resource-group $rgDes --template-spec $id

正常終了後、ストレージアカウントにタグが付与されています。

6_memo.png

4. 終わりに

本記事を最後まで読んで頂きましてありがとうございます。
Azure内でシステムが完結するのであれば、Template specsは良いサービスだと思いました。今回はAzure CLIを利用しましたが、ポータルからでも利用できる為、わかりやすいです。

5. 参考記事

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?