├── .gitignore ├── Asset └── ArmTemplate.json ├── CONTRIBUTING.md ├── DeployUsingARMTemplate.csproj ├── LICENSE ├── Program.cs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # Auth files 5 | *.auth 6 | *.azureauth 7 | 8 | # User-specific files 9 | *.suo 10 | *.user 11 | *.sln.docstates 12 | .vs/ 13 | *.lock.json 14 | developer/ 15 | 16 | # Build results 17 | binaries/ 18 | [Dd]ebug*/ 19 | [Rr]elease/ 20 | build/ 21 | src/NuGet.Config 22 | tools/7-zip/ 23 | 24 | [Tt]est[Rr]esult 25 | [Bb]uild[Ll]og.* 26 | 27 | *_i.c 28 | *_p.c 29 | *.ilk 30 | *.meta 31 | *.obj 32 | *.pch 33 | *.pdb 34 | *.pgc 35 | *.pgd 36 | *.rsp 37 | *.sbr 38 | *.tlb 39 | *.tli 40 | *.tlh 41 | *.tmp 42 | *.vspscc 43 | *.vssscc 44 | .builds 45 | 46 | *.pidb 47 | 48 | *.log 49 | *.scc 50 | # Visual C++ cache files 51 | ipch/ 52 | *.aps 53 | *.ncb 54 | *.opensdf 55 | *.sdf 56 | 57 | # Visual Studio profiler 58 | *.psess 59 | *.vsp 60 | 61 | # Code analysis 62 | *.CodeAnalysisLog.xml 63 | 64 | # Guidance Automation Toolkit 65 | *.gpState 66 | 67 | # ReSharper is a .NET coding add-in 68 | _ReSharper*/ 69 | 70 | *.[Rr]e[Ss]harper 71 | 72 | # NCrunch 73 | *.ncrunch* 74 | .*crunch*.local.xml 75 | 76 | # Installshield output folder 77 | [Ee]xpress 78 | 79 | # DocProject is a documentation generator add-in 80 | DocProject/buildhelp/ 81 | DocProject/Help/*.HxT 82 | DocProject/Help/*.HxC 83 | DocProject/Help/*.hhc 84 | DocProject/Help/*.hhk 85 | DocProject/Help/*.hhp 86 | DocProject/Help/Html2 87 | DocProject/Help/html 88 | 89 | # Click-Once directory 90 | publish 91 | 92 | # Publish Web Output 93 | *.Publish.xml 94 | 95 | # Others 96 | [Bb]in 97 | [Oo]bj 98 | TestResults 99 | [Tt]est[Rr]esult* 100 | *.Cache 101 | ClientBin 102 | [Ss]tyle[Cc]op.* 103 | ~$* 104 | *.dbmdl 105 | node_modules 106 | 107 | *.[Pp]ublish.xml 108 | 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Build tasks 112 | tools/*.dll 113 | 114 | # Sensitive files 115 | *.keys 116 | *.pfx 117 | 118 | # Backup & report files from converting an old project file to a newer 119 | # Visual Studio version. Backup files are not needed, because we have git ;-) 120 | _UpgradeReport_Files/ 121 | Backup*/ 122 | UpgradeLog*.XML 123 | 124 | # NuGet 125 | packages 126 | packages/repositories.config 127 | 128 | # Mac development 129 | .DS_Store 130 | 131 | # Specification DLLs 132 | *.Specification.dll 133 | 134 | # Generated readme.txt files # 135 | src/*/readme.txt 136 | 137 | build.out 138 | .nuget/ 139 | 140 | # Azure Project 141 | csx/ 142 | *.GhostDoc.xml 143 | pingme.txt 144 | -------------------------------------------------------------------------------- /Asset/ArmTemplate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 3 | "contentVersion": "1.0.0.0", 4 | "parameters": { 5 | "hostingPlanName": { 6 | "type": "string", 7 | "defaultValue": "" 8 | }, 9 | "skuName": { 10 | "type": "string", 11 | "defaultValue": "" 12 | }, 13 | "skuCapacity": { 14 | "type": "int", 15 | "defaultValue": 0 16 | }, 17 | "webSiteName": { 18 | "type": "string", 19 | "defaultValue": "" 20 | } 21 | }, 22 | "resources": [ 23 | { 24 | "apiVersion": "2015-08-01", 25 | "name": "[parameters('hostingPlanName')]", 26 | "type": "Microsoft.Web/serverfarms", 27 | "location": "[resourceGroup().location]", 28 | "tags": { 29 | "displayName": "HostingPlan" 30 | }, 31 | "sku": { 32 | "name": "[parameters('skuName')]", 33 | "capacity": "[parameters('skuCapacity')]" 34 | }, 35 | "properties": { 36 | "name": "[parameters('hostingPlanName')]" 37 | } 38 | }, 39 | { 40 | "apiVersion": "2015-08-01", 41 | "name": "[parameters('webSiteName')]", 42 | "type": "Microsoft.Web/sites", 43 | "location": "[resourceGroup().location]", 44 | "tags": { 45 | "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource", 46 | "displayName": "Website" 47 | }, 48 | "dependsOn": [ 49 | "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]" 50 | ], 51 | "properties": { 52 | "name": "[parameters('webSiteName')]", 53 | "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]" 54 | }, 55 | "resources": [ 56 | { 57 | "apiVersion": "2015-08-01", 58 | "name": "web", 59 | "type": "config", 60 | "dependsOn": [ 61 | "[concat('Microsoft.Web/sites/', parameters('webSiteName'))]" 62 | ], 63 | "properties": { 64 | "javaVersion": "1.8", 65 | "javaContainer": "TOMCAT", 66 | "javaContainerVersion": "8.0" 67 | } 68 | } 69 | ] 70 | } 71 | ] 72 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Azure samples 2 | 3 | Thank you for your interest in contributing to Azure samples! 4 | 5 | ## Ways to contribute 6 | 7 | You can contribute to [Azure samples](https://github.com/Azure-Samples/resources-dotnet-deploy-using-arm-template) in a few different ways: 8 | 9 | - Submit feedback on [this sample page](https://azure.microsoft.com/documentation/samples/resources-dotnet-deploy-using-arm-template/) whether it was helpful or not. 10 | - Submit issues through [issue tracker](https://github.com/Azure-Samples/resources-dotnet-deploy-using-arm-template/issues) on GitHub. We are actively monitoring the issues and improving our samples. 11 | - If you wish to make code changes to samples, or contribute something new, please follow the [GitHub Forks / Pull requests model](https://help.github.com/articles/fork-a-repo/): Fork the sample repo, make the change and propose it back by submitting a pull request. -------------------------------------------------------------------------------- /DeployUsingARMTemplate.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft Corporation. All rights reserved. 2 | // Licensed under the MIT License. See License.txt in the project root for license information. 3 | 4 | using Azure; 5 | using Azure.Core; 6 | using Azure.Identity; 7 | using Azure.ResourceManager; 8 | using Azure.ResourceManager.Resources; 9 | using Azure.ResourceManager.Resources.Models; 10 | 11 | namespace DeployUsingARMTemplate 12 | { 13 | public class Program 14 | { 15 | private static ResourceIdentifier? _resourceGroupId = null; 16 | 17 | /** 18 | * Azure Resource sample for deploying resources using an ARM template. 19 | */ 20 | public static async Task RunSample(ArmClient client) 21 | { 22 | var rgName = "rgRSAT"; // change the value here for your own resource group name 23 | var deploymentName = "dpRSAT"; // change the value here for your own arm deployment name 24 | try 25 | { 26 | //============================================================= 27 | // Create resource group. 28 | Console.WriteLine($"Creating a resource group with name: {rgName}"); 29 | 30 | var subscription = await client.GetDefaultSubscriptionAsync(); 31 | var rgLro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(AzureLocation.WestUS)); 32 | var resourceGroup = rgLro.Value; 33 | _resourceGroupId = resourceGroup.Id; 34 | 35 | Console.WriteLine($"Created a resource group: {_resourceGroupId}"); 36 | //============================================================= 37 | // Create a deployment for an Azure App Service via an ARM 38 | // template. 39 | 40 | Console.WriteLine($"Starting a deployment for an Azure App Service: {deploymentName}"); 41 | 42 | // tweak the values here to customize your web app 43 | var hostingPlanName = "hpRSAT"; 44 | var webAppName = "wnRSAT"; 45 | var webSkuName = "B1"; 46 | var webSkuCapacity = 1; 47 | var templateContent = File.ReadAllText(Path.Combine(".", "Asset", "ArmTemplate.json")).TrimEnd(); 48 | var deploymentContent = new ArmDeploymentContent(new ArmDeploymentProperties(ArmDeploymentMode.Incremental) 49 | { 50 | Template = BinaryData.FromString(templateContent), 51 | Parameters = BinaryData.FromObjectAsJson(new 52 | { 53 | hostingPlanName = new 54 | { 55 | value = hostingPlanName 56 | }, 57 | webSiteName = new 58 | { 59 | value = webAppName 60 | }, 61 | skuName = new 62 | { 63 | value = webSkuName 64 | }, 65 | skuCapacity = new 66 | { 67 | value = webSkuCapacity 68 | }, 69 | }) 70 | }); 71 | // we do not need the response of this deployment, therefore we just wait the deployment to complete here and discard the response. 72 | await resourceGroup.GetArmDeployments().CreateOrUpdateAsync(WaitUntil.Completed, deploymentName, deploymentContent); 73 | 74 | Console.WriteLine($"Completed the deployment: {deploymentName}"); 75 | } 76 | finally 77 | { 78 | try 79 | { 80 | if (_resourceGroupId is not null) 81 | { 82 | Console.WriteLine($"Deleting Resource Group: {_resourceGroupId}"); 83 | await client.GetResourceGroupResource(_resourceGroupId).DeleteAsync(WaitUntil.Completed); 84 | Console.WriteLine($"Deleted Resource Group: {_resourceGroupId}"); 85 | } 86 | } 87 | catch (Exception ex) 88 | { 89 | Console.WriteLine(ex); 90 | } 91 | } 92 | } 93 | 94 | public static async Task Main(string[] args) 95 | { 96 | try 97 | { 98 | //================================================================= 99 | // Authenticate 100 | var credential = new DefaultAzureCredential(); 101 | 102 | var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID"); 103 | // you can also use `new ArmClient(credential)` here, and the default subscription will be the first subscription in your list of subscription 104 | var client = new ArmClient(credential, subscriptionId); 105 | 106 | await RunSample(client); 107 | } 108 | catch (Exception ex) 109 | { 110 | Console.WriteLine(ex); 111 | } 112 | } 113 | } 114 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | --- 2 | page_type: sample 3 | languages: 4 | - csharp 5 | products: 6 | - azure 7 | extensions: 8 | services: Resource-Manager 9 | platforms: dotnet 10 | --- 11 | 12 | # Getting started on deploying using an ARM template in C# # 13 | 14 | Azure Resource sample for deploying resources using an ARM template. 15 | 16 | 17 | ## Running this Sample ## 18 | 19 | To run this sample, first you need to set up a way to authenticate to Azure with Azure Identity. 20 | 21 | Some options are: 22 | - Through the [Azure CLI Login](https://docs.microsoft.com/cli/azure/authenticate-azure-cli). 23 | - Via [Visual Studio](https://docs.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#authenticating-via-visual-studio). 24 | - Setting [Environment Variables](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/docs/AuthUsingEnvironmentVariables.md). 25 | 26 | ```bash 27 | git clone https://github.com/Azure-Samples/resources-dotnet-deploy-using-arm-template.git 28 | cd resources-dotnet-deploy-using-arm-template 29 | dotnet run 30 | ``` 31 | 32 | ## More information ## 33 | 34 | [Azure Management Libraries for C#](https://github.com/Azure/azure-sdk-for-net) 35 | [Azure .Net Developer Center](https://azure.microsoft.com/en-us/develop/net/) 36 | If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212) 37 | 38 | --- 39 | 40 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. --------------------------------------------------------------------------------