Skip to content

Streamline custom domain support when using PublishAsAzureContainerApp #6271

@mitchdenny

Description

@mitchdenny

Developers deploying a .NET Aspire application to Azure via AZD will often want to associate the container app resource with a custom domain name. This means that they need to create the necessary customizations on the ingress settings on the container app resource, but also set up a certificate in the in the managed environment.

This is a bit of a chicken and egg problem because in order to complete this process the DNS records need to exist which verify ownership and intent to service traffic via container apps. Specifically, there is a TXT record that is the hash of the resource owner's subscription ID and a seed value in addition to a CNAME record that points to *.trafficmanager.net (you can use a more specific endpoint but in this scenario you would use the traffic manager endpoint because you can't know the randomly assigned FQDN up front).

The code to configure the Bicep generation via Azure Provisioning looks something like this (still testing):

       .PublishAsAzureContainerApp((module, app) =>
       {
           var environment = ContainerAppManagedEnvironment.FromExisting("environment");
           environment.Name = app.EnvironmentId;
           module.Add(environment);

           var managedCert = new ContainerAppManagedCertificate("cert");
           managedCert.Parent = environment;
           managedCert.Properties = new ManagedCertificateProperties()
           {
               SubjectName = "mydomain.mycompany.com"
           };
           module.Add(managedCert);

           app.Configuration.Value!.Ingress!.Value!.CustomDomains = new Azure.Provisioning.BicepList<ContainerAppCustomDomain>()
           {
                new ContainerAppCustomDomain()
                {
                    BindingType = ContainerAppCustomDomainBindingType.SniEnabled,
                    CertificateId = managedCert.Id,
                    Name = "mydomain.mycompany.com"
                }
           };

           // Scale to 0
           app.Template.Value!.Scale.Value!.MinReplicas = 0;
       });

The code to generate the content for the TXT record is here:

using System.Security.Cryptography;
using System.Text;
 
var hash = SHA256.Create();
string uniqueId = "<subscription id>" + "282EF";
var hashed = hash.ComputeHash(Encoding.UTF8.GetBytes(uniqueId));
 
var sb = new StringBuilder();
 
foreach (var b in hashed)
{
    sb.Append(b.ToString("X2"));
}
 
Console.WriteLine(sb.ToString());

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-deploymentarea-integrationsIssues pertaining to Aspire Integrations packagesazureIssues associated specifically with scenarios tied to using Azure

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions