手把手教你創建Azure ARM Template


Azure的ARM模式在中國已經落地了。在ARM模式中,通過ARM的Template批量的創建各種資源是與ASM模式的最大的區別之一。目前Azure ARM的Template數量已經越來越多,更多的客戶會選擇采用Template的模式進行資源的部署:

在前面的文章中已經介紹了如何通過已有的Template修改成你所需要的模板,請參考:

http://www.cnblogs.com/hengwei/p/5634380.html

本文將一步一步的創建一個最簡單的存儲賬戶的ARM Template,並部署到Azure China中。

一 准備工具

1 下載安裝工具:Visual Studio Code

首先下載Visual Studio Code:

https://code.visualstudio.com/

這個軟件是Visual Studio的簡化版,是免費的。並且可以支持Windows、MAC和Linux。

2 安裝ARM的插件:

打開Visual Studio Code,在最左邊點中"Extensions",在搜索框中輸入azure后搜索:

其中"Azure Resource Manager Tools"和"armsnippet"就是Azure ARM Template的插件。點擊安裝。

點擊Enable,重啟Visual Studio Code。

3 配置Visual Studio Code

從鏈接:

https://raw.githubusercontent.com/Azure/azure-xplat-arm-tooling/master/VSCode/armsnippets.json

復制內容,在Visual Studio Code中打開:

File->Preferences->User Snippets:

輸入json,點擊JSON:

在{ }中復制剛剛拷貝的內容:

Ctrl-S保存后關閉Visual Studio Code。

二 准備要創建的資源

本文將編寫最簡單的創建存儲的JSON文件。如果客戶要創建一個存儲賬戶,需要准備如下信息:

1 StorageAccountName: hwsa10

2 StorageAccountType: Standard_LRS

3 Resource Group: 采用一個已經存在的hwarm

 

三 編寫JSON模板

1 編寫AzureDeploy.json文件

打開Visual Studio Code,新建一個文件,在右下角,點擊Plain Text輸入json:

2 在編輯區域輸入"arm":

此時會有提示"Microsoft Azure Resouce Manager(ARM) JSON Template structure",按回車:

將會出現:

{

"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",

"contentVersion": "1.0.0.0",

"parameters": { },

"variables": { },

"resources": [ ],

"outputs": { }

}

ARM模板的標准的幾個參數。

3 編輯parameter

在parameter中輸入相關參數,在輸入了""后,Visual Studio Code出現提示:

這幾項是Parameter可以輸入的參數,其中"type"是必選項。在輸入過程中,每次輸入"",都會出現提示:

把前面准備的"storageAccountName"和"storageAccountType"輸入到Parameter中。

"parameters": {

"storageAccountName":{"type":"string"},

"storageAccountType":{

"type": "string",

"defaultValue": "Standard_LRS",

"allowedValues": [

"Standard_LRS",

"Standard_GRS",

"Premium_LRS"

]

}

},

輸入resource內容:

輸入""后,出現提示,其中apiVersion、location、propertises、type、name是必選項:

同樣,每次輸入""都會有提示:

輸入完的結果:

{

"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",

"contentVersion": "1.0.0.0",

"parameters": {

"storageAccountName":{"type":"string"},

"storageAccountType":{

"type": "string",

"defaultValue": "Standard_LRS",

"allowedValues": [

"Standard_LRS",

"Standard_GRS",

"Premium_LRS"

]

}

},

"variables": { },

"resources": [

{

"type": "Microsoft.Storage/storageAccounts",

"name": "[parameters('storageAccountName')]",

"apiVersion": "2015-06-15",

"location": "[resourceGroup().location]",

"properties": {

"accountType": "[parameters('storageAccountType')]"

}

 

}

],

"outputs": { }

}

四 編輯parameter文件

根據已有的parameter文件,將前面定義的兩個參數填好:

{

"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",

"contentVersion": "1.0.0.0",

"parameters": {

"storageAccountName": {

"value": "hwsa01"

},

"storageAccountType": {

"value": "Standard_LRS"

}

}

}

至此,兩個文件都編輯完成。

然后通過PowerShell可以將此Template發布到Azure上,創建StorageAccount。

PS C:\Users\hengz> New-AzureRmResourceGroupDeployment -Name hwarmtemplate -ResourceGroupName hwarm -Mode Incremental -TemplateFile D:\AzureDeploy.json -TemplateParameterFile D:\DeployParameterFile.json

 

 

DeploymentName : hwarmtemplate

ResourceGroupName : hwarm

ProvisioningState : Succeeded

Timestamp : 2016/9/9 13:29:53

Mode : Incremental

TemplateLink :

Parameters :

Name Type Value

=============== ========================= ==========

storageAccountName String hwsa10

storageAccountType String Standard_LRS

 

Outputs :

DeploymentDebugLogLevel :

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM