Hello Sebastian Pacheco,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
Problem
I understand that you are having Error inesperado: 'DefaultAzureCredential' object has no attribute 'signed_session' in your Azure batch pool function.
Solution
You need to understand that Azure Batch SDK does not use this credential adapter out of the box, that's what caused the error because the Azure Batch SDK expects credentials that provide a different API than what DefaultAzureCredential can provides. https://learn.microsoft.com/en-us/python/api/overview/azure/batch?view=azure-python and https://learn.microsoft.com/en-us/python/api/azure-batch/azure.batch.batch_auth.sharedkeycredentials?view=azure-python
So, as a best practices Azure Batch traditionally uses shared key credentials for authentication and this is to ensure compatibility with the Azure Batch SDK, I will advise you to use SharedKeyCredentials instead of DefaultAzureCredential.
Though, you might find an option to wrap the DefaultAzureCredential using an adapter that provides the expected API for the Azure Batch client as I have seen some developers do, but I am not sure the later end issue. Although, default credential capable of handling most Azure SDK authentication scenarios but here https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python explains its class and intended usage
Now! So far that you can make sure that the subnet you specify is properly set up and that the Batch pool can communicate with the Azure Key Vault through the network and the Azure Function’s managed identity or any service principal you use has the necessary roles assigned for example, Contributor Role on the Azure Batch account and Network Contributor Role on the virtual network and subnet. You're good.
See the below modified version of your code to create_batch_pool function to use shared key authentication as I said earlier:
import azure.batch as batch
import azure.batch.models as batch_models
from azure.batch.batch_auth import SharedKeyCredentials
def create_batch_pool():
account_url = 'https://<URL>.<location>.batch.azure.com'
account_name = '<your_batch_account_name>'
account_key = '<your_batch_account_key>'
try:
# Use SharedKeyCredentials for authentication with Azure Batch
credentials = SharedKeyCredentials(account_name, account_key)
batch_client = batch.BatchServiceClient(credentials, batch_url=account_url)
pool_id = 'homa_nodos_batch'
vm_size = 'STANDARD_D2S_V3'
node_count = 1
# Configuration of the virtual network and subnet
vnet_id = '/subscriptions/<id-subscriptions>/resourceGroups/<resources-group>/providers/Microsoft.Network/virtualNetworks/vnet-nodos-batch'
subnet_id = f'{vnet_id}/subnets/default'
new_pool = batch_models.PoolAddParameter(
id=pool_id,
display_name='homa_nodos_batch',
vm_size=vm_size,
virtual_machine_configuration=batch_models.VirtualMachineConfiguration(
image_reference=batch_models.ImageReference(
publisher='microsoftwindowsserver',
offer='windowsserver',
sku='2019-datacenter',
version='latest'
),
node_agent_sku_id='batch.node.windows amd64',
os_disk=batch_models.OSDisk(
caching='none',
managed_disk=batch_models.ManagedDisk(
storage_account_type='premium_lrs'
)
),
node_placement_configuration=batch_models.NodePlacementConfiguration(
policy='Regional'
)
),
resize_timeout='PT15M',
target_dedicated_nodes=node_count,
target_low_priority_nodes=0,
enable_auto_scale=False,
enable_inter_node_communication=False,
network_configuration=batch_models.NetworkConfiguration(
subnet_id=subnet_id,
public_ip_address_configuration=batch_models.PublicIPAddressConfiguration(
provision='batchmanaged'
)
),
start_task=batch_models.StartTask(
command_line='cmd /c "python-3.12.4-amd64.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 && pip install pymysql psycopg2-binary sqlalchemy pandas mysql-connector-python azure-identity azure-keyvault-secrets && powershell.exe -ExecutionPolicy Bypass -File variables.ps1"',
resource_files=[
batch_models.ResourceFile(
http_url='https://<blobname>.blob.core.windows.net/script/python-3.12.4-amd64.exe<SAS>',
file_path='python-3.12.4-amd64.exe'
),
batch_models.ResourceFile(
http_url='https://<blobname>.blob.core.windows.net/script/variables_entorno.ps1<SAS>',
file_path='variables.ps1'
)
],
user_identity=batch_models.UserIdentity(
auto_user=batch_models.AutoUserSpecification(
scope='pool',
elevation_level='admin'
)
),
max_task_retry_count=3,
wait_for_success=True
),
task_slots_per_node=1,
task_scheduling_policy=batch_models.TaskSchedulingPolicy(
node_fill_type='Pack'
),
target_node_communication_mode='default'
)
batch_client.pool.add(new_pool)
logging.info(f"Pool {pool_id} created with {node_count} nodes of size {vm_size}.")
except batch_models.BatchErrorException as e:
logging.error(f"Error creating the pool: {e}")
raise
except Exception as e:
logging.error(f"Unexpected error: {e}")
raise
With the above, test the function it should create the Batch pool as expected and should be correctly associated with the Virtual Network.
Accept Answer
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.
Best Regards,
Sina Salam