Jan_V
Jan_V

Reputation: 4406

Add Azure Traffic Manager endpoints in main bicep template as child resource

I'm trying to add a new Traffic Manager Profile via a Network/trafficManagerProfiles.bicep module which I invoke in my main.bicep file.
This works well.

The main.bicep file is creating multiple Function Apps using their own little module, which is invoked similar like this

module functionAppModule 'Web/functions.bicep' = {
  dependsOn: [
    appServicePlanModule
    webApiStorageAccount
  ]
  name: 'functionAppModule'
  params: {
    environmentName: environmentName
    systemName: systemName
    azureRegion: azureRegion
    appServicePlanId: appServicePlanModule.outputs.id
  }
}

Now I'm trying to add the necessary endpoints of my web applications (Azure Functions) to the Traffic Manager Profile, which is also possible by using the endpoints property.

However, this would mean I need to add a parameter to this file accepting an array of objects containing all information about my App Services, or I would need to resolve them over here (by retrieving the instances with the existing keyword). This doesn't sound like the way to implement this, because all those resources are already available/referenced in the main.bicep file.

The Traffic Manager Profile module now looks like this:

param systemName string
@allowed([
  'dev'
  'test'
  'acc'
  'prod'
])
param environmentName string
param relativeLiveEndpoint string = '/api/Live'

var trafficManagerProfileName = '${systemName}${environmentName}'

resource trafficManagerProfile 'Microsoft.Network/trafficmanagerprofiles@2018-08-01' = {
  name: trafficManagerProfileName
  location: 'global'
  properties: {
    allowedEndpointRecordTypes: [
      'DomainName'
    ]
    dnsConfig: {
      relativeName: trafficManagerProfileName
      ttl: 60
    }
    profileStatus: 'Enabled'
    trafficRoutingMethod: 'Performance'
    monitorConfig: {
      profileMonitorStatus: 'Online'
      protocol: 'HTTPS'
      port: 443
      path: relativeLiveEndpoint
      timeoutInSeconds: 10
      intervalInSeconds: 30
      toleratedNumberOfFailures: 3
    }
    endpoints: [
      {
        id: 'the resource id'
        name: 'the resource name'
        type: 'the resource type'
        properties: {
          endpointStatus: 'Enabled'
          endpointMonitorStatus: 'Online'
          targetResourceId: 'the resource id'
          target: 'mysite.azurewebsites.net'
          endpointLocation: 'West Europe'
          weight: 1
          priority: 1
        }
      }
      // All other app services
    ]
  }
}

According to some answers here on Stack Overflow, the endpoints can also be created via a child resource in ARM templates (something like Microsoft.Network/trafficmanagerprofiles/endpoints), however, this doesn't appear to be available in Bicep (or just isn't available in the autocomplete).

What I'd like to do is something like this in my main.bicep file, because that way I can reference all App Services I want to add.

var trafficManagerProfileEndpoints 'Microsoft.Network/trafficmanagerprofiles/endpoints@2050-01-01` = {
  name: '${trafficManagerProfile.Name}/endpoints'
  endpoints: [ 
    // All of my endpoints
  ]
}

Somewhat similar compared to what's available via Microsoft.Web/sites/config@2020-12-01 for configuration settings in an App Service.

Any ideas on this?

Upvotes: 2

Views: 907

Answers (2)

Thomas
Thomas

Reputation: 29542

This is possible through Bicep ARM but the Microsoft.Network/trafficManagerProfiles api doesn't have types available for endpoints so it will generate a warning even if the deployment works perfectly.

If you define a traffic manager profile without the endpoints property:

resource trafficManager 'Microsoft.Network/trafficmanagerprofiles@2018-08-01' = {
  name: '<name>'
  location: 'global'
  properties: {
    profileStatus: 'Enabled'
    trafficRoutingMethod: '<trafficRoutingMethod>'
    dnsConfig: {
      ...
    }
    monitorConfig: {
      ...
    }    
  }
}

You can then add endpoints like that:

// Azure endpoints: cloud service, app service, app service slots, public ip
resource trafficManagerAzureEndpoint 'Microsoft.Network/trafficManagerProfiles/azureEndpoints@2018-08-01' = {
  name: name
  parent: trafficManager 
  properties: {
    ...
  }
}

// External endpoint
resource trafficManagerExternalEndpoint 'Microsoft.Network/trafficManagerProfiles/externalEndpoints@2018-08-01' = {
  name: name
  parent: trafficManager 
  properties: {
    ...
  }
}

// Nested endpoints
resource trafficManagerNestedEndpoint 'Microsoft.Network/trafficManagerProfiles/nestedEndpoints@2018-08-01' = {
  name: name
  parent: trafficManager 
  properties: {
    ...
  }
}

Upvotes: 1

Eduard Keilholz
Eduard Keilholz

Reputation: 933

I just ran through your question at lightning speed, forgive me if I don't fully understand it, but it looks like you need to declare your web app as a resource again, but as existing. This allows you to access all the props you need from your app service.

This blog explains how to use existing resources in Bicep: https://hexmaster.nl/posts/bicep-existing/

Upvotes: 2

Related Questions