Closed9

CloudShellでDiagram as Codeを試してみる

Junichi HashimotoJunichi Hashimoto

Goをセットアップする。

# Go をインストール
sudo dnf install -y golang

# 作業用ディレクトリの作成と権限設定
mkdir -p ~/gopath
export GOPATH="$HOME/gopath"
export PATH="$PATH:/usr/lib/golang/bin:$GOPATH/bin"

# このセッションだけで使えるので、永続化したい場合は ~/.bashrc に追記
echo 'export GOPATH=$HOME/gopath' >> ~/.bashrc
echo 'export PATH=$PATH:/usr/lib/golang/bin:$GOPATH/bin' >> ~/.bashrc

# 動作確認
go version
# go version go1.24.5 linux/amd64
Junichi HashimotoJunichi Hashimoto

Diagram as Codeをインストールする。

go install github.com/awslabs/diagram-as-code/cmd/awsdac@latest
awsdac --version
# awsdac version dev
Junichi HashimotoJunichi Hashimoto

サンプルのYamlファイルをダウンロードする。

curl -o ./alb-ec2.yaml https://raw.githubusercontent.com/awslabs/diagram-as-code/main/examples/alb-ec2.yaml
Junichi HashimotoJunichi Hashimoto

Diagramの生成に失敗。fontファイルがインストールされていない模様。

awsdac ./alb-ec2.yaml 
panic: Specified fonts are not installed.

goroutine 1 [running]:
github.com/awslabs/diagram-as-code/internal/types.(*Resource).prepareFontFace(0xc00019f8c0, 0x1, 0x0?)
        /home/cloudshell-user/gopath/pkg/mod/github.com/awslabs/diagram-as-code@v0.21.12/internal/types/resource.go:254 +0x250
github.com/awslabs/diagram-as-code/internal/types.(*Resource).Scale(0xc00019f8c0, 0x0, 0xc00003d680)
        /home/cloudshell-user/gopath/pkg/mod/github.com/awslabs/diagram-as-code@v0.21.12/internal/types/resource.go:317 +0x245
github.com/awslabs/diagram-as-code/internal/types.(*Resource).Scale(0xc00019e7e0, 0x0, 0x0)
        /home/cloudshell-user/gopath/pkg/mod/github.com/awslabs/diagram-as-code@v0.21.12/internal/types/resource.go:407 +0xaf2
github.com/awslabs/diagram-as-code/internal/ctl.createDiagram(0xc00003db48, 0xc00011c4c0)
        /home/cloudshell-user/gopath/pkg/mod/github.com/awslabs/diagram-as-code@v0.21.12/internal/ctl/create.go:110 +0xa5
github.com/awslabs/diagram-as-code/internal/ctl.CreateDiagramFromDacFile({0x7ffd6bcc1360, 0xe}, 0xc00011c4c0, 0xc00003dbf8)
        /home/cloudshell-user/gopath/pkg/mod/github.com/awslabs/diagram-as-code@v0.21.12/internal/ctl/dacfile.go:134 +0x905
main.main.func2(0xc0001c4400?, {0xc00011c550?, 0x7?, 0x90aedf?})
        /home/cloudshell-user/gopath/pkg/mod/github.com/awslabs/diagram-as-code@v0.21.12/cmd/awsdac/main.go:74 +0xc5
github.com/spf13/cobra.(*Command).execute(0xc0001ca008, {0xc000120050, 0x1, 0x1})
        /home/cloudshell-user/gopath/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:987 +0xabb
github.com/spf13/cobra.(*Command).ExecuteC(0xc0001ca008)
        /home/cloudshell-user/gopath/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:1115 +0x44f
github.com/spf13/cobra.(*Command).Execute(...)
        /home/cloudshell-user/gopath/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:1039
main.main()
        /home/cloudshell-user/gopath/pkg/mod/github.com/awslabs/diagram-as-code@v0.21.12/cmd/awsdac/main.go:88 +0x3b0
Junichi HashimotoJunichi Hashimoto

fontファイルをインストールする。

# 更新してフォントパッケージをインストール
sudo dnf install -y liberation-fonts

