Migrating a Linux VM from one Azure directory to another using ARM templates

Scenario: You want to move an Azure Linux VM from one subscription to another subscription hosted in a different directory. You want to do this using an ARM template.

Assumptions: 

i) You already have a VM running on one of the subscriptions.

ii) For the purpose of this tutorial, I am using a VM running Ubuntu which I will be moving. You could be using any other Linux workload.

iii) The source subscription is called X and the target is called Y.

iv) We will use Powershell to deploy the template but you can also use Bash.

Steps:

      1. We need to download the ARM template of the VM from the source subscription. Let’s call it subscription X.
      2. Login to the Azure portal of subscription X.
      3. Navigate to the VM. Then click on the VM’s Resource Group. On the LHS, click on Automation Script. Then click on Download.  It will download a zip.
        Download template
        Download ARM template
      4. Go to the directory where target subscription Y is located.
      5. Fetch the subscription ID of subscription Y. You can get this by clicking on Overview in any Resource Group and copying subscription Id from there.
      6. Alternatively, type Get-AzureRMSubscription in the Powershell Cloudshell and copy the subscription ID for subscription Y.
      7. In the downloaded zip, open template.json and update all the subscription IDs to the one we copied in the previous step.
      8. If there are any storage accounts in the template.json, update the value to a unique value to avoid errors when running the template. You can search for parameters starting with prefix storageAccounts.
      9. Upload this template to a storage account on subscription Y. Get its blob URL.
      10. Create a resource group in the subscription Y. Let’s call it “RG-DEST-VM”
      11. In the Azure CloudShell, ensure Powershell is selected.
      12. Make sure you have selected the correct subscription

       

    Select–AzureRmSubscription –SubscriptionId  <<ID>>

    1. 13. Deploy the template using:

  1. New-AzureRmResourceGroupDeployment -ResourceGroupName “RG-DEST-VM” -TemplateUri  your-blob-url

Reading Azure Application Settings in a Node.js App

Environment Variables and Deployment Slots.

So you’ve recently deployed a Node application to an Azure App Service. Everything is working smooth and now you want to create a deployment slot to host your staging/ pre-production environment.

You want to use separate databases (duh! don’t want to alter production data) for the staging and production environment but don’t want to make changes to the code each time you deploy to staging slot.

The idea here is to make sure that each time you swap your slot, the value for the database connection or any variable that is environment dependent is loaded directly from the environment, thus freeing the user of the hassle to manually alter / edit code during deployment.

Continue reading →

Debugging a deployed Azure App Service

Very recently I ran into a few issue upon deploying an app to Azure app service which I was unable to replicate locally.

My API app was working locally but on deploying I was getting a multitude of reference errors.

Error : The located assembly’s manifest definition does not match the assembly reference.

The most likely cause of this error is a mismatch in the assembly version of the reference that your project is trying to load.

Complete Error:

Could not load file or assembly ‘System.Web.Mvc, Version=4.0.40804.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference.

Debugging on Azure:

Now since my project was building locally and I was getting this error only on deployment, I tried to look at the bin folder of my deployed solution to see which assembly was uploaded there.

  1. Go to your App Service in which you have deployed the solution.
  2. Search for Advanced tools and navigate to the Kudu Console. Alternatively, navigating to http://yoururl.scm.azurewebsites.net will also work.

kudu_image

  1. Download the dll that is causing the error from the bin directory. Right click and check the dll properties and version number.

  2. If there is a mismatch in the version between what is shown in the downloaded copy and the one included in your project solution, you need to redeploy the solution to Azure.

Resolution Steps

  1. On investigating, turns out that the .dll uploaded in my Azure bin directory was an older version and the deployed solution was trying to load a newer version as mentioned in the package file.
  2. The best way to resolve this is to delete the contents of your bin directory on Azure and do a clean deploy.
  3. Navigate to the bin directory of Kudu and delete the bin folder.
  4. Right click on your project from Solution Explorer and do a Web Deploy to the target app service.

 

 

 

Passing the New Azure 70-532 Exam with ARM Objectives

I recently wrote and passed the Azure 70-532: Developing Microsoft Azure Solutions. The exam was updated on 22 Nov, 2016 and I took it on 23 Dec 2016. I was a little apprehensive as the exam objectives had changed to reflect ARM modules as that meant I would have to redo a fair amount of studying. But I managed to pass the exam in one go and I thought I would like to share the resources and links which I went through that helped me achieve the same.

Here is a breakup of the links I studied as per the new objectives.

Create and manage Azure Resource Manager virtual machines (30 – 35%)

Continue reading →

Understanding Cloud Service Offerings: SaaS, PaaS, IaaS

We have all come across the terms SaaS (Software as a Service), PaaS (Platform as a Service) and IaaS (Infrastructure as a Service) and confused one with the other more often than not. Let us try to understand these 3 concepts in more lucid terms. Having a clear idea of the three flavours of cloud services provides the basis required to continue your journey on cloud computing.

There are diagrams out there explaining separation of responsibilities but this post is a high level view of what these terms mean and tries to give you an intuition of the differences in each without throwing in a lot of technical jargon.

Continue reading →

How to write to Azure Blob Storage?

What is a blob?

BLOB stands for Binary Large Object. Well, that clears things up, doesn’t it? Simply put, a blob is a large file such as an image or video or some other type of multimedia that needs to be handled in a manner different from how text is written into traditional databases. The large file is stored as a collection of binary data into a storage system.

How do I write images/ content to a Blob in Azure Storage?

Azure blob storage is a service for storing such data. Each blob storage account can hold  upto 500 TB of data. Within each storage account, there can be multiple containers. Containers are similar to folders in that they can be used to logically segregate different types of files.

The process of writing to a blob in Azure consists of the following steps:-

I. Creating a container in Azure

  1. First we go to the Azure portal (if you do not have an account, create one and you will get a monthly credit) and create a new storage account that will hold our blobs.
  2. Navigate to ms.azure.portal.com and click on the “+” >> Storage >> Storage Account.im1
  3. Name the storage account and create a new resource group if you do not already have any.
    img2
  4. Now we create the container. Go to all resources, find the storage account you have created and click on it.  In the right pane, click Containers and then create a new Container of Access Type, Private.im3-container
  5. Before we write code to access the container, we will need to get the keys for the storage account. Go to all resources, click on the storage account you created. Scroll to Settings and Click on Access keys. Copy Key1 and save it somewhere. We will need this when trying to save a blob to the container.

im4-accesskeys

II. Writing blobs to the container.

  1. Create a New Console Application in Visual Studio.
  2. Open your Program.cs file and create a new function called WriteToBlob(). This function must be of type public static.
  3. Write the following lines in the function and replace the constants with the credential we have acquired from previous step.
    string accountName = "ENTER_YOUR_STORAGE_ACCOUNT_NAME";
    string accountKey = "YOUR_STORAGE_ACCOUNT_KEY";
    string response = "";
  4. Create a blob client:
    CloudBlobClient client = account.CreateCloudBlobClient();
  5. Get a reference to the container. Make sure to replace CONATINER_NAME with the actual name of your container.
    CloudBlobContainer sampleContainer = client.GetContainerReference("CONTAINER_NAME");
    sampleContainer.CreateIfNotExists();
  6. Get a reference to the blob. If the blob does not exist, this function will create the blob. So if you are uploading an image, this is where you would specify the blob name.
    CloudBlockBlob blob =  sampleContainer.GetBlockBlobReference("NAME_TO_GIVE_TO_UPLOADED_IMAGE");
  7. Upload the file to the blob
    using (Stream file =  System.IO.File.OpenRead("LOCAL_PATH_OF_FILE_TO_UPLOAD"))
    {
    blob.UploadFromStream(file);
    }
    response = "File uploaded successfully";
  8. Finally call the WriteToBlob() function in your Program.cs’ Main function.

You can download the entire source code from Github

JavaScript and Prototypal Inheritance – I

How does inheritance happen in JavaScript?

Inheritance in Javascript does not follow the rules of classical inheritance. Instead, objects link to objects (OLOO). This is done using an object’s prototype and hence, the method of inheritance is called prototypal inheritance.

Before we see examples of how this works let’s try to understand what we mean by a prototype and how it allows objects to inherit from each other.

What is an object prototype? 

Every object created in JavaScript derives from ‘Object’, a standard built-in object in Javascript. The prototype property of Object, ie, Object.prototype is itself an object from which all other objects inherit methods and properties.

object-prototype

Continue reading →

The journey from a novice to developer

This is an introductory post. I have been in the software development industry for the past 2 years and belong to the section of the population that our community refers to as ‘freshers’.

When I was in college, just beginning my computer science degree, I deluded myself into thinking that I was a good programmer, but when I stepped out into the world of startups and corporates, I got my first practical experience into developing software. Even though I had worked on projects which included setting up simple CRUD based websites, there was always a yearning for more challenges, more things that would set off sparks in my brain. There was always so much to explore: apps, machine learning, big data, competitive programming etc. In wanting to do it all, I learnt most of the things only on the surface.

Here I was, a CS undergrad in her sophomore year, solving ACM-ICPC questions, studying courses on Coursera and Udacity, feeling vindicated on clearing her assignments, blissfully unaware of the existence of customer requirements. Sure I read a ton of tech news and blogs, but I never got a sense of just how many many processes come together when creating something till I landed my first internship and learnt about stuff like SCRUMs, sprints, all the different types of testing. It made me realize that just being able to write code isn’t enough, you need to be able to develop your own style of coding, set goals, test code, handle change requests, incorporate customer feedback,etc. The initial period of the bubble bursting wasn’t easy to take, but for those with enough hunger, adaptability is key.

I am starting this blog with the aim of giving people who are somewhat introverted like me( who don’t usually go outright and ask for someone to show them how, especially more so when they know that with a little perseverance they can solve things on their own), a few pointers and nudges which I feel could have helped me a lot if I had known how to go about it. A lot of people I know would argue that I wasted a lot of time figuring things out when I could have simply asked someone, but I think everyone needs to go through their own struggles to realize their capabilities and build confidence.

This blog will be a combination of posts on coding styles, how to improve your code, tutorials and guides to software and anything else that I can think about (pertaining to tech), of course.