🧐

Azure Logic Apps の IaC化

2023/11/25に公開

はじめに

この記事では、Azure Logic Apps の Infrastructure as Code(IaC)へのアプローチについて考察します。Azure Logic Apps は、マイクロソフトが提供するクラウドベースのサービスで、プログラミングなしでのデータとアプリの統合、業務フローの自動化に利用されます。このツールは、ノーコード・ローコード開発に属し、エンジニアでなくてもアプリ開発が可能です。しかし、学習曲線、開発の自由度の制限、ソース管理の複雑さなど、いくつかの課題も存在します。これらの課題に対処するため、IaC の導入が重要です。

IaC(Infrastructure as Code)化のアプローチ

IaC を実現する方法には、主に以下の3つがあります。

  • Azure Resource Manager (ARM) テンプレート
    Azure リソースをデプロイするための JSON 形式のファイルです。

  • Terraform
    複数のクラウドプロバイダーにわたるリソース管理を可能にするオープンソースツールです。

  • Azure Bicep
    Azure Resource Manager (ARM) テンプレートの代替として開発された、シンプルで宣言的な言語です。

過去3年間の Google Trends によると、Terraform、Bicep、Azure Resource Manager (ARM) の順に注目度が高まっています。具体的な数値では、Terraform の検索量は Bicep の約2倍、ARM の約3倍です。これは、Terraform の汎用性とクロスプラットフォーム対応によるものと考えられます。

Azure Resource Manager (ARM) から Azure Bicep への変換

Logic Apps は、専用のUIとデザイナーを使用して直感的にワークフローを定義できます。これらの定義は ARM テンプレートとして保存されます。以下に、Azure Resource Manager (ARM) テンプレートの例と、それを Bicep に変換する方法を示します。

Logic Apps デザイナの例

Azure ポータル > 対象の Logic Apps > 編集 をクリックします。

Azure Resource Manager (ARM) テンプレートの例

次に、Azure Resource Manager (ARM) テンプレートを見てみます。
Azure ポータル > 対象の Logic Apps > テンプレートのエクスポート をクリックします。
この操作で、Logic Apps の Azure Resource Manager (ARM) テンプレート(json)として表示されます。

app.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "workflows_logic_zenn_92c051f79f4b79_name": {
            "defaultValue": "logic-zenn-92c051f79f4b79",
            "type": "String"
        },
        "userAssignedIdentities_id_zenn_92c051f79f4b79_externalid": {
            "defaultValue": "/subscriptions/「サブスクリプションID」/resourceGroups/「リソースグループ名」/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id-zenn-92c051f79f4b79",
            "type": "String"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Logic/workflows",
            "apiVersion": "2017-07-01",
            "name": "[parameters('workflows_logic_zenn_92c051f79f4b79_name')]",
            "location": "japaneast",
            "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                    "/subscriptions/「サブスクリプションID」/resourceGroups/「リソースグループ名」/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id-zenn-92c051f79f4b79": {}
                }
            },
            "properties": {
                "state": "Enabled",
                "definition": {
                    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "triggers": {
                        "request": {
                            "type": "Request",
                            "kind": "Http",
                            "inputs": {
                                "schema": {
                                    "properties": {
                                        "itemid": {
                                            "type": "string"
                                        },
                                        "organization": {
                                            "type": "string"
                                        },
                                        "project": {
                                            "type": "string"
                                        }
                                    },
                                    "type": "object"
                                }
                            }
                        }
                    },
                    "actions": {
                        "HTTP:Azure_DevOps_Rest_API_Workitem取得": {
                            "runAfter": {},
                            "type": "Http",
                            "inputs": {
                                "authentication": {
                                    "audience": "499b84ac-1321-427f-aa17-267ca6975798",
                                    "identity": "[parameters('userAssignedIdentities_id_zenn_92c051f79f4b79_externalid')]",
                                    "type": "ManagedServiceIdentity"
                                },
                                "headers": {
                                    "Content-Type": "application/json-patch+json"
                                },
                                "method": "GET",
                                "uri": "https://dev.azure.com/@{triggerBody()?['organization']}/@{triggerBody()?['project']}/_apis/wit/workitems/@{triggerBody()?['itemid']}?api-version=7.1-preview.3"
                            }
                        },
                        "Response": {
                            "runAfter": {
                                "HTTP:Azure_DevOps_Rest_API_Workitem取得": [
                                    "Succeeded"
                                ]
                            },
                            "type": "Response",
                            "inputs": {
                                "body": "@body('HTTP:Azure_DevOps_Rest_API_Workitem取得')",
                                "statusCode": 200
                            }
                        }
                    },
                    "outputs": {}
                },
                "parameters": {}
            }
        }
    ]
}

