How to pass environment variable into AzureML components when using yaml schema?

Ben Hadad 0 Reputation points
2025-03-23T09:19:18.0833333+00:00

Using the command, there is a parameter called environment_variables

https://learn.microsoft.com/en-us/python/api/azure-ai-ml/azure.ai.ml.entities.command?view=azure-python

However, I couldn't find a similar behavior when defining components with a YAML file.

Thanks

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,232 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 31,391 Reputation points
    2025-03-23T21:44:35.5866667+00:00

    Hello Ben !

    Thank you for posting on Microsoft Learn.

    You're right in AzureML), when using the Python SDK, you can directly set environment_variables. But when you're defining components using YAML, it's a bit different.

    You can define environment variables inside the environment section of your YAML component definition using the environment_variables field.

    $schema: https://azuremlschemas.azureedge.net/latest/commandComponent.schema.json
    name: my_component
    version: 1
    type: command
    inputs:
      input_path: 
        type: uri_file
    outputs:
      output_path:
        type: uri_folder
    environment: 
      docker:
        image: mcr.microsoft.com/azureml/base:latest
      environment_variables:
        MY_ENV_VAR_1: "hello"
        MY_ENV_VAR_2: "world"
    code: ./src
    command: >
      python main.py --input_path ${{inputs.input_path}} --output_path ${{outputs.output_path}}
    

    If you're using a registered AzureML environment, you can define the environment variables in the environment YAML file when registering the environment.

    name: my-env
    version: 1
    image: mcr.microsoft.com/azureml/base:latest
    environment_variables:
      MY_ENV_VAR_1: "hello"
      MY_ENV_VAR_2: "world"
    
    

    Then in your component YAML, just reference this environment:

    environment: azureml:my-env:1
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.