Home > Blockchain >  How to reference sub resource when provisioning Azure Application Gateway instance
How to reference sub resource when provisioning Azure Application Gateway instance

Time:10-18

When I try to provision Azure Application Gateway instance with the following code it ends up with an error saying that fronted port appgwfp80 can't be found.

var appGWName = "agw-pt-dev-uks-001";
var applicationGateway = new ApplicationGateway(appGWName, new ApplicationGatewayArgs
{
    ResourceGroupName = resourceGroup.Name,
    BackendAddressPools = new ApplicationGatewayBackendAddressPoolArgs[]
    {
        new ()
        {
            Name = "appgwpool",
            BackendAddresses = new ApplicationGatewayBackendAddressArgs[] { new () { Fqdn = appService.DefaultSiteHostname }}
        }
    },
    BackendHttpSettingsCollection = ..,
    GatewayIPConfigurations = ..,
    FrontendPorts = 
    {
        new ApplicationGatewayFrontendPortArgs
        {
            Name = "appgwfp80",
            Port = 80,
        },
    },
    FrontendIPConfigurations = ..,
    HttpListeners = new ApplicationGatewayHttpListenerArgs[]
    {
        new ()
        {
            Name = "appgwhl",
            FrontendIPConfiguration = new SubResourceArgs
            {
                Id = resourceGroup.Id.Apply(id => $"{id}/providers/Microsoft.Network/applicationGateways/{appGWName}/frontendIPConfigurations/publicIp")
            },
            FrontendPort = new SubResourceArgs
            {
                Id = resourceGroup.Id.Apply(id => $"{id}/providers/Microsoft.Network/applicationGateways/{appGWName}/frontendPorts/appgwfp80")
            }
        }
    },
    RequestRoutingRules = ..,
    Sku = new ApplicationGatewaySkuArgs
    {
        Capacity = 1,
        Name = "Standard_v2",
        Tier = "Standard_v2",
    },
});

To my understanding, it's because when Pulumi provision resources it appends suffix to the end for these purposes and because I reference fronted port id with resourceGroup.Id.Apply(id => $"{id}/providers/Microsoft.Network/applicationGateways/{appGWName}/frontendPorts/appgwfp80") where I pass agw-pt-dev-uks-001 as appGWName it can't be found because Application Gateway name was set to something like agw-pt-dev-uks-001d7c2fa0.

The problem goes away when I explicitly set ApplicationGatewayName = "agw-pt-dev-uks-001".

I would like to follow one naming convention for all of my resources but now Application Gateway is provisioned without suffix in the name when other resources have this suffix in place.

Is there any other way to reference subresource id so for instance I can somehow get the Application Gateway name generated by Pulumi? Or do I just need to pass a fixed name for all of my resources and just mark them with DeleteBeforeReplace so in case of changes Pulumi can apply changes by removing provisioned resources?

Thanks for all of your suggestions.

CodePudding user response:

There's no other way to build the ID at the moment, and you need to set ApplicationGatewayName explicitly. If you want a random suffix, you could generate it yourself

//using Pulumi.Random;
var suffix = new RandomString("name", new RandomStringArgs
{
    Length = 8,
    Special = false,
    Upper = false
});
var resourceName = Output.Format($"appgw-{suffix.Result}");
var applicationGateway = new ApplicationGateway(appGWName, new ApplicationGatewayArgs
{
    ApplicationGatewayName = resourceName,
    // ... use the same technique to build IDs
});

A potential improvement is tracked in this issue.

  • Related