今日は小ネタ。
久しぶりにAWS CloudFormationを書くことがありました。このとき、値を固定値ではなく可変値にするためにParameters
を使うことがよくあります。例えば、以下のようにVPCを作成するCloudFormationにてCIDRの値をパラメータ化すると使い勝手は良さそうです。
AWSTemplateFormatVersion: 2010-09-09 Description: Create VPC and subnets Parameters: ProjectName: Type: String VPCCIDER: Type: String Default: 10.0.0.0/16 PublicSubnet1CIDER: Type: String Default: 10.0.0.0/24 PublicSubnet2CIDER: Type: String Default: 10.0.1.0/24 PrivateSubnet1CIDER: Type: String Default: 10.0.2.0/24 PrivateSubnet2CIDER: Type: String Default: 10.0.3.0/24 Resources: # VPCの作成 VPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VPCCIDER InstanceTenancy: default EnableDnsSupport: true EnableDnsHostnames: true Tags: - Key: Name Value: !Sub ${ProjectName}-vpc # 一つ目のPublic Subnetの作成 PublicSubnet1: Type: AWS::EC2::Subnet Properties: CidrBlock: !Ref PublicSubnet1CIDER VpcId: !Ref VPC AvailabilityZone: !Select [ 0, !GetAZs ] MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Sub ${ProjectName}-public-subnet1 # 二つ目のPublic Subnetの作成 PublicSubnet2: Type: AWS::EC2::Subnet Properties: CidrBlock: !Ref PublicSubnet2CIDER VpcId: !Ref VPC AvailabilityZone: !Select [ 1, !GetAZs ] MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Sub ${ProjectName}-public-subnet1 # 一つ目のPrivate Subnet1を作成 PrivateSubnet1: Type: AWS::EC2::Subnet Properties: CidrBlock: !Ref PrivateSubnet1CIDER VpcId: !Ref VPC AvailabilityZone: !Select [ 0, !GetAZs ] Tags: - Key: Name Value: !Sub ${ProjectName}-private-subnet1 # 二つ目のPrivate Subnet2を作成 PrivateSubnet2: Type: AWS::EC2::Subnet Properties: CidrBlock: !Ref PrivateSubnet2CIDER VpcId: !Ref VPC AvailabilityZone: !Select [ 1, !GetAZs ] Tags: - Key: Name Value: !Sub ${ProjectName}-private-subnet2 # Internet Gatewayを作成 VPCIGW: Type: AWS::EC2::InternetGateway Properties: Tags: - Key: Name Value: !Sub ${ProjectName}-igw # Internet Gatewayのアタッチ VPCIGWAttach: Type: AWS::EC2::VPCGatewayAttachment Properties: InternetGatewayId: !Ref VPCIGW VpcId: !Ref VPC # Route Tableの作成 PublicRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Sub ${ProjectName}-public-route-table # Routing定義 PublicRoute: Type: AWS::EC2::Route DependsOn: VPCIGW Properties: RouteTableId: !Ref PublicRouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref VPCIGW # Route Tableのアサイン PublicSubnet1Association: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet1 RouteTableId: !Ref PublicRouteTable PublicSubnet2Association: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet2 RouteTableId: !Ref PublicRouteTable
これをAWS Consoleで読み込ませ、スタックの作成画面に移ると以下のような画面となります。
パラメータのアルファベット順に表示されるので、あまり使い勝手は良くないです。これを制御するのが、AWS::CloudFormation::Interface
。
上記のVPC作成のYAMLファイルにおいては、以下のように記述します。Metadata
を追記する形となります。
AWSTemplateFormatVersion: 2010-09-09 Description: Create VPC and subnets Metadata: "AWS::CloudFormation::Interface": ParameterGroups: - Label: default: "YourProjectName" Parameters: - ProjectName - Label: default: "Network Configuration" Parameters: - VPCCIDER - PublicSubnet1CIDER - PublicSubnet2CIDER - PrivateSubnet1CIDER - PrivateSubnet2CIDER ParameterLabels: VPCCIDER: default: VPC CIDER PublicSubnet1CIDER: default: Public Subnet1 CIDER PublicSubnet2CIDER: default: Public Subnet2 CIDER PrivateSubnet1CIDER: default: Private Subnet1 CIDER PrivateSubnet2CIDER: default: Private Subnet2 CIDER (以下略)
これをAWS Consoleで読み込ませると、以下のような画面となります。
それぞれの設定項目がグルーピング化され、記述する順番も思考と同じなので分かりやすい入力欄となります。