Reputation: 137
I am writing a program in Java that creates a VM instance in Azure, uploads a script to a container, and downloads and executes the script in the VM. However, I am currently facing a difficulty in granting the machine access to the container. When the machine is already up, I can manually go to Azure and assign a role with access, however I want to do that in my program (when creating the VM) before the machine is up, so that the program can run uninterrupted. Is there a way to do that? From the Documentation
Currently, the Azure portal does not support assigning a user-assigned managed identity during the creation of a VM. Instead, refer to one of the following VM creation Quickstart articles to first create a VM, and then proceed to the next section for details on assigning a user-assigned managed identity to the VM
Am I understanding correctly that it is not possible? Is there a workaround?
Upvotes: 1
Views: 274
Reputation: 42143
It is possible, the doc just means you can't do that in the portal, not mean in the code.
In your case, actually I am not sure you want to use system-assigned identity or user-assigned identity.
Here is a sample which creates a Linux VM with system-assigned identity enabled via withSystemAssignedManagedServiceIdentity
, if you want to use user-assigned identity, you could change the code to use WithUserAssignedManagedServiceIdentity
, you can specific an existing or not-yet-created user assigned identity, it depends on your requirement.
VirtualMachine virtualMachine = azureResourceManager.virtualMachines()
.define(linuxVMName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withNewPrimaryNetwork("10.0.0.0/28")
.withPrimaryPrivateIPAddressDynamic()
.withNewPrimaryPublicIPAddress(pipName)
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
.withRootUsername(userName)
.withRootPassword(password)
.withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
.withOSDiskCaching(CachingTypes.READ_WRITE)
.withSystemAssignedManagedServiceIdentity()
.create();
Upvotes: 1