Azure CLI provides an easy way to setup an account for Azure Resource Manager (ARM) and furthermore creates an new service principal for the Simple Azure access. In this tutorial, we use IPython helper (!) to run Azure CLI.
azure cli tool asks you to open a web browser and sign in the azure portal to authenciate. The following command azure login
will guide you to the page https://aka.ms/devicelogin with a unique one-time login verification code.
You will be asked to type the code in your browser to complete the login.
NOTE Run all cells step-by-step with instructions to complete Azure Account Setup.
In [2]:
!yes|azure login
Azure Python SDK which Simple Azure is based on requires the credential information below for ARM and ASM (Azure Service Management).
The following sections demonstrate Azure CLI commands to obtain these information step-by-step.
account show
displays subscription id and tenant id as ID and Tenant ID.
In [4]:
!azure account show
IPython filters the subscription ID and tenant ID using awk
command and stores into sid and tid variables.
In [ ]:
sid_tid = !azure account show|awk -F ':' '/ID/{ print $3}'
sid = sid_tid[0]
tid = sid_tid[1]
Once you loaded your azure credential, a service principal is required to get access of resource groups therefore Azure Services via Azure Resource Manager and Templates are permitted to use in Simple Azure. Azure CLI provides a few commands to complete this step.
"azure ad sp create" command create a new service principal in Active Directory with a name (--name option).
In [10]:
out=!azure ad sp create --name simpleazure
cid = out[6].split(":")[1].lstrip()
newout="\n".join(out)
print(newout)
Id
after Service Principal Names is our client id for Simple Azure. cid
variable stores the ID in the previous commands.
A password for Service Principal will be used as client_secret later in Simple Azure. Please provide your desired password in below.
In [72]:
password=""
In [19]:
!azure ad sp set -p $password $cid
Note that '$cid' is a client id obtained from the previous command.
Assigning role permits certain actions to your service principal under your subscription id. "Owner" allows you have every rights to use resources without restrictions. See more roles: here
In [62]:
!azure role assignment create --objectId $cid -o Owner -c /subscriptions/$sid
Are you completed all steps without any issues? Congraturations! You just completed login setup for your azure account.
Let's try to deploy a sample template using Simple Azure and the credentials that we just obtained.
In [42]:
from simpleazure import SimpleAzure as saz
In [38]:
import os
os.environ['AZURE_SUBSCRIPTION_ID'] = $sid
os.environ['AZURE_CLIENT_SECRET'] = $password
os.environ['AZURE_TENANT_ID'] = $tid
os.environ['AZURE_CLIENT_ID'] = $cid
In [49]:
saz_obj = saz()
In [44]:
url = "https://raw.githubusercontent.com/Azure-Samples/resource-manager-python-template-deployment/master/templates/template.json"
The sample template requires three parameters:
In [ ]:
saz_obj.arm.deploy(template = url, param = {"sshKeyData": "ssh-rsa AAAAB3...<skipped>... hroe.lee@simpleazure", 'dnsLabelPrefix':"simpleazure", 'vmName':'simpleazure-first-vm'})
Deleting a resource group where deployments are made stops all services and deletes resources in the group. Simple Azure uses prefixed group name 'saz' and the following function will delete the group.
In [67]:
saz_obj.arm.remove_resource_group()
Out[67]: