Search code examples
javaazure-container-instancesazure-sdkprivate-subnet

How to attach private vnet or subnet with container instances in java using Azure SDK?


Challenge in attaching subnet and vnet configurations while creating azure container instances.

I am trying to create Azure container instances using the Azure SDK - Java/.Net. This container requires to communicate with components across different VMs. I was able to achieve this using Azure CLI commands with vnet and subnet configurations. But unable to replicate the same via SDK.

Code Snippet in Java

ContainerGroup containerGroup = azure.containerGroups().define(aciName).withRegion(Region.EUROPE_NORTH)
                    .withExistingResourceGroup(rgName).withLinux()
                    .withPrivateImageRegistry(registryServer, registryServerName, registryServerKey)
                    .defineVolume(volumeMountName).withExistingReadOnlyAzureFileShare(fileShareName)
                    .withStorageAccountName(storageAccountName).withStorageAccountKey(storageAccountKey).attach()
                    .defineContainerInstance(aciName).withImage(containerImageName).withExternalTcpPort(80)
                    .withVolumeMountSetting(volumeMountName, volumeMountPath).withCpuCoreCount(1)
                    .withMemorySizeInGB(1.5).withEnvironmentVariable("APP_PATH", volumeMountPath)
                    .withStartingCommandLine(commandLineArgs.toString()).attach().withDnsPrefix(aciName)
                    .withRestartPolicy(ContainerGroupRestartPolicy.NEVER).create();

Azure CLI

az container create --resource-group --name --image --cpu 1 --memory 1.5 --registry-login-server --registry-username --registry-password --azure-file-volume-share-name --azure-file-volume-account-name > --azure-file-volume-account-key --azure-file-volume-mount-path --restart-policy Never --e --subnet --subnet-address-prefix --vnet --vnet-name --subscription --command-line ""

Unable to attach the vnet and subnet configurations while creating azure container instances.


Solution

  • Unfortunately, it seems that deploy ACI in Vnet is not supported in Java currently. You can take a look at the issue in Github.

    If you take a look at the CLI command which can achieve it, then you will find the command actually use the Azure REST API to do that.

    enter image description here

    In the request, it set the container group property networkProfile with the subnet network profile. So if you really want to deploy the ACI in the Vnet, you could use the Azure REST API for Container Instance to achieve it in the Java code.