繁体   English   中英

预配 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 提供资源时,它会出于这些目的将后缀附加到末尾,并且因为我使用resourceGroup.Id.Apply(id => $"{id}/providers/Microsoft.Network/applicationGateways/{appGWName}/frontendPorts/appgwfp80")在我将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
});

此问题中跟踪了潜在的改进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM