Closed5

Diagram as CodeでCloudFormationテンプレートからDiagramを生成してみる

Junichi HashimotoJunichi Hashimoto

サンプルのCloudFormationテンプレートをダウンロードする。

curl -o ./vpc-subnet-ec2-cfn.yaml https://raw.githubusercontent.com/awslabs/diagram-as-code/refs/heads/main/examples/vpc-subnet-ec2-cfn.yaml
Junichi HashimotoJunichi Hashimoto

テンプレートからDiagramを生成する。

awsdac ./vpc-subnet-ec2-cfn.yaml --cfn-template
# [Completed] AWS infrastructure diagram generated: output.png

Junichi HashimotoJunichi Hashimoto

テンプレート -> Yamlファイル -> Diagramを試してみる。中間ファイルであるYamlファイルを編集することにより、Diagramをカスタマイズできる。

まずYamlファイルを生成する。

awsdac ./vpc-subnet-ec2-cfn.yaml --cfn-template --dac-file
[Completed] dac (diagram-as-code) data written to output.yaml
[Completed] AWS infrastructure diagram generated: output.png
output.yaml
Diagram:
    DefinitionFiles:
        - Type: URL
          Url: https://raw.githubusercontent.com/awslabs/diagram-as-code/main/definitions/definition-for-aws-icons-light.yaml
          LocalFile: ""
          Embed:
            Definitions: {}
    Resources:
        AWSCloud:
            Type: AWS::Diagram::Cloud
            Icon: ""
            IconFill: null
            Direction: ""
            Preset: AWSCloudNoLogo
            Align: center
            HeaderAlign: ""
            FillColor: ""
            Title: ""
            TitleColor: ""
            Font: ""
            Children:
                - VPC
            BorderColor: ""
            BorderChildren: []
        Canvas:
            Type: AWS::Diagram::Canvas
            Icon: ""
            IconFill: null
            Direction: ""
            Preset: ""
            Align: ""
            HeaderAlign: ""
            FillColor: ""
            Title: ""
            TitleColor: ""
            Font: ""
            Children:
                - AWSCloud
            BorderColor: ""
            BorderChildren: []
        EC2Instance1:
            Type: AWS::EC2::Instance
            Icon: ""
            IconFill: null
            Direction: ""
            Preset: ""
            Align: ""
            HeaderAlign: ""
            FillColor: ""
            Title: ""
            TitleColor: ""
            Font: ""
            Children: []
            BorderColor: ""
            BorderChildren: []
        EC2Instance2:
            Type: AWS::EC2::Instance
            Icon: ""
            IconFill: null
            Direction: ""
            Preset: ""
            Align: ""
            HeaderAlign: ""
            FillColor: ""
            Title: ""
            TitleColor: ""
            Font: ""
            Children: []
            BorderColor: ""
            BorderChildren: []
        Subnet1:
            Type: AWS::EC2::Subnet
            Icon: ""
            IconFill: null
            Direction: ""
            Preset: ""
            Align: ""
            HeaderAlign: ""
            FillColor: ""
            Title: ""
            TitleColor: ""
            Font: ""
            Children:
                - EC2Instance1
            BorderColor: ""
            BorderChildren: []
        Subnet2:
            Type: AWS::EC2::Subnet
            Icon: ""
            IconFill: null
            Direction: ""
            Preset: ""
            Align: ""
            HeaderAlign: ""
            FillColor: ""
            Title: ""
            TitleColor: ""
            Font: ""
            Children:
                - EC2Instance2
            BorderColor: ""
            BorderChildren: []
        VPC:
            Type: AWS::EC2::VPC
            Icon: ""
            IconFill: null
            Direction: ""
            Preset: ""
            Align: ""
            HeaderAlign: ""
            FillColor: ""
            Title: ""
            TitleColor: ""
            Font: ""
            Children:
                - Subnet1
                - Subnet2
            BorderColor: ""
            BorderChildren: []
    Links: []
Junichi HashimotoJunichi Hashimoto

Yamlファイルをコピーしてリンクを追加する。

cp output.yaml output-customized.yaml 
diff output.yaml output-customized.yaml 
120c120,124
<     Links: []
---
>     Links: 
>         - Source: EC2Instance1
>           SourcePosition: E
>           Target: EC2Instance2
>           TargetPosition: W
Junichi HashimotoJunichi Hashimoto

リンクされたDiagramが生成できた。

awsdac ./output-customized.yaml -o output-src-customized-yaml.png
# [Completed] AWS infrastructure diagram generated: output-src-customized-yaml.png

このスクラップは2ヶ月前にクローズされました