AdSense

網頁

2023/2/6

Golang 建立AWS Direct Connect Private Virtual Interface

Go以AWS提供的SDK aws-sdk-go-v2來建立Direct Connect的Private virtual interface。



事前要求

參考「AWS 建立IAM管理使用者及credentials」設定供應用程式存取AWS需要的credentials。

參考「Golang 建立AWS Direct Connect API client」建立Direct Connect API client。

參考「Golang 建立AWS Direct Connect Connection」建立Direct Connect Connection。

參考「Golang 建立AWS Direct Connect gateway」建立Direct Connect gateway。


建立Private Virtual Interface

呼叫directconnect.Client.CreatePrivateVirtualInterface傳入參數directconnect.CreatePrivateVirtualInterfaceInput來建立private virutal interface。

directconnect.CreatePrivateVirtualInterfaceInput.ConnectionId為要連結的Direct Connect Connection的ID。

directconnect.CreatePrivateVirtualInterfaceInput.NewPrivateVirtualInterface屬性為types.NewPrivateVirtualInterface,其DirectConnectGatewayId為要連結的Direct Connect gateway的ID。

main.go

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/directconnect"
    "github.com/aws/aws-sdk-go-v2/service/directconnect/types"
)

func main() {
    ctx := context.TODO()
    client := NewDirectConnectClient(ctx)

    connectionId := "dxcon-fg5kq63s"

    virtualInterfaceName := "demo-virtual-interface-001"
    authKey := "bgp-auth-key"
    directConnectGatewayId := "e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af"
    newPrivateVirtualInterface := types.NewPrivateVirtualInterface{
        Asn:                    4567,
        VirtualInterfaceName:   &virtualInterfaceName,
        Vlan:                   123,
        AddressFamily:          types.AddressFamilyIPv4,
        AuthKey:                &authKey,
        DirectConnectGatewayId: &directConnectGatewayId,
    }

    params := &directconnect.CreatePrivateVirtualInterfaceInput{
        ConnectionId:               &connectionId,
        NewPrivateVirtualInterface: &newPrivateVirtualInterface,
    }

    output, err := client.CreatePrivateVirtualInterface(ctx, params)
    if err != nil {
        panic(err)
    }

    fmt.Println(*output.VirtualInterfaceId)          // dxvif-ffwha4ij
    fmt.Println(*output.VirtualInterfaceName)        // demo-virtual-interface-001
    fmt.Println(output.VirtualInterfaceState)        // pending
    fmt.Println(*output.VirtualInterfaceType)        // private
    fmt.Println(*output.ConnectionId)                // dxcon-fg5kq63s
    fmt.Println(*output.DirectConnectGatewayId)      // e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af
    fmt.Println(*output.AmazonSideAsn)               // 64512
    fmt.Println(*output.BgpPeers[0].BgpPeerId)       // dxpeer-fgzqetz9
    fmt.Println(output.BgpPeers[0].BgpPeerState)     // pending
    fmt.Println(output.BgpPeers[0].BgpStatus)        // down
    fmt.Println(*output.BgpPeers[0].AmazonAddress)   // 169.254.96.1/29
    fmt.Println(*output.BgpPeers[0].CustomerAddress) // 169.254.96.6/29
}

func NewDirectConnectClient(ctx context.Context) *directconnect.Client {
    cfg, err := config.LoadDefaultConfig(
        ctx,
        config.WithRegion("ap-northeast-1"),
    )
    if err != nil {
        panic(err)
    }

    return directconnect.NewFromConfig(cfg) // Create an Amazon Direct Connect service client
}

測試

執行Go應用程式輸出以下結果。

dxvif-ffwha4ij
demo-virtual-interface-001
pending
private
dxcon-fg5kq63s
e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af
64512
dxpeer-fgzqetz9
pending
down
169.254.96.1/29
169.254.96.6/29

github


在AWS console檢視建立的virtual interface。




沒有留言:

AdSense