Giter Site home page Giter Site logo

resource-manager-dotnet-template-deployment's Introduction

services platforms author
azure-resource-manager
dotnet
devigned

Deploy an SSH Enabled VM with a Template with .NET

This sample explains how to use Azure Resource Manager templates to deploy your resources to Azure using the Azure SDK for .NET.

When deploying an application definition with a template, you can provide parameter values to customize how the resources are created. You specify values for these parameters either inline or in a parameter file.

On this page

Run this sample

  1. If you don't have it, install the .NET Core SDK.

  2. Clone the repository.

    git clone https://github.com/Azure-Samples/resource-manager-dotnet-resources-and-groups.git
    
  3. Install the dependencies.

    dotnet restore
    
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Add these environment variables to your .env file using your subscription id and the tenant id, client id and client secret from the service principle that you created.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
    
  6. Run the sample.

    dotnet run
    

What is program.cs doing?

The sample starts by logging in using your service principal.

// Build the service credentials and Azure Resource Manager clients
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var resourceClient = new ResourceManagementClient(serviceCreds);
resourceClient.SubscriptionId = subscriptionId;

Then it creates a resource group into which the VM will be deployed.

resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup { Location = westus});

Deploy the template

Now, the sample loads the template and deploys it into the resource group that it just created.

// build the deployment from a json file template from parameters
var templateParams = new Dictionary<string, Dictionary<string, object>>{
        {"dnsLabelPrefix", new Dictionary<string, object>{{"value", vmDnsLabel}}},
        {"vmName", new Dictionary<string, object>{{"value", "azure-deployment-sample-vm"}}}
    };

var deployParams = new Deployment{
    Properties = new DeploymentProperties{
        Template = JObject.Parse(File.ReadAllText(templateFileLocation)),
        Parameters = templateParams,
        Mode = DeploymentMode.Incremental
    }
};

var sshPubLocation = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".ssh", "id_rsa.pub");
if(File.Exists(sshPubLocation)){
    Write("Found SSH public key in {0}.", sshPubLocation);
    var pubKey = File.ReadAllText(sshPubLocation);
    Write("Using public key: {0}", pubKey);
    templateParams.Add("sshKeyData", new Dictionary<string, object>{{"value", pubKey}});
}
else
{
    Write("We could not find a RSA public key in {0}. Please create a public key and place it there.", sshPubLocation);
    return;
}

var groupParams = new ResourceGroup { Location = westus};
var deployResult = resourceClient.Deployments.CreateOrUpdate(resourceGroupName, "sample-deploy", deployParams);

Console.ReadLine();

resourceClient.ResourceGroups.Delete(resourceGroupName);

resource-manager-dotnet-template-deployment's People

Contributors

acomsmpbot avatar allclark avatar devigned avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

resource-manager-dotnet-template-deployment's Issues

dotnet restore failed with dotnet 1.0.1

Running dotnet restore using dotnet 1.0.1 prints the error:
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
I googled around for the error message and tried to run dotnet migrate but it is taking forever.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.