【问题标题】:How to reference sub resource when provisioning Azure Application Gateway instance预配 Azure 应用程序网关实例时如何引用子资源
【发布时间】:2021-12-04 18:51:30
【问题描述】:

当我尝试使用以下代码预配 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 可以通过删除配置的资源来应用更改?

感谢您的所有建议。

【问题讨论】:

    标签: c# azure-application-gateway pulumi


    【解决方案1】:

    目前没有其他方法可以构建 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 中跟踪了潜在的改进。

    【讨论】:

    • 感谢您的回答。
    猜你喜欢
    • 2021-02-05
    • 2018-11-24
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2012-02-08
    • 1970-01-01
    相关资源
    最近更新 更多