Search code examples
pythonazureazure-container-instancesvnet

How to create new container group in azure vnet using python


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 ?


Solution

  • 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:

    enter image description here

    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.

      enter image description here

    • 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

      enter image description here