LoginSignup
1
0

More than 1 year has passed since last update.

ChatGPTに「パブリックサブネットとプライベートサブネットを含むVPCを作成するAWS Cloudformationのテンプレート教えて」と聞いてみた

Last updated at Posted at 2023-02-17

標題通りです、結果出てきたコードが下記、動くかどうか試してはないけど十分に実用に耐えるんではないかと思う。


質問(1)パブリックサブネットとプライベートサブネットを含むVPCを作成する AWS Cloudformationのテンプレート教えて

出力されたコード
qiita.rb
{
  "Resources": {
    "MyVPC": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": "10.0.0.0/16",
        "Tags": [
          {
            "Key": "Name",
            "Value": "MyVPC"
          }
        ]
      }
    },
    "PublicSubnet": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "VpcId": {
          "Ref": "MyVPC"
        },
        "CidrBlock": "10.0.0.0/24",
        "AvailabilityZone": "us-west-2a",
        "Tags": [
          {
            "Key": "Name",
            "Value": "PublicSubnet"
          }
        ]
      }
    },
    "PrivateSubnet": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "VpcId": {
          "Ref": "MyVPC"
        },
        "CidrBlock": "10.0.1.0/24",
        "AvailabilityZone": "us-west-2b",
        "Tags": [
          {
            "Key": "Name",
            "Value": "PrivateSubnet"
          }
        ]
      }
    },
    "InternetGateway": {
      "Type": "AWS::EC2::InternetGateway",
      "Properties": {
        "Tags": [
          {
            "Key": "Name",
            "Value": "MyInternetGateway"
          }
        ]
      }
    },
    "GatewayAttachment": {
      "Type": "AWS::EC2::VPCGatewayAttachment",
      "Properties": {
        "VpcId": {
          "Ref": "MyVPC"
        },
        "InternetGatewayId": {
          "Ref": "InternetGateway"
        }
      }
    },
    "PublicRouteTable": {
      "Type": "AWS::EC2::RouteTable",
      "Properties": {
        "VpcId": {
          "Ref": "MyVPC"
        },
        "Tags": [
          {
            "Key": "Name",
            "Value": "PublicRouteTable"
          }
        ]
      }
    },
    "PublicRoute": {
      "Type": "AWS::EC2::Route",
      "DependsOn": "GatewayAttachment",
      "Properties": {
        "RouteTableId": {
          "Ref": "PublicRouteTable"
        },
        "DestinationCidrBlock": "0.0.0.0/0",
        "GatewayId": {
          "Ref": "InternetGateway"
        }
      }
    },
    "PublicSubnetRouteTableAssociation": {
      "Type": "AWS::EC2::SubnetRouteTableAssociation",
      "Properties": {
        "SubnetId": {
          "Ref": "PublicSubnet"
        },
        "RouteTableId": {
          "Ref": "PublicRouteTable"
        }
      }
    },
    "PrivateRouteTable": {
      "Type": "AWS::EC2::RouteTable",
      "Properties": {
        "VpcId": {
          "Ref": "MyVPC"
        },
        "Tags": [
          {
            "Key": "Name",
            "Value": "PrivateRouteTable"
          }
        ]
      }
    },

質問(2)サブネットの名前は実行時に入力できるようにしたい

ChatGPTの回答

CloudFormationテンプレートで、サブネット名を入力パラメータとして取得するには、以下のように Parameters セクションを定義します。

qiita.rb
yaml
Copy code
Parameters:
  PublicSubnetName:
    Type: String
    Default: PublicSubnet
    Description: Name of the public subnet
  PrivateSubnetName:
    Type: String
    Default: PrivateSubnet
    Description: Name of the private subnet

そして、それらの名前を Resources セクション内で使用するリソースの定義の中で、 SubnetName プロパティとして参照できます。例えば、以下のように AWS::EC2::Subnet リソースを定義できます。

qiita.rb

yaml
Copy code
Resources:
  PublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: !Select [0, !GetAZs ""]
      Tags:
        - Key: Name
          Value: !Ref PublicSubnetName
  PrivateSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.2.0/24
      AvailabilityZone: !Select [1, !GetAZs ""]
      Tags:
        - Key: Name
          Value: !Ref PrivateSubnetName

この例では、2つのパラメータ PublicSubnetName と PrivateSubnetName を定義し、それぞれデフォルト値を設定しています。そして、AWS::EC2::Subnet リソースの Tags プロパティで、Name タグの値にそれぞれの名前を指定しています。

質問(3)最初のテンプレートに組み込んでみて

ChatGPTの回答
qiita.rb
AWSTemplateFormatVersion: '2010-09-09'
Description: 'VPC with public and private subnets'

Parameters:
  VpcCidr:
    Type: String
    Default: '10.0.0.0/16'
    Description: 'CIDR block for the VPC'

  PublicSubnetCidr:
    Type: String
    Default: '10.0.1.0/24'
    Description: 'CIDR block for the public subnet'

  PrivateSubnetCidr:
    Type: String
    Default: '10.0.2.0/24'
    Description: 'CIDR block for the private subnet'

  PublicSubnetName:
    Type: String
    Default: PublicSubnet
    Description: Name of the public subnet

  PrivateSubnetName:
    Type: String
    Default: PrivateSubnet
    Description: Name of the private subnet

Resources:
  VPC:
    Type: 'AWS::EC2::VPC'
    Properties:
      CidrBlock: !Ref VpcCidr
      Tags:
        - Key: Name
          Value: VPC

  InternetGateway:
    Type: 'AWS::EC2::InternetGateway'

  GatewayAttachment:
    Type: 'AWS::EC2::VPCGatewayAttachment'
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref InternetGateway

  PublicSubnet:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref VPC
      CidrBlock: !Ref PublicSubnetCidr
      AvailabilityZone: !Select [0, !GetAZs '']
      Tags:
        - Key: Name
          Value: !Ref PublicSubnetName

  PrivateSubnet:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref VPC
      CidrBlock: !Ref PrivateSubnetCidr
      AvailabilityZone: !Select [1, !GetAZs '']
      Tags:
        - Key: Name
          Value: !Ref PrivateSubnetName

  PublicRouteTable:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: PublicRouteTable

  PrivateRouteTable:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: PrivateRouteTable

  PublicRoute:
    Type: 'AWS::EC2::Route'
    DependsOn: GatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: '0.0.0.0/0'
      GatewayId: !Ref InternetGateway

  PrivateRoute:
    Type: 'AWS::EC2::Route'
    Properties:
      RouteTableId: !Ref PrivateRouteTable
      DestinationCidrBlock: '0.0.0



会話続けるとOutputsでネスト化とかMappingでリージョン毎にAMI変えるとかもっとブラッシュアップしたテンプレートもできると思う。
これ見るに初動でハンドコーディングはもうしないだろうな。
今の時点ではまだChatGPTへ適切な質問ができる質問力が大事と言えるけど、それも数ヶ月でいらなくなって、人間にはやりたいことの意思だけあればいいって事にすぐになりそう。今生きてる私たちは産業革命以降最大の激変のまっただ中にいると思う。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0