Azure Resource Manager (ARM) から Bicep への変換

このコードを自身のローカル環境などにjson形式のファイルとして保存します。
今回は、app.jsonとします。

次に、Azure Resource Manager (ARM) テンプレートから、Azure Bicep へ変換してみます。
Azure Cliを利用して下記を実行することで、

cli
az bicep decompile --file app.json

この例ですと、app.bicepとしてファイルが出力されます。

app.bicep
param workflows_logic_zenn_92c051f79f4b79_name string = 'logic-zenn-92c051f79f4b79'
param userAssignedIdentities_id_zenn_92c051f79f4b79_externalid string = '/subscriptions/「サブスクリプションID」/resourceGroups/「リソースグループ名」/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id-zenn-92c051f79f4b79'

resource workflows_logic_zenn_92c051f79f4b79_name_resource 'Microsoft.Logic/workflows@2017-07-01' = {
  name: workflows_logic_zenn_92c051f79f4b79_name
  location: 'japaneast'
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '/subscriptions/「サブスクリプションID」/resourceGroups/「リソースグループ名」/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id-zenn-92c051f79f4b79': {}
    }
  }
  properties: {
    state: 'Enabled'
    definition: {
      '$schema': 'https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#'
      contentVersion: '1.0.0.0'
      parameters: {}
      triggers: {
        request: {
          type: 'Request'
          kind: 'Http'
          inputs: {
            schema: {
              properties: {
                itemid: {
                  type: 'string'
                }
                organization: {
                  type: 'string'
                }
                project: {
                  type: 'string'
                }
              }
              type: 'object'
            }
          }
        }
      }
      actions: {
        'HTTP:Azure_DevOps_Rest_API_Workitem取得': {
          runAfter: {}
          type: 'Http'
          inputs: {
            authentication: {
              audience: '499b84ac-1321-427f-aa17-267ca6975798'
              identity: userAssignedIdentities_id_zenn_92c051f79f4b79_externalid
              type: 'ManagedServiceIdentity'
            }
            headers: {
              'Content-Type': 'application/json-patch+json'
            }
            method: 'GET'
            uri: 'https://dev.azure.com/@{triggerBody()?[\'organization\']}/@{triggerBody()?[\'project\']}/_apis/wit/workitems/@{triggerBody()?[\'itemid\']}?api-version=7.1-preview.3'
          }
        }
        Response: {
          runAfter: {
            'HTTP:Azure_DevOps_Rest_API_Workitem取得': [
              'Succeeded'
            ]
          }
          type: 'Response'
          inputs: {
            body: '@body(\'HTTP:Azure_DevOps_Rest_API_Workitem取得\')'
            statusCode: 200
          }
        }
      }
      outputs: {}
    }
    parameters: {}
  }
}

Bicep に変換することで、コードはより読みやすく、簡潔になります。これは、Bicep が ARMテンプレートよりもシンタックスがシンプルであるためです。

まとめ

Azure Logic Apps を IaC 化するには、まずは Azure ポータルからデザイナーで定義し、ARM テンプレートをエクスポートすることから始めます。その後、これをBicepにデコンパイルすることで、管理とメンテナンスが容易になります。

References

https://learn.microsoft.com/ja-jp/azure/logic-apps/logic-apps-overview
https://learn.microsoft.com/ja-jp/cli/azure/
https://trends.google.co.jp/trends/explore?date=2020-11-25 2023-11-25&q=Azure Resource Manager,Azure Bicep,Azure Terraform&hl=ja

GitHubで編集を提案

Discussion