I need to create Azure container group with 1 container instance which will be integrated in a specific Vnet using python. But I didn't find anything useful, can anyone help me ?
Asked
Active
Viewed 624 times
0
-
You can try using the [`begin_create_or_update`](https://learn.microsoft.com/en-us/python/api/azure-mgmt-containerinstance/azure.mgmt.containerinstance.operations.containergroupsoperations?view=azure-python#begin-create-or-update-resource-group-name--container-group-name--container-group----kwargs-) method under the `ContainerGroupsOperations` Class in the `azure.mgmt.containerinstance.operations` module of Azure python SDK – Srijit_Bose-MSFT Oct 07 '21 at 15:44
-
Please check the [`ContainerGroup` Class Parameters](https://learn.microsoft.com/en-us/python/api/azure-mgmt-containerinstance/azure.mgmt.containerinstance.models.containergroup?view=azure-python) for more information. The parameter `subnet_ids` of the `ContainerGroup` object defines the subnet resource IDs for a container group. Reference: https://learn.microsoft.com/en-us/python/api/azure-mgmt-containerinstance/azure.mgmt.containerinstance.models.containergroupsubnetid?view=azure-python – Srijit_Bose-MSFT Oct 07 '21 at 15:48
-
@Srijit_Bose-MSFT I am new in azure, what is the id in ContainerGroupSubnetId constructor ? where do i find it exactly – Hkni Oct 07 '21 at 16:05
-
Every resource in Azure has a unique resource ID in the format like `/subscriptions/{subscription-id}/resourceGroups/{Resource-Group-Name}/providers/{ResourceProvider}/{ResourceType}/{ResourceName}`. If you want to deploy the Azure Container Instance in an existing Azure Virtual network subnet then you must provide the subnet's resource ID which should look similar to `/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/RGName/providers/Microsoft.Network/virtualNetworks/VnetName/subnets/SubnetName` – Srijit_Bose-MSFT Oct 07 '21 at 16:54
-
For e.g., the following [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/what-is-azure-cli) command returns the resource ID of a subnet `test` in the `test-net` virtual network located in `test-rg` resource group: `az network vnet subnet show -g test-rg --vnet-name test-net -n test --query id -o tsv` – Srijit_Bose-MSFT Oct 07 '21 at 17:00
-
You can use the [get](https://docs.microsoft.com/en-us/python/api/azure-mgmt-network/azure.mgmt.network.v2020_06_01.operations.subnetsoperations?view=azure-python#get-resource-group-name--virtual-network-name--subnet-name--expand-none----kwargs-) method in `SubnetsOperations` Class of `azure.mgmt.network.v2020_06_01.operations` module of the Azure Python SDK to return an object of type [Subnet](https://docs.microsoft.com/en-us/python/api/azure-mgmt-network/azure.mgmt.network.v2020_06_01.models.subnet?view=azure-python). `$result.id` will give you the resource ID of the desired subnet. – Srijit_Bose-MSFT Oct 07 '21 at 17:10
-
I couldnt import 'ContainerGroupSubnetId' from azure.mgmt.containerinstance.models to try your suggestion. :/ – Hkni Oct 08 '21 at 12:55
-
@AnsumanBal-MT yes I know I just still have errors so I cannot say if this is really the solution for my problem – Hkni Oct 09 '21 at 10:13
1 Answers
0
You can get the delegated subnet to be used by container group using the below code :
from azure.identity import AzureCliCredential
from azure.mgmt.network import NetworkManagementClient
credential = AzureCliCredential()
subscription_id = "948d4068-xxxxxx-xxxxx-xxxxx-e00a844e059b"
#credential = DefaultAzureCredential
network_client = NetworkManagementClient(credential, subscription_id)
resource_group_name = "ansumantest"
location = "West US 2"
virtual_network_name = "ansuman-vnet"
subnet_name = "acisubnet"
Subnet=network_client.subnets.get(resource_group_name, virtual_network_name, subnet_name)
print(Subnet.id)
Output:
You can add something similar as given below as per your requirement to the above code provided to get the subnetid:
from azure.mgmt.containerinstance import ContainerInstanceManagementClient
from azure.mgmt.containerinstance.models import (ContainerGroup,
Container,
ContainerGroupNetworkProtocol,
ImageRegistryCredential,
ContainerPort,
IpAddress,
Port,
ResourceRequests,
ResourceRequirements,
ContainerGroupSubnetId,
OperatingSystemTypes)
subnet_id = Subnet.id
container_client = ContainerInstanceManagementClient(credential,subscription_id)
container_image_name = "your private image present in acr "
user_name = "username for the server"
password= "password for the server"
# Configure the container
container_resource_requests = ResourceRequests(memory_in_gb=1, cpu=1.0)
container_resource_requirements = ResourceRequirements(requests=container_resource_requests)
container = Container(name=container_group_name,image=container_image_name,resources=container_resource_requirements,ports=[ContainerPort(port=80)])
# Configure the container group
ports = [Port(protocol=ContainerGroupNetworkProtocol.tcp, port=80)]
group_ip_address = IpAddress(ports=ports,type="Private")
acisubnet = ContainerGroupSubnetId(id=subnet_id,name=subnet_name)
imagecredentials= ImageRegistryCredential(server="server.azurecr.io",username=user_name,password=password)
container_group= ContainerGroup(location=location,containers=[container], os_type=OperatingSystemTypes.WINDOWS,ip_address=group_ip_address,subnet_ids=[acisubnet],image_registry_credentials=imagecredentials)
# Create the container group
container_client.container_groups.begin_create_or_update(resource_group_name,container_group_name,container_group)
Note:
If you are creating a container group in your vnet then you won't be able to have public access anymore for the container group and you can't use a public image (error below) . You have to only use your private image present on your Azure container registry.
If you are not sure about subnet delegation then please go to your
vnet>>subnet>>click on subnet to be used by containerGroup>>select the subnet delegation as show in image>>save

Ansuman Bal
- 9,705
- 2
- 10
- 27
-
the subnets.get have been given me this error: HttpResponseError: (InvalidApiVersionParameter) The api-version '2021-02-01' is invalid. The supported versions are '2021-04-01,2021-01-01,2020-10-01,2020-09-01,2020-08-01,2020-07-01,2020-06-01,2020-05-01,2020-01-01,2019-11-01,2019-10-01,2019-09-01,2019-08-01,2019-07-01,2019-06-01,2019-05-10,2019-05-01,2019-03-01,2018-11-01,2018-09-01,2018-08-... – Hkni Oct 09 '21 at 10:11
-
@Houdakilani,can you try reinstalling the network module and then try the code . – Ansuman Bal Oct 09 '21 at 11:40
-
The same, can you please share the versions you are using with pip freeze ? – Hkni Oct 09 '21 at 12:07
-
Hello @Houdakilani , I am using the following versions of the models as shown in  – Ansuman Bal Oct 11 '21 at 05:20