# フォントキャッシュの更新
fc-cache -r
fc-list | grep -i liberation
/usr/share/fonts/liberation-mono/LiberationMono-Regular.ttf: Liberation Mono:style=Regular
/usr/share/fonts/liberation-sans/LiberationSans-Italic.ttf: Liberation Sans:style=Italic
/usr/share/fonts/liberation-mono/LiberationMono-Bold.ttf: Liberation Mono:style=Bold
/usr/share/fonts/liberation-sans/LiberationSans-Bold.ttf: Liberation Sans:style=Bold
/usr/share/fonts/liberation-sans/LiberationSans-Regular.ttf: Liberation Sans:style=Regular
/usr/share/fonts/liberation-serif/LiberationSerif-Regular.ttf: Liberation Serif:style=Regular
/usr/share/fonts/liberation-serif/LiberationSerif-Italic.ttf: Liberation Serif:style=Italic
/usr/share/fonts/liberation-mono/LiberationMono-BoldItalic.ttf: Liberation Mono:style=Bold Italic
/usr/share/fonts/liberation-serif/LiberationSerif-Bold.ttf: Liberation Serif:style=Bold
/usr/share/fonts/liberation-mono/LiberationMono-Italic.ttf: Liberation Mono:style=Italic
/usr/share/fonts/liberation-sans/LiberationSans-BoldItalic.ttf: Liberation Sans:style=Bold Italic
/usr/share/fonts/liberation-serif/LiberationSerif-BoldItalic.ttf: Liberation Serif:style=Bold Italic
Junichi HashimotoJunichi Hashimoto

Diagram生成に成功。

awsdac ./alb-ec2.yaml 
# [Completed] AWS infrastructure diagram generated: output.png
Junichi HashimotoJunichi Hashimoto

使用したYamlファイルのサンプル。

./alb-ec2.yaml
Diagram:
  DefinitionFiles:
    - Type: URL
      Url: "https://raw.githubusercontent.com/awslabs/diagram-as-code/main/definitions/definition-for-aws-icons-light.yaml"
    #- Type: LocalFile
    #  LocalFile: ./definitions/definition-for-aws-icons-light.yaml

  Resources:
    Canvas:
      Type: AWS::Diagram::Canvas
      Direction: vertical
      Children:
        - AWSCloud
        - User
    AWSCloud:
      Type: AWS::Diagram::Cloud
      Direction: vertical
      Preset: AWSCloudNoLogo
      Align: center
      Children:
        - VPC
    # VPC
    VPC:
      Type: AWS::EC2::VPC
      Direction: vertical
      Children:
        - VPCPublicStack
        - ALB
      BorderChildren:
        - Position: S
          Resource: IGW
    VPCPublicStack:
      Type: AWS::Diagram::HorizontalStack
      Children:
        - VPCPublicSubnet1
        - VPCPublicSubnet2
    VPCPublicSubnet1:
      Type: AWS::EC2::Subnet
      Preset: PublicSubnet
      Children:
        - VPCPublicSubnet1Instance
    VPCPublicSubnet1Instance:
      Type: AWS::EC2::Instance
    VPCPublicSubnet2:
      Type: AWS::EC2::Subnet
      Preset: PublicSubnet
      Children:
        - VPCPublicSubnet2Instance
    VPCPublicSubnet2Instance:
      Type: AWS::EC2::Instance
    ALB:
      Type: AWS::ElasticLoadBalancingV2::LoadBalancer
      Preset: Application Load Balancer
    IGW:
      Type: AWS::EC2::InternetGateway
      IconFill:
        Type: rect
    User:
      Type: AWS::Diagram::Resource
      Preset: User

  Links:
    - Source: ALB
      SourcePosition: NNW
      Target: VPCPublicSubnet1Instance
      TargetPosition: SSE
      TargetArrowHead:
        Type: Open
    - Source: ALB
      SourcePosition: NNE
      Target: VPCPublicSubnet2Instance
      TargetPosition: SSW
      TargetArrowHead:
        Type: Open
    - Source: IGW
      SourcePosition: N
      Target: ALB
      TargetPosition: S
      TargetArrowHead:
        Type: Open
    - Source: User
      SourcePosition: N
      Target: IGW
      TargetPosition: S
      TargetArrowHead:
        Type: Open
このスクラップは1ヶ月前にクローズされました