[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

CloudFormationでVPC環境を作ってみよう(テンプレートファイルの書き方編)

皆さんこんにちは
システムソリューション部SREチーム所属の岡崎です。

以前からAWSでの構築に携わることが多く、その中で触れる機会が多かった「CloudFormation」の簡単な使い方を紹介したいと思います。
今回はCloudFormationの実行に必要なテンプレートファイルの記入の仕方を記入します。

CloudFormationとは??

AWSが提供しているAWSクラウド環境内のリソースをyml, json形式のファイルで管理・構築できるサービスとなります。
AWSリソースの起動、停止、削除をそれぞれのコンソールからではなく単一のコンソールからスタックという単位で管理することができます。
CloudFormationで構築されたEC2インスタンスなどに費用は発生しますが、CloudFormation自体に追加料金は発生しません。

VPC周りを構築する

では早速以下のymlファイルを元にCloudFormationを使って、VPCを作ってみましょう

vpc.yml

---
AWSTemplateFormatVersion: '2010-09-09'
 
# パラメータセッティング
Parameters:
  # それぞれの識別子を入力
  ProjectCode:
    Type: String
    Default: test
    Description: Project Code
 # VPCのCIDRを入力
  VPCCidr:
    Type: String
    Default: 10.31.0.0/16
    Description: VPCCidr
  # SubnetのCIDRを入力
  PublicSubnetCidr:
    Type: String
    Default: 10.31.0.0/24
    Description: PublicSubnetCidr

Resources:
# VPC周り
  #VPCの設定
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VPCCidr
      Tags:
        - Key: Name
          Value: !Join [ "-", [ !Ref ProjectCode, vpc ] ]

  # インターネットゲートウェイの設定
  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Join [ "-", [ !Ref ProjectCode, igw ] ]
  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref InternetGateway
  PublicRouteTableIGW:
    Type: AWS::EC2::RouteTable
    DependsOn: AttachGateway
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Join [ "-", [ !Ref ProjectCode, public-route-table-igw ] ]
  PublicRouteIGW:
    Type: AWS::EC2::Route
    DependsOn: AttachGateway
    Properties:
      RouteTableId: !Ref PublicRouteTableIGW
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  # サブネットの設定
  PublicSubnet:
    Type: AWS::EC2::Subnet
    DependsOn: AttachGateway
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: ap-northeast-1a
      CidrBlock: !Ref PublicSubnetCidr
      Tags:
        - Key: Name
          Value: !Join [ "-", [ !Ref ProjectCode, public-subnet ] ]

項目ごとの解説

では上記のymlファイルの解説をいたします。

---
AWSTemplateFormatVersion: '2010-09-09'

CloudFormationテンプレートのフォーマットバージョンを記入しています。
現在最新のテンプレートのフォーマットバージョンは2010-09-09であり、
こちらが2019年3月21日時点では唯一有効な値となっております。

# パラメータセッティング
Parameters:
  # それぞれの識別子を入力
  ProjectCode:
    Type: String
    Default: test
    Description: Project Code
 # VPCのCIDRを入力
  VPCCidr:
    Type: String
    Default: 10.31.0.0/16
    Description: VPCCider
  # SubnetのCIDRを入力
  PublicSubnetCidr:
    Type: String
    Default: 10.31.0.0/24
    Description: PublicSubnetCider

それぞれのリソースを作成したいとき、毎回決まった値を作成する場合はいいのですが、
運用しやすいように値を変えたい場合があると思います。
CloudFormationではそれら自由に値を変更したいときに「Parameters」というセクションで設定できます。
サンプルファイルではリソースごとに共通する識別子をProjectCodeとして設定し、
初期値をtestとしていますが、スタックとに自由に入力できるようにしております。
VPCのCidrやSubnetのCidrも初期値として設定していますが、同じようにスタック単位で値を設定できるようにしております。

Resources:
# VPC周り
  #VPCの設定
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VPCCidr
      Tags:
        - Key: Name
          Value: !Join [ "-", [ !Ref ProjectCode, vpc ] ]

AWSのリソースを管理する部分が上記の「Resources」というセクションになります。
VPCの部分は論理IDと言われ、任意のものを設定可能で、他のリソースから呼び出したりも可能です。
しかし、それぞれが一意でないと実行時にエラーが発生してしまいます。
Type:~の部分が実際のAWSリソースを定義する部分になります。
Properties配下がVPC自体の設定の部分になり、さきほどのVPCCidrのパラメーターをRefという関数で呼び出し事前に定義したCIDRを呼び出しCidrBlockに挿入しております。
今回VPCで設定するのはCidrBlockのみになりますが、詳細な設定はAWS公式にありますのでまた確認いただければと思います。
AWS公式「AWS::EC2::VPC」について

また、Tags部分にてわかりやすいようにNameタグを付けております。
こちらもValueの部分を識別子-vpcというタグが付くようにJoinという関数を利用して結合しております。

  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref InternetGateway
  PublicRouteTableIGW:
    Type: AWS::EC2::RouteTable
    DependsOn: AttachGateway
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Join [ "-", [ !Ref ProjectCode, public-route-table-igw ] ]
  PublicRouteIGW:
    Type: AWS::EC2::Route
    DependsOn: AttachGateway
    Properties:
      RouteTableId: !Ref PublicRouteTableIGW
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  # サブネットの設定
  PublicSubnet:
    Type: AWS::EC2::Subnet
    DependsOn: AttachGateway
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: ap-northeast-1a
      CidrBlock: !Ref PublicSubnetCidr
      Tags:
        - Key: Name
          Value: !Join [ "-", [ !Ref ProjectCode, public-subnet ] ]

その他のリソースも同じようにType:~でそれぞれのリソースを宣言しPropertiesにて細かい設定を行い、Nameタグを紐づけております。

まとめ

今回はCloudFormationでVPCを構築するために必要なテンプレートファイルの書き方を記入しましたが、
実際の実行部分は長くなってしまうので次回にまた記入したいと思います。
日々手動でAWSリソースを立ち上げることがあると思いますが、こういったツールを使うことで
少しでも工数を減らすことができると思いますので、AWSを利用することが多い方は
利用自体は無料なのでぜひとも利用してほしいとおもいます。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
0
読み込み中...
0 票, 平均: 0.00 / 10
5,292
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Junichiro Okazaki

Extensive experience in relocating and operating smartphone games.

He handles multi-cloud operations, server construction and relocation on a daily basis. As the number of cases has increased, I am considering how to improve the efficiency of my work. We often consider methods for relocating servers based on the merits of each cloud.

While we were relocating between clouds and from physical to cloud, we achieved two consecutive victories in a competition held by the Japan MSP Association.