Manage traffic manager profiles using Ruby

Letzte Aktualisierung: 17.10.2017
In GitHub bearbeiten

This sample demonstrates how to manage Azure traffic manager profiles using the Ruby SDK.

On this page

Run this sample

  1. If you don't already have it, install Ruby and the Ruby DevKit.

  2. If you don't have bundler, install it.

    gem install bundler
  3. Clone the repository.

    git clone
  4. Install the dependencies using bundle.

    cd traffic-manager-ruby-manage-profiles
    bundle install
  5. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  6. Set the following environment variables using the information from the service principle that you created.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}

    [AZURE.NOTE] On Windows, use set instead of export.

  7. Run the sample.

    bundle exec ruby example.rb

What is example.rb doing?

This sample starts by setting up ResourceManagementClient and TrafficeManager objects using your subscription and credentials.

# Create the Resource Manager Client with an Application (service principal) token provider
subscription_id = ENV['AZURE_SUBSCRIPTION_ID'] || '11111111-1111-1111-1111-111111111111' # your Azure Subscription Id
provider =
credentials =

# resource client
resource_client =
resource_client.subscription_id = subscription_id

# traffic manager client
traffic_manager_client =
traffic_manager_client.subscription_id = subscription_id

It registers the subscription for the "Microsoft.Media" namespace and creates a resource group.

# Register subscription for 'Microsoft.Media' namespace
provider = resource_client.providers.register('Microsoft.Media')

# Create a resource group
resource_group_params = do |rg|
    rg.location = REGION

resource_group = resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME, resource_group_params)

There are a couple of supporting functions (print_item and print_properties) that print a resource group and it's properties. With that set up, the sample lists all resource groups for your subscription, it performs these operations.

Create a profile

param =
param.location = 'global'
param.traffic_routing_method = 'Performance'
param.dns_config = do |dns_config|
    dns_config.relative_name = PROFILE_NAME
    dns_config.ttl = 30
param.monitor_config = do |monitor_config|
    monitor_config.protocol = 'HTTP'
    monitor_config.port = 80
    monitor_config.path = '/sample_monitor_page'

profile = traffic_manager_client.profiles.create_or_update(RESOURCE_GROUP_NAME, PROFILE_NAME, param)

List profiles

profile_list_result = traffic_manager_client.profiles.list_all

Delete a profile

traffic_manager_client.profiles.delete(RESOURCE_GROUP_NAME, PROFILE_NAME)