0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

初めてAWS Cloudformationをさわる人へ_邪道編

Last updated at Posted at 2025-01-27

目次

1.はじめに
2.AWS Cloudformationを選んだ理由
3.邪道なAWS Cloudformationテンプレート作成
4.おわりに

はじめに

最近、IaC(Infrastructure as Code)という言葉をよく聞くようになりました。
IaCとはCPUやメモリ、ストレージ、サービス、ネットワークなどのインフラ環境をコードを用いて自動的に構築することです。
IaCのメリットとして下記があります。
・コードで構築するため設定ミスの防止が可能
・コード化されたインフラを再利用するため構築コスト削減

IaCツールの一つであるAWS Cloudformationについて勉強し始めたので、初心者向けにメモを残します。
邪道編(生成AIに依頼)と正道編の二部構成です。

AWS Cloudformationを選んだ理由

IaCができる有名なツールといてTerraformとAWS Cloudformationがあります。
メインで利用するクラウドがAWSだったことから、TerraformではなくAWS Cloudformationをまずは学ぶことにしました。
マルチクラウドで環境を構築する場合はTerraformが良いかと思います。

TerraformとAWS Cloudformationの比較表

Terraform AWS Cloudformation
UI CUI ( terraform コマンド) CUI (AWSCLI)、GUI (AWSコンソール)
リソースの管理方法 ファイルで管理 ファイルで管理、AWSのサービスで自動管理
マルチクラウド性 複数プラットフォームに対応 AWS以外のリソース作成不可
文法 HCL(Hashicorp Configuration Language)というDSL jsonまたはyaml
組込み関数 多い 少ない

TerraformとAWS CloudFormationの違いについてはこちらの記事を参考にしています。

AWS Cloudformationテンプレート作成方法(邪道)

まず邪道なテンプレートの作成方法です。

作成方法:AIにお願い

チャットボットを立ち上げます。
「CloudformationでVPC,パブリックサブネット、InterNetGatewayを構築するコードを教えてください。」と送ります。
以上です。
IMG_3008.jpg

下記が実際に出力したコードになります。

VPC,PublicSubnet,InternetGateway
AWSTemplateFormatVersion: '2010-09-09' 
Description: 'VPC, Public Subnet, and Internet Gateway setup'

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: '10.0.0.0/16'
      EnableDnsHostnames: true
      EnableDnsSupport: true
      Tags:
        - Key: Name
          Value: MyVPC

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: MyInternetGateway

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

  PublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: '10.0.1.0/24'
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: PublicSubnet

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

  PublicRoute:
    Type: AWS::EC2::Route
    DependsOn: AttachGateway
    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

Outputs:
  VPCId:
    Description: The ID of the VPC
    Value: !Ref VPC
    Export:
      Name: !Sub '${AWS::StackName}-VPCId'

  PublicSubnetId:
    Description: The ID of the Public Subnet
    Value: !Ref PublicSubnet
    Export:
      Name: !Sub '${AWS::StackName}-PublicSubnetId'

  InternetGatewayId:
    Description: The ID of the Internet Gateway
    Value: !Ref InternetGateway
    Export:
      Name: !Sub '${AWS::StackName}-InternetGatewayId'

AWS Cloudformationでスタックが正常に作成できました。
image.png

今回はX(旧Twitter)のGROXを使いました。
無料で一般的に使いやすいく身近なAIとしては優秀です。

おわりに

まっとうなテンプレート作成方法についてセクションや関数説明とか話そうとするとテキスト量が多くなってしまうので2部構成にします。
次回、正道編です。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?