😀

Azure ポータルで作成した Azure Logic Apps を ARM テンプレートでエクスポートして Bicep

に公開

Azure ポータルで作成した Azure リソースを IaC 化したい場合、Terraform なら terraform import で tf ファイルに取り込む事ができます。Bicep ならどうするんだろうと調べたら、APM テンプレートをエクスポートして Bicep 化すれば良さそうなので、やってみました。

ARM テンプレートをエクスポート

Azure Logic Apps を Azure ポータルで作成している前提で、arm-template.json というファイル名でエクスポートします。

bash
resourceid=$(az resource show \
  --resource-group mnrpt-rg \
  --name mnrpt-test \
  --resource-type Microsoft.Logic/workflows \
  --query id \
  --output tsv)

az group export \
  --resource-group mnrpt-rg \
  --resource-ids $resourceid \
  --skip-resource-name-params \
  > arm-template.json

エクスポートした JSON ファイルを確認

arm-template.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "resources": [
    {
      "apiVersion": "2017-07-01",
      "location": "japaneast",
      "name": "mnrpt-test",
      "properties": {
        "definition": {
          "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
          "actions": {},
          "contentVersion": "1.0.0.0",
          "outputs": {},
          "parameters": {},
          "triggers": {
            "manual": {
              "correlation": {
                "clientTrackingId": "test"
              },
              "inputs": {},
              "kind": "Http",
              "type": "Request"
            }
          }
        },
        "parameters": {},
        "state": "Enabled"
      },
      "type": "Microsoft.Logic/workflows"
    }
  ],
  "variables": {}
}

ARM テンプレートを Bicep に変換

bash
az bicep decompile \
  --file arm-template.json \
  --force

変換した Bicep ファイルを確認

arm-template.bicep
resource mnrpt_test 'Microsoft.Logic/workflows@22017-07-01' = {
  location: 'japaneast'
  name: 'mnrpt-test'
  properties: {
    definition: {
      '$schema': 'https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#'
      actions: {}
      contentVersion: '1.0.0.0'
      outputs: {}
      parameters: {}
      triggers: {
        manual: {
          correlation: {
            clientTrackingId: 'test'
          }
          inputs: {}
          kind: 'Http'
          type: 'Request'
        }
      }
    }
    parameters: {}
    state: 'Enabled'
  }
}

Bicep ファイルを手直し

bash
az deployment group what-if \
  --resource-group mnrpt-rg \
  --template-file arm-template.bicep

こちらのコマンドを実行すると Modify 行として ~ Microsoft.Logic/workflows/mnrpt-test [2017-07-01] が表示されるため、下記のように一行目の api-version の箇所にある @2017-07-01@2019-05-01 に変更します。

arm-template.bicep
resource mnrpt_test 'Microsoft.Logic/workflows@2019-05-01' = {
  location: 'japaneast'
  name: 'mnrpt-test'
  properties: {
    definition: {
      '$schema': 'https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#'
      actions: {}
      contentVersion: '1.0.0.0'
      outputs: {}
      parameters: {}
      triggers: {
        manual: {
          correlation: {
            clientTrackingId: 'test'
          }
          inputs: {}
          kind: 'Http'
          type: 'Request'
        }
      }
    }
    parameters: {}
    state: 'Enabled'
  }
}

現在デプロイしているものと変更差分が無い事を確認

Nochange 行として = Microsoft.Logic/workflows/mnrpt-test [2019-05-01] が表示されます。

bash
az deployment group what-if \
  --resource-group mnrpt-rg \
  --template-file arm-template.bicep

デプロイしても変わらない事を確認

bash
az deployment group create \
  --resource-group mnrpt-rg \
  --template-file arm-template.bicep

参考

https://learn.microsoft.com/ja-jp/azure/azure-resource-manager/templates/export-template-cli

https://learn.microsoft.com/ja-jp/azure/azure-resource-manager/bicep/decompile?tabs=azure-cli

https://learn.microsoft.com/ja-jp/cli/azure/deployment/group?view=azure-cli-latest

Discussion