📑
AWS CloudFormationでVPCやEC2などを作成する方法
AWS EC2でReactアプリをデプロイする方法で作成したリソースを、AWS CloudFormationで作成します。
用語
- テンプレート
- スタック
手順
テンプレートの作成
下記の内容のサンプルファイルを作成します。
AWSTemplateFormatVersion: "2010-09-09" ## 現在のCloudFormationの最新バージョン
Resources:
VPC:
Type: "AWS::EC2::VPC"
Properties:
Tags:
- Value: "learn-cfn-vpc" ## 任意の名前を入力
Key: "Name"
CidrBlock: "10.0.0.0/16"
EnableDnsHostnames: true
EnableDnsSupport: true
InternetGateway:
Type: "AWS::EC2::InternetGateway"
Properties:
Tags:
- Value: "learn-cfn-internet-gateway" ## 任意の名前を入力
Key: "Name"
AttachGateway:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
RouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
Tags:
- Value: "learn-cfn-route-table" ## 任意の名前を入力
Key: "Name"
VpcId: !Ref VPC
Route:
Type: "AWS::EC2::Route"
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: "0.0.0.0/0"
GatewayId: !Ref InternetGateway
Subnet1:
Type: "AWS::EC2::Subnet"
Properties:
Tags:
- Value: "learn-cfn-subnet-1" ## 任意の名前を入力
Key: "Name"
AvailabilityZone: "ap-northeast-1c"
CidrBlock: "10.0.0.0/20"
VpcId: !Ref VPC
Subnet1RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref Subnet1
RouteTableId: !Ref RouteTable
Subnet2:
Type: "AWS::EC2::Subnet"
Properties:
Tags:
- Value: "learn-cfn-subnet-2" ## 任意の名前を入力
Key: "Name"
AvailabilityZone: "ap-northeast-1a"
CidrBlock: "10.0.16.0/20"
VpcId: !Ref VPC
Subnet2RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref Subnet2
RouteTableId: !Ref RouteTable
SecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: "learn-cfn-security-group" ## 任意の名前を入力
GroupDescription: "security group for learn-cfn stack" ## 任意の説明を入力(必須)
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 5173
ToPort: 5173
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 5432
ToPort: 5432
CidrIp: 10.0.0.0/16
VpcId: !Ref VPC
EC2:
Type: "AWS::EC2::Instance"
Properties:
Tags:
- Value: "learn-cfn-ec2" ## 任意の名前を入力
Key: "Name"
IamInstanceProfile: "AmazonSSMRoleForInstancesQuickSetup"
ImageId: "ami-05206bf8aecfc7ae6"
InstanceType: "t2.micro"
KeyName: "RSA"
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeviceIndex: 0
SubnetId: !Ref Subnet1
GroupSet:
- !Ref SecurityGroup
DB:
Type: "AWS::RDS::DBInstance"
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
Tags:
- Value: "learn-cfn-db" ## 任意の名前を入力
Key: "Name"
AllocatedStorage: 20
BackupRetentionPeriod: 0
DBInstanceClass: "db.t4g.micro"
DBName: "postgres"
DBSubnetGroupName: !Ref DBSubnetGroup
VPCSecurityGroups:
- !GetAtt SecurityGroup.GroupId
DeletionProtection: false
Engine: "postgres"
EngineVersion: "17.2"
MasterUsername: "postgres"
MasterUserPassword: password
MultiAZ: false
StorageType: "gp2"
DBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: "My Subnet Group"
SubnetIds:
- !Ref Subnet1
- !Ref Subnet2
CloudFormationの実行
- AWS CloudFormationページに移動する
- 「スタックの作成」をクリックする
- 「テンプレートファイルのアップロード」を選択し、ファイルをアップロードして「次へ」をクリックする
- 任意のスタック名を入力して「次へ」をクリックする
- 任意のスタックオプションを設定し、「次へ」をクリックする(今回は何も設定しない)
- 内容を確認し、「送信」をクリックする
- 一定時間経過後、テンプレートに問題がなければスタックが作成される
Discussion