pulumi-azure-native: Circular dependency between WebAppHostNameBinding and Certificate

I’m trying to create a add custom domain to WebApp and have it use a managed certificate created by azure. This can be done with these two types:

This example shows how to do it, but there is a big issue:

const subdomain = `dummy`;

const verificationRecord = new cloudflare.Record("TXT verification record", {
  zoneId: cloudflareZoneId,
  name: `asuid.${subdomain}`,
  type: "TXT",
  value: `${azureAppVerificationToken}`,
});

const app = new web.WebApp(
  "Docker App",
  {
    name: `pulumi-config`,
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    serverFarmId: plan.id,
    siteConfig: {
      alwaysOn: true,
      linuxFxVersion: `DOCKER|${dockerImage}`,
    },
  },
  { dependsOn: verificationRecord }
);

const dnsRecord = new cloudflare.Record("CNAME record", {
  name: subdomain,
  zoneId: cloudflareZoneId,
  type: "CNAME",
  value: app.defaultHostName,
  ttl: 300,
  proxied: false,
});

const cert = new web.Certificate("Certificate", {
  name: "mycert",
  password: "xxxx",
  location: resourceGroup.location,
  resourceGroupName: resourceGroup.name,
  serverFarmId: plan.id,
  canonicalName: `${subdomain}.mydomain.com`,
});

const hostNameBinding = new web.WebAppHostNameBinding("custom domain binding", {
  name: "custom-domain-binding",
  resourceGroupName: resourceGroup.name,
  hostName: `${subdomain}.mydomain.com`,
  thumbprint: cert.thumbprint,
  sslState: "SniEnabled",
});

This would do exactly what i want, if I omit/comment the creation of the cert and the thumbprint in the hostNameBinding on the first run.

If I don’t do that, I get this:

  azure-nextgen:web/latest:Certificate (mycert):
    error: Code="BadRequest" Message="Properties.CanonicalName is invalid.  Certificate creation requires hostname dummy.mydomain.com added to an app in the serverFarmId /subscriptions/55f6bc5c-cb2d-4352-b37e-6b3c0854adcf/resourceGroups/pulumi-rg/providers/Microsoft.Web/serverfarms/linux-asp" Details=[{"Message":"Properties.CanonicalName is invalid.  Certificate creation requires hostname dummy.mydomain.com added to an app in the serverFarmId /subscriptions/55f6bc5c-cb2d-4352-b37e-6b3c0854adcf/resourceGroups/pulumi-rg/providers/Microsoft.Web/serverfarms/linux-asp"},{"Code":"BadRequest"},{"ErrorEntity":{"Code":"BadRequest","ExtendedCode":"51021","Message":"Properties.CanonicalName is invalid.  Certificate creation requires hostname dummy.mydomain.com added to an app in the serverFarmId /subscriptions/55f6bc5c-cb2d-4352-b37e-6b3c0854adcf/resourceGroups/pulumi-rg/providers/Microsoft.Web/serverfarms/linux-asp","MessageTemplate":"{0} is invalid.  {1}","Parameters":["Properties.CanonicalName","Certificate creation requires hostname dummy.mydomain.com added to an app in the serverFarmId /subscriptions/55f6bc5c-cb2d-4352-b37e-6b3c0854adcf/resourceGroups/pulumi-rg/providers/Microsoft.Web/serverfarms/linux-asp"]}}]

So the problem is, that we have circular dependency between two resources. As we want to run pulumi without any manual interaction at all, this is not going to work - having to comment/uncomment resources in the correct order is just not practical.

I understand this is bad API design on Azure/MS site, but I kind of expect Pulumi provide a way to work around this. Maybe some post create action on the Certificate that allows to update an existing resource again?

How can this be done?

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 18
  • Comments: 17 (1 by maintainers)

Most upvoted comments

Hello, We have identified a solution.

When you declare the code for WebAppHostNameBinding, don’t specify the value ‘web.SslState.SniEnabled,’ for SslState property.

In place, specify the ‘web.SslState.Disabled’ value.

This way, the WebAppHostNameBinding can be deployed without asking for a certificate thumbprint.

Then after creating the certificate and then the certificateBinding (in Azure Classic) it is ok : Azure switches the sslState to ‘SniEnabled’ automatically in background (this can be seen and verified in Azure portal and Arm Template).

The only thing that needs to be deployed (prerequisite) for the Custom Domain to be created without error are the DNS records (asuid txt record and cname record to the WebApp fqdn).

