如何使用 Pulumi 创建 Azure 混合连接?

How do you create an Azure Hybrid Connection using Pulumi?

先决条件:

使用他们网站 (https://www.pulumi.com/docs/reference/pkg/azure/appservice/hybridconnection/) 提供的示例:

using Pulumi;
using Azure = Pulumi.Azure;
using Pulumi.AzureNextGen; 

class MyStack : Stack
{
    public MyStack()
    {
        var exampleResourceGroup = new Azure.Core.ResourceGroup("exampleResourceGroup", new Azure.Core.ResourceGroupArgs
        {
            Location = "West Europe",
        });
        var examplePlan = new Azure.AppService.Plan("examplePlan", new Azure.AppService.PlanArgs
        {
            Location = exampleResourceGroup.Location,
            ResourceGroupName = exampleResourceGroup.Name,
            Sku = new Azure.AppService.Inputs.PlanSkuArgs
            {
                Tier = "Standard",
                Size = "S1",
            },
        });
        var exampleAppService = new Azure.AppService.AppService("exampleAppService", new Azure.AppService.AppServiceArgs
        {
            Location = exampleResourceGroup.Location,
            ResourceGroupName = exampleResourceGroup.Name,
            AppServicePlanId = examplePlan.Id,
        });
        var exampleNamespace = new Azure.Relay.Namespace("exampleNamespace", new Azure.Relay.NamespaceArgs
        {
            Location = exampleResourceGroup.Location,
            ResourceGroupName = exampleResourceGroup.Name,
            SkuName = "Standard",
        });
        var exampleHybridConnection = new Azure.Relay.HybridConnection("exampleHybridConnection", new Azure.Relay.HybridConnectionArgs
        {
            ResourceGroupName = exampleResourceGroup.Name,
            RelayNamespaceName = exampleNamespace.Name,
            UserMetadata = "examplemetadata",
        });
        var exampleAppservice_hybridConnectionHybridConnection = new Azure.AppService.HybridConnection("exampleAppservice/hybridConnectionHybridConnection", new Azure.AppService.HybridConnectionArgs
        {
            AppServiceName = exampleAppService.Name,
            ResourceGroupName = exampleResourceGroup.Name,
            RelayId = exampleHybridConnection.Id,
            Hostname = "testhostname.example",
            Port = 8080,
            SendKeyName = "RootManageSharedAccessKey",
        });
    }

}

我收到以下错误:

错误:无法列出命名空间“XXX”(资源组“XXX”)的访问密钥:relay.NamespacesClient#ListKeys:响应请求失败:StatusCode=404 -- 原始错误:autorest/azure:无法解析错误响应:“”请求的资源 defaultSender 不存在。”错误:json:无法将字符串解组为 azure.RequestError

类型的 Go 值

为了解决这个问题,我必须添加中继命名空间授权规则:

using Pulumi;
using Azure = Pulumi.Azure;
using Pulumi.AzureNextGen;

class MyStack : Stack
{
    public MyStack()
    {
        var exampleResourceGroup = new Azure.Core.ResourceGroup("exampleResourceGroup", new Azure.Core.ResourceGroupArgs
        {
            Location = "West Europe",
        });
        var examplePlan = new Azure.AppService.Plan("examplePlan", new Azure.AppService.PlanArgs
        {
            Location = exampleResourceGroup.Location,
            ResourceGroupName = exampleResourceGroup.Name,
            Sku = new Azure.AppService.Inputs.PlanSkuArgs
            {
                Tier = "Standard",
                Size = "S1",
            },
        });
        var exampleAppService = new Azure.AppService.AppService("exampleAppService", new Azure.AppService.AppServiceArgs
        {
            Location = exampleResourceGroup.Location,
            ResourceGroupName = exampleResourceGroup.Name,
            AppServicePlanId = examplePlan.Id,
        });
        var exampleNamespace = new Azure.Relay.Namespace("exampleNamespace", new Azure.Relay.NamespaceArgs
        {
            Location = exampleResourceGroup.Location,
            ResourceGroupName = exampleResourceGroup.Name,
            SkuName = "Standard",
        });

var defaultSenderAuthorizationRule = new Pulumi.AzureNextGen.Relay.Latest.NamespaceAuthorizationRule("defaultSenderAuthorizationRule",
                new AzureProviderNextGen.Relay.Latest.NamespaceAuthorizationRuleArgs
                {
                    AuthorizationRuleName = "defaultSender",
                    NamespaceName = exampleNamespace .Name,
                    ResourceGroupName = exampleNamespace .ResourceGroupName,
                    Rights =
                    {
                        "Manage",
                        "Listen",
                        "Send",
                    },
                });


        var exampleHybridConnection = new Azure.Relay.HybridConnection("exampleHybridConnection", new Azure.Relay.HybridConnectionArgs
        {
            ResourceGroupName = exampleResourceGroup.Name,
            RelayNamespaceName = exampleNamespace.Name,
            UserMetadata = "examplemetadata",
        });
        var exampleAppservice_hybridConnectionHybridConnection = new Azure.AppService.HybridConnection("exampleAppservice/hybridConnectionHybridConnection", new Azure.AppService.HybridConnectionArgs
        {
            AppServiceName = exampleAppService.Name,
            ResourceGroupName = exampleResourceGroup.Name,
            RelayId = exampleHybridConnection.Id,
            Hostname = "testhostname.example",
            Port = 8080,
            SendKeyName = "RootManageSharedAccessKey",
        });
    }

}

在此之后,它工作正常并且堆栈完成并表示我的 Appservice:HybridConnection 已创建。 然而,当我导航到应用程序服务 Network/HybridConnection 设置时,它不在那里。

预期行为:

成功创建 AppService.HybridConnection 后,它应该出现在 Azure 门户中应用服务的混合连接列表中。

实际行为:

AppService.HybridConnection创建成功但未出现在AppService\Hybrid连接列表

在查看 Azure Activity 日志时,似乎资源已创建,然后立即被迅速删除。

解决这个问题的方法是..

  1. 更新 Relay.HybridConnection 中的用户元数据,使其包含带有主机名和端口的端点密钥
var exampleHybridConnection = new Azure.Relay.HybridConnection("exampleHybridConnection", new Azure.Relay.HybridConnectionArgs
        {
            ResourceGroupName = exampleResourceGroup.Name,
            RelayNamespaceName = exampleNamespace.Name,
            UserMetadata = "[{\"key\":\"endpoint\",\"value\":\"testhostname.example:8080\"}]",
        });
  1. 将授权规则添加到 Relay.HybridConnection。
var defaultListenerRule = new Pulumi.AzureNextGen.Relay.Latest.HybridConnectionAuthorizationRule("defaultListener",
                new Pulumi.AzureNextGen.Relay.Latest.HybridConnectionAuthorizationRuleArgs
                {
                    AuthorizationRuleName = "defaultListener",
                    HybridConnectionName = exampleHybridConnection.Name,
                    NamespaceName = exampleHybridConnection.RelayNamespaceName,
                    ResourceGroupName = exampleHybridConnection.ResourceGroupName,
                    Rights =
                    {
                        "Listen",
                    },
                });

            var defaultSenderRule = new Pulumi.AzureNextGen.Relay.Latest.HybridConnectionAuthorizationRule("defaultSender",
                new Pulumi.AzureNextGen.Relay.Latest.HybridConnectionAuthorizationRuleArgs
                {
                    AuthorizationRuleName = "defaultSender",
                    HybridConnectionName = exampleHybridConnection.Name,
                    NamespaceName = exampleHybridConnection.RelayNamespaceName,
                    ResourceGroupName = exampleHybridConnection.ResourceGroupName,
                    Rights =
                    {
                        "Send",
                    },
                });
  1. 然后在 AppService.HybridConnection.
  2. 上添加 CustomResourceOptions 参数“DeleteBeforeReplace = true”
var exampleAppservice_hybridConnectionHybridConnection = new Azure.AppService.HybridConnection("exampleAppservice/hybridConnectionHybridConnection", new Azure.AppService.HybridConnectionArgs
            {
                AppServiceName = exampleAppService.Name,
                ResourceGroupName = exampleResourceGroup.Name,
                RelayId = exampleHybridConnection.Id,
                Hostname = "testhostname.example",
                Port = 8080,
                SendKeyName = "exampleSharedAccessKey",
            }, new CustomResourceOptions { DeleteBeforeReplace = true });

创建后,所有内容都将链接并显示在混合连接管理器和 Azure 的门户中。