Azure Resource Manager The Series : ตอนที่ 4– แหล่งที่มาของ Azure Template

0
198

หลังจากที่ได้เรียนรู้ถึงโครงสร้างของแฟ้มข้อมูลที่เป็น JSON Format ไปแล้วในตอนก่อนหน้านี้นั้น  ในตอนนี้ผมจะขอพาทุกท่านไปทำความรู้จักกับแหล่งของ Azure Template กันเสียก่อนครับ ซึ่งแหล่งที่มาพวกนี้จะช่วยให้ท่านสามารถทำการเลือกใช้ Template สำเร็จรูปที่ Microsoft เตรียมไว้ให้มาใช้งานได้สะดวกรวดเร็ว ไม่ต้องเขียนใหม่ทั้งหมดครับ

แหล่งที่มาของ Azure Template

สำหรับท่านที่เพิ่งเริ่มใช้งาน Azure Template ในระยะแรกๆ อาจจะสงสัยว่าจะเริ่มต้นหา Azure Template จากที่ไหนที่จะสามารถนำมาใช้ประยุกต์ให้เข้ากับงานของตัวเองได้อย่างง่าย  ซึ่งเบื้องต้นผมขอแนะนำวิธีการศึกษา Azure Template จาก 2 แหล่งที่มาคือ

1. Azure Portal

Azure Portal นับว่าเป็นแหล่งข้อมูลในการศึกษาเกี่ยวกับ Azure Template ได้อย่างง่ายมากๆ ซึ่งหากเราสังเกตให้ดีๆ ในตอนสุดท้ายของการ Deploy อะไรสักอย่างเข้าไปใน Azure Portal นั้นในหน้าต่างสุดท้ายก่อนที่จะเริ่มการ Deploy นั้นจะมี Link ให้  “Download template File and Parameter” ดังรูป ซึ่งจะช่วยให้ผู้ใช้งานสามารถทำการ download template File และ Parameter file มาใช้งานได้เลยครับ

01

ซึ่งหลังจากที่คลิกที่ Link ดังกล่าวไปแล้ว จะปรากฎรายละเอียดต่างๆ ของ Template File  และมีตัวเลือก “Download” ให้ผู้ใช้งานสามารถ download มาใช้งานได้ต่อไป

02 

ซึ่งเมื่อทำการ Download เสร็จแล้ว จะได้ไฟล์ ชื่อ Template.zip ซึ่งเมื่อทำการ  Extract ออกมาแล้วจะมีไฟล์ในรูปแบบต่างๆ ให้เลือกใช้ในหลากหลายรูปแบบ เช่น PowerShell (.PS1),  Shell Script สำหรับ Linux (.SH) เป็นต้น  และที่สำคัญคือจะมี Template.JSON ซึ่งเป็นไฟล์ในการกำหนดรายละเอียดต่างๆ    และ Parameters.JSON ซึ่งเป็นพารามิเตอร์ในการทำงาน 

จากตัวอย่างต่อไปนี้เป็น Template File และ Parameters File สำหรับการ Deploy Virtual Machine จำนวน 1 VM โดยมีการสร้าง Storage Account, Virtual Network และอื่นๆ ที่จำเป็นไปในคราวเดียวกัน ซึ่งจะมี Template File ดังนี้

