配置 Azure 应用程序网关实例时如何引用子资源

How to reference sub resource when provisioning Azure Application Gateway instance

当我尝试使用以下代码配置 Azure 应用程序网关实例时,它以错误结束,指出无法找到前端端口 appgwfp80

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",
    },
});

据我了解,这是因为当 Pulumi 提供资源时,它会在 these purposes 的末尾附加后缀,并且因为我使用 resourceGroup.Id.Apply(id => $"{id}/providers/Microsoft.Network/applicationGateways/{appGWName}/frontendPorts/appgwfp80") 引用前端端口 ID,我将 agw-pt-dev-uks-001 作为 appGWName 找不到,因为应用程序网关名称设置为 agw-pt-dev-uks-001d7c2fa0.

当我明确设置 ApplicationGatewayName = "agw-pt-dev-uks-001" 时,问题就消失了。

我想为我的所有资源遵循一个命名约定,但现在当其他资源有此后缀时,应用程序网关的名称没有后缀。

是否有任何其他方式来引用子资源 ID,例如我可以以某种方式获取 Pulumi 生成的应用程序网关名称?或者我是否只需要为我的所有资源传递一个固定名称并用 DeleteBeforeReplace 标记它们以便在发生更改时 Pulumi 可以通过删除已配置的资源来应用更改?

感谢您的所有建议。

目前没有其他方法可以构建 ID,您需要明确设置 ApplicationGatewayName。如果你想要一个随机的后缀,你可以自己生成它

//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
});

this issue 中跟踪了潜在的改进。