You don’t have to modify the code along the way, deploy resources in 2 times or make some changes manually through Azure portal. Deployment takes place in one go.

[Pulumi Typescript example]

import * as web from '@pulumi/azure-native/web'
import * as appservice from '@pulumi/azure/appservice'

import { fqdn, rg } from './common'
import { appServicePlan } from './plan'
import { app } from './webapp'

export const customDomain = new web.WebAppHostNameBinding(
    `custom-domain-${fqdn}`,
    {
        hostName: fqdn,
        resourceGroupName: rg.name,
        name: app.name,
        sslState: web.SslState.Disabled, // Do not specify 'SniEnabled',
        hostNameType: web.HostNameType.Verified,
        siteName: app.name,
    },
    {
        ignoreChanges: ['sslstate', 'thumbprint'],
    },
)

export const cert = new web.Certificate(
    `cert-${fqdn}`,
    {
        canonicalName: fqdn,
        resourceGroupName: rg.name,
        serverFarmId: appServicePlan.id,
    },
    {
        dependsOn: [customDomain],
    },
)

new appservice.CertificateBinding(
    `custom-domain-binding-${fqdn}`,
    {
        hostnameBindingId: customDomain.id,
        certificateId: cert.id,
        sslState: 'SniEnabled',
    },
    {
        ignoreChanges: ['hostnameBindingId'],
    },
)

Any update or fix for this?

Thanks for that @donniehale-awh , it got me unblocked ❤️

I went an extra step and make a component resource for it - an SSLAppServiceBinding, if anyone wants it, its yours:

using System;
using Pulumi;
using Pulumi.AzureNative.Web;

public class SslAppServiceBindingArgs : ResourceArgs
{
    public Input<string> AppServiceName { get; set; }
    public Input<string> ResourceGroupName { get; set; }
    public Input<string> HostName { get; set; }

    public Input<string> ServerFarmId { get; set; }
}