TEMPLATE.JSON

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string"
        },
        "virtualMachineName": {
            "type": "string"
        },
        "virtualMachineSize": {
            "type": "string"
        },
        "adminUsername": {
            "type": "string"
        },
        "virtualNetworkName": {
            "type": "string"
        },
        "networkInterfaceName": {
            "type": "string"
        },
        "networkSecurityGroupName": {
            "type": "string"
        },
        "adminPassword": {
            "type": "securestring"
        },
        "storageAccountName": {
            "type": "string"
        },
        "storageAccountType": {
            "type": "string"
        },
        "diagnosticsStorageAccountName": {
            "type": "string"
        },
        "diagnosticsStorageAccountId": {
            "type": "string"
        },
        "diagnosticsStorageAccountType": {
            "type": "string"
        },
        "addressPrefix": {
            "type": "string"
        },
        "subnetName": {
            "type": "string"
        },
        "subnetPrefix": {
            "type": "string"
        },
        "publicIpAddressName": {
            "type": "string"
        },
        "publicIpAddressType": {
            "type": "string"
        }
    },
    "variables": {
        "vnetId": "[resourceId('rg-ARMBlog','Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
        "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
    },
    "resources": [
        {
            "name": "[parameters('virtualMachineName')]",
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2016-04-30-preview",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]",
                "[concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]",
                "[concat('Microsoft.Storage/storageAccounts/', parameters('diagnosticsStorageAccountName'))]"
            ],
            "properties": {
                "osProfile": {
                    "computerName": "[parameters('virtualMachineName')]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]",
                    "windowsConfiguration": {
                        "provisionVmAgent": "true"
                    }
                },
                "hardwareProfile": {
                    "vmSize": "[parameters('virtualMachineSize')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "MicrosoftWindowsServer",
                        "offer": "WindowsServer",
                        "sku": "2016-Datacenter",
                        "version": "latest"
                    },
                    "osDisk": {
                        "name": "[parameters('virtualMachineName')]",
                        "createOption": "fromImage",
                        "vhd": {
                            "uri": "[concat(concat(reference(resourceId('rg-ARMBlog', 'Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2015-06-15').primaryEndpoints['blob'], 'vhds/'), parameters('virtualMachineName'), '20170417222850.vhd')]"
                        }
                    },
                    "dataDisks": []
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
                        }
                    ]
                },
                "diagnosticsProfile": {
                    "bootDiagnostics": {
                        "enabled": true,
                        "storageUri": "[reference(resourceId('rg-ARMBlog', 'Microsoft.Storage/storageAccounts', parameters('diagnosticsStorageAccountName')), '2015-06-15').primaryEndpoints['blob']]"
                    }
                }
            }
        },
        {
            "name": "[parameters('storageAccountName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2015-06-15",
            "location": "[parameters('location')]",
            "properties": {
                "accountType": "[parameters('storageAccountType')]"
            }
        },
        {
            "name": "[parameters('diagnosticsStorageAccountName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2015-06-15",
            "location": "[parameters('location')]",
            "properties": {
                "accountType": "[parameters('diagnosticsStorageAccountType')]"
            }
        },
        {
            "name": "[parameters('virtualNetworkName')]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2016-12-01",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[parameters('addressPrefix')]"
                    ]
                },
                "subnets": [
                    {
                        "name": "[parameters('subnetName')]",
                        "properties": {
                            "addressPrefix": "[parameters('subnetPrefix')]"
                        }
                    }
                ]
            }
        },
        {
            "name": "[parameters('networkInterfaceName')]",
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2016-09-01",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
                "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]",
                "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIpAddress": {
                                "id": "[resourceId('rg-ARMBlog','Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
                            }
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[resourceId('rg-ARMBlog', 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
                }
            }
        },
        {
            "name": "[parameters('publicIpAddressName')]",
            "type": "Microsoft.Network/publicIpAddresses",
            "apiVersion": "2016-09-01",
            "location": "[parameters('location')]",
            "properties": {
                "publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
            }
        },
        {
            "name": "[parameters('networkSecurityGroupName')]",
            "type": "Microsoft.Network/networkSecurityGroups",
            "apiVersion": "2016-09-01",
            "location": "[parameters('location')]",
            "properties": {
                "securityRules": [
                    {
                        "name": "default-allow-rdp",
                        "properties": {
                            "priority": 1000,
                            "sourceAddressPrefix": "*",
                            "protocol": "TCP",
                            "destinationPortRange": "3389",
                            "access": "Allow",
                            "direction": "Inbound",
                            "sourcePortRange": "*",
                            "destinationAddressPrefix": "*"
                        }
                    }
                ]
            }
        }
    ],
    "outputs": {
        "adminUsername": {
            "type": "string",
            "value": "[parameters('adminUsername')]"
        }
    }
}

PARAMETERS.JSON

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "value": "southeastasia"
        },
        "virtualMachineName": {
            "value": "ARM-VM01"
        },
        "virtualMachineSize": {
            "value": "Standard_DS3_v2"
        },
        "adminUsername": {
            "value": "vmadmin"
        },
        "virtualNetworkName": {
            "value": "ARMBlog-vnet"
        },
        "networkInterfaceName": {
            "value": "arm-vm01811"
        },
        "networkSecurityGroupName": {
            "value": "ARM-VM01-nsg"
        },
        "adminPassword": {
            "value": null
        },
        "storageAccountName": {
            "value": "saarmblog"
        },
        "storageAccountType": {
            "value": "Premium_LRS"
        },
        "diagnosticsStorageAccountName": {
            "value": "rgarmblogdiag587"
        },
        "diagnosticsStorageAccountType": {
            "value": "Standard_LRS"
        },
        "diagnosticsStorageAccountId": {
            "value": "Microsoft.Storage/storageAccounts/rgarmblogdiag587"
        },
        "addressPrefix": {
            "value": "172.16.0.0/16"
        },
        "subnetName": {
            "value": "SERVER"
        },
        "subnetPrefix": {
            "value": "172.16.1.0/24"
        },
        "publicIpAddressName": {
            "value": "ARM-VM01-ip"
        },
        "publicIpAddressType": {
            "value": "Dynamic"
        }
    }
}

2. Azure Quick Start Template

ในส่วนนี้ จะเป็น Template ที่ค่อนข้างยาก มีการ deployment ที่หลากหลาย ซึ่งทาง Microsoft ได้มีการจัดเตรียมไว้ให้ผู้ที่สนใจสามารถนำไปประยุกต์ใช้งานได้ตามความต้องการ ซึ่งผู้ที่สนใจสามารถที่จะไปหาข้อมูลได้จาก https://azure.microsoft.com/en-us/resources/templates/  ซึ่งจะมี template ต่างๆ ให้เลือกใช้มากมาย และมีตัวเลือกให้สามารถทำการค้นหาได้อย่างสะดวก  ซึ่งหน้าตาของ Azure Quick Start Template เป็นดังตัวอย่างในรูป

04