AzureのリソースをARM Templateとして表現する

⏱️約3分
シェア:

ARM Templateは、Azureリソースを定義するjson形式のファイルです。 ARM Templateは、IaCを実現するのに役立ちます。 ただし、そもそもARM Templateを作成するのは少し難しいです。 そこで、今回は、新しいリソースをデプロイする必要がある場合に、 ARM Templateを効率的に作成する方法について説明します。 最初に、新しいリソースをjson形式で記述する方法をすばやく理解する方法を説明し、次にいくつかの例を示します。

1. Azure Portalからテンプレートをエクスポートする

テンプレートを最初から作成することは困難です。したがって、最初にAzure Portalでターゲットリソースを作成してから、リソースのテンプレートをエクスポートして参照として使用することをお勧めします。

Export template1 Export template2

ここでは、リソースがARM Templateとして記述されていることがわかります。

2.公式文書を読む

次に、Microsoftの公式ドキュメント1を参照できます。 このドキュメントでは、定義されているプロパティの種類、指定できる値の種類などについて説明しています。 これらのドキュメントを読むことにより、さきほどエクスポートしたテンプレートを編集します。 各リファレンスページの下部にもサンプルテンプレートがあります。

Quick start template

3. REST APIリファレンスを確認する

ARM Template一部のプロパティオブジェクトの詳細に関する説明が見つからない場合があります。 その場合、REST APIリファレンスが役立つ場合があります。 ARM Templateを実行すると、関連するREST APIがバックエンドで呼び出されます。 これは、REST APIリファレンス2によってオブジェクト定義を推測できることを意味します。

その他のヒント

テンプレート関数

テンプレート関数を使用して、Azureリソースを効果的に表すことができます。 たとえば、 copy関数を使用して配列オブジェクトを作成できます。

json
1{
2 "$schema": "https://schema.management.azure.com/schemas/2018-05-01/deploymentTemplate.json#",
3 "contentVersion": "1.0.0.0",
4 "parameters": {
5 "ipAddressArray": {
6 "type": "string",
7 "metadata": {
8 "description": "IP Address split by a comma."
9 }
10 }
11 },
12 "resources": [
13 {
14 "type": "Microsoft.Web/sites/config",
15 "name": "functionConfig",
16 "apiVersion": "2018-11-01",
17 "location": "[resourceGroup().location]",
18 "scale": null,
19 "properties": {
20 "copy": [
21 {
22 "name": "ipSecurityRestrictions",
23 "count": "[length(split(parameters('ipAddressArray'), ','))]",
24 "input": {
25 "ipAddress": "[string(split(parameters('ipAddressArray'), ',')[copyIndex('ipSecurityRestrictions')])]",
26 "name": "[concat(resourceGroup().location, copyIndex('ipSecurityRestrictions'))]"
27 }
28 }
29 ]
30 }
31 }
32 ]
33}

ipAddressArrayが、カンマで区切られたIPアドレスのセットを表す文字列パラメーターであるとします。 10.0.0.0/16,10.0.1.0/16,...

このARM Templateは次のようにコンパイルされます。

json
1{
2 "$schema": "https://schema.management.azure.com/schemas/2018-05-01/deploymentTemplate.json#",
3 "contentVersion": "1.0.0.0",
4 "parameters": {
5 "ipAddressArray": {
6 "type": "string",
7 "metadata": {
8 "description": "IP Address split by comma"
9 }
10 }
11 },
12 "resources": [
13 {
14 "type": "Microsoft.Web/sites/config",
15 "name": "functionConfig",
16 "apiVersion": "2018-11-01",
17 "location": "japaneast",
18 "scale": null,
19 "properties": {
20 "ipSecurityRestrictions": [
21 {
22 "ipAddress": "10.0.0.0/16",
23 "name": "japaneast0"
24 },
25 {
26 "ipAddress": "10.0.1.0/16",
27 "name": "japaneast1"
28 },
29 ...
30 ]
31 }
32 }
33 ]
34}

ipSecurityRestrictionscopy関数によって定義されていることがわかります。 詳細については、Microsoftの公式ドキュメント3を参照してください。

その他のツール

Resource Explorer

Resource Explorer 4は、リソースをjson形式で記述する方法を理解するのにも役立ちます。

Resource Explorer

このツールは、リソースのプロパティを確認する場合に役立ちます。 例えば、API ManagementからmanagedIdを取得する必要があるとします。

json
1{
2 "value": [
3 {
4 "id": "/subscriptions/your-subscription-id/resourceGroups/your-resource-group-name/providers/Microsoft.ApiManagement/service/your-api-management-service-name",
5 "name": "your-apim-name",
6 "type": "Microsoft.ApiManagement/service",
7 "location": "Japan East",
8 "etag": "xxx",
9 "properties": {
10 "publisherEmail": "xxx",
11 "publisherName": "xxx",
12 "notificationSenderEmail": "xxx",
13 "provisioningState": "Succeeded",
14 "targetProvisioningState": "",
15 "createdAtUtc": "2020-04-13T09:37:25.5871876Z",
16 "gatewayUrl": "https://your-apim-name.azure-api.net",
17 "gatewayRegionalUrl": "xxx",
18 "portalUrl": "https://your-apim-name.portal.azure-api.net",
19 "developerPortalUrl": "https://your-apim-name.developer.azure-api.net",
20 "managementApiUrl": "https://your-apim-name.management.azure-api.net",
21 "scmUrl": "https://your-apim-name.scm.azure-api.net",
22 "hostnameConfigurations": [],
23 "publicIPAddresses": [
24 "x.x.x.x"
25 ],
26 "privateIPAddresses": null,
27 "additionalLocations": null,
28 "virtualNetworkConfiguration": null,
29 "customProperties": {
30 "Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Protocols.Tls10": "False",
31 "Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Protocols.Tls11": "False",
32 "Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Protocols.Ssl30": "False",
33 "Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Ciphers.TripleDes168": "False",
34 "Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Backend.Protocols.Tls10": "False",
35 "Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Backend.Protocols.Tls11": "False",
36 "Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Backend.Protocols.Ssl30": "False",
37 "Microsoft.WindowsAzure.ApiManagement.Gateway.Protocols.Server.Http2": "False"
38 },
39 "virtualNetworkType": "None",
40 "certificates": null
41 },
42 "sku": {
43 "name": "Developer",
44 "capacity": 1
45 },
46 "identity": {
47 "type": "SystemAssigned",
48 "principalId": "your-id",
49 "tenantId": "your-tenant"
50 }
51 },

Resource Explorerは、リソースの構造を理解するのに役立ちます。 この場合、 identity.principalIdを介してIDを取得できることがわかります。

したがって、これがIDにアクセスするためのテンプレート関数です。

json
1"[reference(parameters('resourceId'), 'api-version', 'Full').identity.principalId]"

ARM Template Viewer

Visual Studio Code拡張版であるARM Template Viewerも、リソースを視覚化するのに役立ちます。 これは、 ARM Template Viewerによって作成された図です。

ARM Template Viewer

各リソースが相互にどのように依存しているかを理解するのに役立ちます。

概要

この記事では、新しいARM Templateを効率的に作成する方法について説明しました。 これで、 ARM Templateを介して必要なリソースを作成できます。幸運を!

Footnotes

  1. Define resources in Azure Resource Manager templates

  2. Azure REST API Reference

  3. Azure Resource Manager template functions

  4. Resource Explorer

シェア:

関連記事