/// <summary>
/// Does some tricky logic to create a binding and certificate for an app service with SSL.
/// Requires a double `pulumi up`
///
/// See https://github.com/pulumi/pulumi-azure-native/issues/578
/// </summary>
public class SslAppServiceBinding : ComponentResource
{
    public SslAppServiceBinding(string name, SslAppServiceBindingArgs args, ComponentResourceOptions? options = null)
        : base("custom:azure:SslAppServiceBinding", name, args, options)
    {
        var inputArgs = Output.Tuple(args.HostName, args.AppServiceName, args.ResourceGroupName);

        // We don't have access to the app service or resource group names until
        // "Apply" can resolve them.
        //
        // And we can't use the non-async version of "Invoke" because the exception
        // thrown when the resource isn't found in that version aborts the program
        // immediately - "catch" doesn't work.
        var exists = inputArgs.Apply(args =>
        {
            var bindingLookupArgs = new GetWebAppHostNameBindingArgs
            {
                HostName = args.Item1,
                Name = args.Item2,
                ResourceGroupName = args.Item3,
            };

            try
            {
                var bindingLookupResult = GetWebAppHostNameBinding.InvokeAsync(bindingLookupArgs).Result;
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        });

        exists.Apply(canAddSsl =>
        {
            if (canAddSsl)
            {
                // If we get here, the initial binding exists; so we can create the certificate
                // and update the binding
                var managedCert = new Certificate($"{name}-cert", new()
                {
                    ServerFarmId = args.ServerFarmId,
                    CanonicalName = args.HostName,
                    HostNames = new[]
                    {
                        args.HostName,
                    },
                    ResourceGroupName = args.ResourceGroupName
                }, new() { Parent = this });

                var customDomain = new WebAppHostNameBinding($"{name}-binding", new()
                {
                    HostName = args.HostName,
                    HostNameType = HostNameType.Verified,
                    ResourceGroupName = args.ResourceGroupName,
                    Name = args.AppServiceName,
                    SiteName = args.AppServiceName,
                    SslState = SslState.SniEnabled,
                    Thumbprint = managedCert.Thumbprint
                }, new() { Parent = this });
                return customDomain;
            }
            else
            {
                // If we get here, we create an initial binding for the FQDN so
                // that the subsequent "pulumi up" can create the certificate
                var customDomain = new WebAppHostNameBinding($"{name}-binding", new()
                {
                    HostName = args.HostName,
                    HostNameType = HostNameType.Verified,
                    ResourceGroupName = args.ResourceGroupName,
                    Name = args.AppServiceName,
                    SiteName = args.AppServiceName
                }, new() { Parent = this });
                return customDomain;
            }
        });
    }
}

And the usage:

var binding = new SslAppServiceBinding($"mybinding", new()
{
    HostName = domain,
    ResourceGroupName = resourceGroup.Name,
    AppServiceName = appService.Name,
    ServerFarmId = plan.Id
}, new() { Parent = app });

Still no replies from anyone with Pulumi - even a comment like “we’re not going to address this until Microsoft does” would be helpful.

In case anyone finds this useful, here’s the “two pulumi up” workaround in C#.

// EXPLANATION OF WHAT'S GOING ON HERE...
//
// For background, see: https://github.com/pulumi/pulumi-azure-native/issues/578
//
// TL;DR - Azure has a circular dependency between WebAppHostNameBinding and Certificate
// a) A binding for the FQDN must exist to create a certificate for that FQDN
// b) For the binding to be TLS-enabled, it needs the thumbprint of the certificate
//
// The solution here requires *TWO* "pulumi up" executions:
// 1) Determines that the binding does not exist, so creates it without a thumbprint
// 2) Once the binding exist, creates the certificate and updates the binding with the thumbprint
// (Technically, per Pulumi, it replaces the binding)

var customDnsFqdn = config.Require("CustomDnsFqdn");
// apiAppService and rgName are available in this scope

try
{
	var inputArgs = Output.Tuple<string, string>(apiAppService.Name, rgName);

	// We don't have access to the app service or resource group names until
	// "Apply" can resolve them.
	//
	// And we can't use the non-async version of "Invoke" because the exception
	// thrown when the resource isn't found in that version aborts the program
	// immediately - "catch" doesn't work.
	inputArgs.Apply(args =>
	{
		var bindingLookupArgs = new GetWebAppHostNameBindingArgs
		{
			HostName = customDnsFqdn,
			Name = args.Item1,
			ResourceGroupName = args.Item2,
		};

		var bindingLookupResult = GetWebAppHostNameBinding.InvokeAsync(bindingLookupArgs).Result;

		return "found";
	});

	// If we get here, the initial binding exists; so we can create the certificate
	// and update the binding
	var managedCert = new Certificate("mycertname", new()
	{
		ServerFarmId = apiAppService.ServerFarmId!,
		CanonicalName = customDnsFqdn,
		HostNames = new[]
		{
			customDnsFqdn,
		},
		Location = "eastus",
		ResourceGroupName = rgName
	});

	var customDomain = new WebAppHostNameBinding("mybindingname", new()
	{
		HostName = customDnsFqdn,
		HostNameType = HostNameType.Verified,
		ResourceGroupName = rgName,
		Name = apiAppService.Name,
		SiteName = apiAppService.Name,
		SslState = SslState.SniEnabled,
		Thumbprint = managedCert.Thumbprint
	});
}
catch
{
	Log.Info($"***** WebAppHostNameBinding DOES NOT EXIST *****");
	// Do nothing - we've verified that the resource doesn't exist

	// If we get here, we create an initial binding for the FQDN so
	// that the subsequent "pulumi up" can create the certificate
	var prodCustomDomain = new WebAppHostNameBinding("mybindingname", new()
	{
		HostName = customDnsFqdn,
		HostNameType = HostNameType.Verified,
		ResourceGroupName = rgName,
		Name = apiAppService.Name,
		SiteName = apiAppService.Name
	});
}

Also having this issue. We are unable to use traffic managers over https routing to WebApps in Azure; we have tried solving the circular dependency between the binding and the certificate without much luck.

Running into this same issue as well.

It’s a bit hacky, but as a workaround I pass an ‘is-initial-create’ config value to the stack. When deploying a new stack for the first time, I call pulumi up twice, first with is-initial-create=true (which triggers logic in the stack to create the WebAppHostNameBinding first without the Thumbprint and the Certificate second), and then second with is-initial-create=false (which triggers logic in the stack to create the Certificate first and the WebAppHostNameBinding second with the certificate thumbprint output). Running pulumi up twice is only required for deploying a new stack - moving forward, pulumi up can be called a single time (always with is-initial-create=false).

@mikhailshilkov this now solved on terraform site: a new resource azurerm_app_service_certificate_binding has been introduced to break the circular dependency:

I guess this could be used in the “old azure provider” - but how can we fix this issue with the nextgen provider?

@markodjukic I see where you’r going with this and yes, this might actually work - but as you say this is also a workaround and I just hope we get a better, proper solution to this any time soon.

Running into same chicken-and-egg situation here. Hate it, but for now going back to Pulumi Terraform provider.