分享方式:


教學課程:使用 Go SDK 在 Azure 公用 MEC 中部署資源

在本教學課程中,了解如何使用 Go SDK 在 Azure 公用多接取邊緣運算 (MEC) 中部署資源。 本教學課程提供以 Go 撰寫的程式碼片段,以在 Azure 公用 MEC 解決方案中部署虛擬機器和公用 IP 資源。 您可以使用相同的模型和範本來部署 Azure 公用 MEC 支援的其他資源和服務。 本文不適合做為 Go 的教學課程;其僅著重於在 Azure 公用 MEC 中部署資源所需的 API 呼叫。

如需 Go 的詳細資訊,請參閱適用於 Go 開發人員的 Azure。 如需 Go 範例,請參閱 Azure Go SDK 範例

在本教學課程中,您會了解如何:

  • 建立虛擬機器
  • 建立公用 IP 位址
  • 部署虛擬網路和公用 IP 位址

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

  • 將加入允許清單的訂用帳戶新增至您的 Azure 帳戶,這可讓您在 Azure 公用 MEC 中部署資源。 如果您沒有作用中的允許訂用帳戶,請連絡 Azure 公用 MEC 產品小組

安裝 Go

您可以下載並安裝最新版的 Go。 其會取代您電腦上的現有 Go。 如果您想要在同一部機器上安裝多個 Go 版本,請參閱管理 Go 安裝

驗證

使用任何 Azure 服務之前,您必須先取得驗證。 您可以使用 Azure CLI 來登入或設定驗證環境變數。

使用 Azure CLI 登入

您可以在命令列中使用 az login,透過預設瀏覽器登入 Azure。 您可以在使用 Azure CLI 登入中找到詳細的指示。

設定環境變數

您需要下列值才能向 Azure 進行驗證:

  • 訂用帳戶識別碼
  • 用戶端識別碼
  • 用戶端祕密
  • 租用戶識別碼

遵循下列指示,從入口網站取得這些值:

  • 取得訂用帳戶識別碼

    1. 登入您的 Azure 帳戶
    2. 選取左側資訊看板的 [訂用帳戶]
    3. 選取所需的任何訂用帳戶
    4. 選取 [概觀]
    5. 複製訂閱識別碼
  • 取得用戶端識別碼 / 用戶端密碼 / 租用戶識別碼

    如需如何取得用戶端識別碼、用戶端密碼和租用戶識別碼的相關資訊,請參閱建立可存取資源的 Microsoft Entra 應用程式和服務主體

  • 設定環境變數

    取得值之後,您必須將下列值設定為環境變數:

    • AZURE_CLIENT_ID
    • AZURE_CLIENT_SECRET
    • AZURE_TENANT_ID
    • AZURE_SUBSCRIPTION_ID

    若要在您的開發系統上設定下列環境變數:

    Windows (需要系統管理員存取權)

    1. 開啟 [控制台]
    2. 選取 [系統安全性]>[系統]
    3. 選取左側的 [進階系統設定]
    4. 在 [系統屬性] 視窗中,選取 Environment Variables… 按鈕。
    5. 選取您想要變更的屬性,然後選取 [編輯...]。 如果未列出屬性名稱,請選取 [新增...]

    以 Linux 為基礎的作業系統

        export AZURE_CLIENT_SECRET="__CLIENT_SECRET__"
        export AZURE_TENANT_ID="__TENANT_ID__"
        export AZURE_SUBSCRIPTION_ID="__SUBSCRIPTION_ID__"````
    
    

Install the package

新的 SDK 會使用 Go 模組進行版本設定和相依性管理。

執行下列命令,以在您的專案資料夾下安裝本教學課程的套件:

go get github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5
go get github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v3
go get github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources
go get github.com/Azure/azure-sdk-for-go/sdk/azcore
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity

佈建虛擬機器

package main

import (
	"context"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v3"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
	"log"
	"os"
)

func main() {
	subscriptionId := os.Getenv("AZURE_SUBSCRIPTION_ID")

	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatalf("authentication failure: %+v", err)
	}

	// client factory
	resourcesClientFactory, err := armresources.NewClientFactory(subscriptionId, cred, nil)
	if err != nil {
		log.Fatalf("cannot create client factory: %+v", err)
	}

	computeClientFactory, err := armcompute.NewClientFactory(subscriptionId, cred, nil)
	if err != nil {
		log.Fatalf("cannot create client factory: %+v", err)
	}

	networkClientFactory, err := armnetwork.NewClientFactory(subscriptionId, cred, nil)
	if err != nil {
		log.Fatalf("cannot create client factory: %+v", err)
	}

	// Step 1: Provision a resource group
	_, err = resourcesClientFactory.NewResourceGroupsClient().CreateOrUpdate(
		context.Background(),
		"<resourceGroupName>",
		armresources.ResourceGroup{
			Location: to.Ptr("westus"),
		},
		nil,
	)
	if err != nil {
		log.Fatal("cannot create resources group:", err)
	}

	// Step 2: Provision a virtual network
	virtualNetworksClientCreateOrUpdateResponsePoller, err := networkClientFactory.NewVirtualNetworksClient().BeginCreateOrUpdate(
		context.Background(),
		"<resourceGroupName>",
		"<virtualNetworkName>",
		armnetwork.VirtualNetwork{
			Location: to.Ptr("westus"),
			ExtendedLocation: &armnetwork.ExtendedLocation{
				Name: to.Ptr("<edgezoneid>"),
				Type: to.Ptr(armnetwork.ExtendedLocationTypesEdgeZone),
			},
			Properties: &armnetwork.VirtualNetworkPropertiesFormat{
				AddressSpace: &armnetwork.AddressSpace{
					AddressPrefixes: []*string{
						to.Ptr("10.0.0.0/16"),
					},
				},
				Subnets: []*armnetwork.Subnet{
					{
						Name: to.Ptr("test-1"),
						Properties: &armnetwork.SubnetPropertiesFormat{
							AddressPrefix: to.Ptr("10.0.0.0/24"),
						},
					},
				},
			},
		},
		nil,
	)
	if err != nil {
		log.Fatal("network creation failed", err)
	}
	virtualNetworksClientCreateOrUpdateResponse, err := virtualNetworksClientCreateOrUpdateResponsePoller.PollUntilDone(context.Background(), nil)
	if err != nil {
		log.Fatal("cannot create virtual network:", err)
	}
	subnetID := *virtualNetworksClientCreateOrUpdateResponse.Properties.Subnets[0].ID

	// Step 3: Provision an IP address
	publicIPAddressesClientCreateOrUpdateResponsePoller, err := networkClientFactory.NewPublicIPAddressesClient().BeginCreateOrUpdate(
		context.Background(),
		"<resourceGroupName>",
		"<publicIPName>",
		armnetwork.PublicIPAddress{
			Name:     to.Ptr("<publicIPName>"),
			Location: to.Ptr("westus"),
			ExtendedLocation: &armnetwork.ExtendedLocation{
				Name: to.Ptr("<edgezoneid>"),
				Type: to.Ptr(armnetwork.ExtendedLocationTypesEdgeZone),
			},
			SKU: &armnetwork.PublicIPAddressSKU{
				Name: to.Ptr(armnetwork.PublicIPAddressSKUNameStandard),
			},
			Properties: &armnetwork.PublicIPAddressPropertiesFormat{
				PublicIPAllocationMethod: to.Ptr(armnetwork.IPAllocationMethodStatic),
			},
		},
		nil,
	)
	if err != nil {
		log.Fatal("public ip creation failed", err)
	}
	publicIPAddressesClientCreateOrUpdateResponse, err := publicIPAddressesClientCreateOrUpdateResponsePoller.PollUntilDone(context.Background(), nil)
	if err != nil {
		log.Fatal("cannot create public ip: ", err)
	}

	// Step 4: Provision the network interface client
	interfacesClientCreateOrUpdateResponsePoller, err := networkClientFactory.NewInterfacesClient().BeginCreateOrUpdate(
		context.Background(),
		"<resourceGroupName>",
		"<networkInterfaceName>",
		armnetwork.Interface{
			Location: to.Ptr("westus"),
			ExtendedLocation: &armnetwork.ExtendedLocation{
				Name: to.Ptr("<edgezoneid>"),
				Type: to.Ptr(armnetwork.ExtendedLocationTypesEdgeZone),
			},
			Properties: &armnetwork.InterfacePropertiesFormat{
				EnableAcceleratedNetworking: to.Ptr(true),
				IPConfigurations: []*armnetwork.InterfaceIPConfiguration{
					{
						Name: to.Ptr("<ipConfigurationName>"),
						Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
							Subnet: &armnetwork.Subnet{
								ID: to.Ptr(subnetID),
							},
							PublicIPAddress: &armnetwork.PublicIPAddress{
								ID: publicIPAddressesClientCreateOrUpdateResponse.ID,
							},
						},
					},
				},
			},
		},
		nil,
	)
	if err != nil {
		log.Fatal("interface creation failed", err)
	}
	interfacesClientCreateOrUpdateResponse, err := interfacesClientCreateOrUpdateResponsePoller.PollUntilDone(context.Background(), nil)
	if err != nil {
		log.Fatal("cannot create interface:", err)
	}

	// Step 5: Provision the virtual machine
	virtualMachinesClientCreateOrUpdateResponsePoller, err := computeClientFactory.NewVirtualMachinesClient().BeginCreateOrUpdate(
		context.Background(),
		"<resourceGroupName>",
		"<vmName>",
		armcompute.VirtualMachine{
			Location: to.Ptr("westus"),
			ExtendedLocation: &armcompute.ExtendedLocation{
				Name: to.Ptr("<edgezoneid>"),
				Type: to.Ptr(armcompute.ExtendedLocationTypesEdgeZone),
			},
			Properties: &armcompute.VirtualMachineProperties{
				StorageProfile: &armcompute.StorageProfile{
					ImageReference: &armcompute.ImageReference{
						Publisher: to.Ptr("<publisher>"),
						Offer:     to.Ptr("<offer>"),
						SKU:       to.Ptr("<sku>"),
						Version:   to.Ptr("<version>"),
					},
				},
				HardwareProfile: &armcompute.HardwareProfile{
					VMSize: to.Ptr(armcompute.VirtualMachineSizeTypesStandardD2SV3),
				},
				OSProfile: &armcompute.OSProfile{
					ComputerName:  to.Ptr("<computerName>"),
					AdminUsername: to.Ptr("<adminUsername>"),
					AdminPassword: to.Ptr("<adminPassword>"),
				},
				NetworkProfile: &armcompute.NetworkProfile{
					NetworkInterfaces: []*armcompute.NetworkInterfaceReference{
						{
							ID: interfacesClientCreateOrUpdateResponse.ID,
							Properties: &armcompute.NetworkInterfaceReferenceProperties{
								Primary: to.Ptr(true),
							},
						},
					},
				},
			},
		},
		nil,
	)
	if err != nil {
		log.Fatal("virtual machine creation failed", err)
	}
	_, err = virtualMachinesClientCreateOrUpdateResponsePoller.PollUntilDone(context.Background(), nil)
	if err != nil {
		log.Fatal("cannot create virtual machine:", err)
	}
}

清除資源

在本教學課程中,您已使用 Go SDK 在 Azure 公用 MEC 中建立 VM。 如果您在未來不需使用這些資源,請使用 Azure 入口網站來刪除您所建立的資源群組。

下一步

若要使用 Python SDK 在 Azure 公用 MEC 中部署虛擬機器,請參閱下列文章: