サンプルリポジトリとネットワーク環境の準備
このページで扱う手順で、Git がインストールされていることを確認します。
1. サンプルリポジトリのクローン
以下のコマンドで、Git がインストールされていることを確認します。
git --version
以下のように Git のバージョンが表示されれば、インストールは確認できています。
git version 2.x.x
バージョンが表示されない場合は、Git/GitHubのセットアップ を先に実施してください。
インストールが確認できたら、このセクションで使用するノートアプリケーションのソースコードをGitHubのサンプルリポジトリからクローンします。コマンドプロンプト(Windows)またはターミナル(Mac)を開き、任意の場所で以下のコマンドを実行します。
git clone https://github.com/gevanni-academy/sample-note-api.git
クローンが完了したら、Visual Studio Codeを起動し、「ファイル」→「フォルダーを開く」から、クローンしたsample-note-apiフォルダを開いてください。以降の操作は、Visual Studio Codeのターミナルから行います。
2. CloudFormationによる環境構築
このセクションでは、ECS/Fargateの構築に集中するため、前提となるネットワークやデータベースのリソースはCloudFormationで一括作成します。
| 📝 CloudFormationとは |
|---|
| CloudFormationは、AWSのリソースをテンプレート(YAML/JSON)で定義し、自動的に作成・管理できるサービスです。手動でリソースを一つずつ作成する代わりに、テンプレートをアップロードするだけで必要なリソースをまとめて構築できます。 IaC( Infrastructure as Code)ツールとしてはTerraformも広く使われていますが、Terraformは事前にインストールが必要です。一方、CloudFormationはAWSマネジメントコンソールからそのまま実行できるため、今回のハンズオンではCloudFormationを使用します。なお、このセクションではCloudFormationテンプレートの内容を理解する必要はありません。テンプレートをそのままアップロードして環境を構築してください。 |
CloudFormationで作成されるリソースは以下のとおりです。
| リソース | 名前 | 説明 |
|---|---|---|
| VPC | note-app-vpc | 10.0.0.0/16のアドレス範囲を持つ仮想ネットワーク |
| パブリックサブネット | note-app-public-subnet-1, 2 | ALBを配置(2AZ構成) |
| プライベートサブネット | note-app-private-subnet-1, 2 | Fargateタスク・RDSを配置(2AZ構成) |
| インターネットゲートウェイ | note-app-igw | パブリックサブネットからのインターネットアクセス用 |
| NATゲートウェイ | note-app-natgw | プライベートサブネットからECRへのイメージ取得用 |
| ルートテーブル | note-app-public-rtb | パブリックサブネット用のルーティング |
| ルートテーブル | note-app-private-rtb | プライベートサブネット用のルーティング(NATゲートウェイ経由) |
| セキュリティグループ | note-app-alb-sg | ALB用(HTTP:80を許可) |
| セキュリティグループ | note-app-api-sg | APIタスク用(ALBからの8000を許可) |
| セキュリティグループ | note-app-front-sg | フロントタスク用(ALBからの80を許可) |
| セキュリティグループ | note-app-rds-sg | RDS用(APIタスクからの3306を許可) |
| DBサブネットグループ | note-app-db-subnet-group | RDS用のサブネットグループ |
| RDSインスタンス | note-app-db | MySQLデータベース(notedb) |
| 💡 ポイント |
|---|
| Fargateタスクをプライベートサブネットに配置するのは、インターネットからタスクに直接アクセスさせないためのセキュリティ上のベストプラクティスです。ユーザからのアクセスはALB(パブリックサブネット)が受け付け、ALBからプライベートサブネット内のタスクに転送します。 ただし、プライベートサブネット内のタスクはそのままではインターネットに接続できないため、ECRからのDockerイメージの取得が失敗します。これを解決するためにNATゲートウェイを配置し、プライベートサブネットからインターネットへのアウトバウンド通信(外向きの通信)を可能にしています。 このセクションではコストを抑えるためゾーナルNATゲートウェイを1つのAZにのみ配置しています。業務ではリージョン全体で共有できるリージョナルNATゲートウェイを使うと構成がシンプルになりますが、リージョン内のAZ数に応じた時間料金がかかるため、コスト要件に応じて使い分けてください。 |
AWSマネジメントコンソールの検索ボックスで「CloudFormation」と入力し、CloudFormationのダッシュボードを開きます。

「スタックの作成」>「新しいリソースを使用(標準)」をクリックします。

スタックの作成の設定を行っていきます。下記表に従い、設定を行ってください。設定後、次へをクリックしてください。
| 設定項目 | 値 | 設定の基準 |
|---|---|---|
| テンプレートの準備 | 既存のテンプレートを選択 | 事前に作成したテンプレートファイルを使用するため |
| テンプレートソース | テンプレートファイルのアップロード | ローカルのテンプレートファイルを直接アップロードするため |
| テンプレートファイルのアップロード | 以下のYAMLテンプレートを保存したファイル | ハンズオン環境を構築するテンプレートを指定するため |
アップロードするのは下記ファイルです。こちらをtemplate.ymlのような任意の名前でローカル環境に保存し、それをアップロードしてください。
AWSTemplateFormatVersion: '2010-09-09'
Description: ECS/Fargate Hands-on Environment (VPC, Security Groups, RDS)
Parameters:
DBMasterPassword:
Type: String
NoEcho: true
Description: Password for RDS master user (admin)
MinLength: 8
Resources:
# ==================== VPC ====================
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: note-app-vpc
# ==================== Subnets ====================
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.1.0/24
AvailabilityZone: ap-northeast-1a
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: note-app-public-subnet-1
PublicSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.2.0/24
AvailabilityZone: ap-northeast-1c
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: note-app-public-subnet-2
PrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.3.0/24
AvailabilityZone: ap-northeast-1a
Tags:
- Key: Name
Value: note-app-private-subnet-1
PrivateSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.4.0/24
AvailabilityZone: ap-northeast-1c
Tags:
- Key: Name
Value: note-app-private-subnet-2
# ==================== Internet Gateway ====================
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: note-app-igw
VPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
# ==================== Route Table ====================
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: note-app-public-rtb
PublicRoute:
Type: AWS::EC2::Route
DependsOn: VPCGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet1
RouteTableId: !Ref PublicRouteTable
PublicSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet2
RouteTableId: !Ref PublicRouteTable
# ==================== NAT Gateway ====================
NatGatewayEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
Tags:
- Key: Name
Value: note-app-natgw-eip
NatGateway:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NatGatewayEIP.AllocationId
SubnetId: !Ref PublicSubnet1
Tags:
- Key: Name
Value: note-app-natgw
# ==================== Private Route Table ====================
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: note-app-private-rtb
PrivateRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway
PrivateSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet1
RouteTableId: !Ref PrivateRouteTable
PrivateSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet2
RouteTableId: !Ref PrivateRouteTable
# ==================== Security Groups ====================
ALBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for ALB
GroupName: note-app-alb-sg
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: note-app-alb-sg
APISecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for API tasks
GroupName: note-app-api-sg
VpcId: !Ref VPC
Tags:
- Key: Name
Value: note-app-api-sg
APIFromALBIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref APISecurityGroup
IpProtocol: tcp
FromPort: 8000
ToPort: 8000
SourceSecurityGroupId: !Ref ALBSecurityGroup
FrontSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for Front tasks
GroupName: note-app-front-sg
VpcId: !Ref VPC
Tags:
- Key: Name
Value: note-app-front-sg
FrontFromALBIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref FrontSecurityGroup
IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId: !Ref ALBSecurityGroup
RDSSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for RDS
GroupName: note-app-rds-sg
VpcId: !Ref VPC
Tags:
- Key: Name
Value: note-app-rds-sg
RDSFromAPIIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref RDSSecurityGroup
IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref APISecurityGroup
# ==================== DB Subnet Group ====================
DBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupName: note-app-db-subnet-group
DBSubnetGroupDescription: Subnet group for note app RDS
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
# ==================== RDS ====================
RDSInstance:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceIdentifier: note-app-db
Engine: mysql
EngineVersion: '8.0'
DBInstanceClass: db.t3.micro
AllocatedStorage: 20
StorageType: gp2
MasterUsername: admin
MasterUserPassword: !Ref DBMasterPassword
DBName: notedb
DBSubnetGroupName: !Ref DBSubnetGroup
VPCSecurityGroups:
- !Ref RDSSecurityGroup
PubliclyAccessible: false
AvailabilityZone: ap-northeast-1a
Outputs:
VPCId:
Description: VPC ID
Value: !Ref VPC
PublicSubnet1Id:
Description: Public Subnet 1 ID
Value: !Ref PublicSubnet1
PublicSubnet2Id:
Description: Public Subnet 2 ID
Value: !Ref PublicSubnet2
PrivateSubnet1Id:
Description: Private Subnet 1 ID
Value: !Ref PrivateSubnet1
PrivateSubnet2Id:
Description: Private Subnet 2 ID
Value: !Ref PrivateSubnet2
RDSEndpoint:
Description: RDS Endpoint Address
Value: !GetAtt RDSInstance.Endpoint.Address
ALBSecurityGroupId:
Description: ALB Security Group ID
Value: !Ref ALBSecurityGroup
APISecurityGroupId:
Description: API Security Group ID
Value: !Ref APISecurityGroup
FrontSecurityGroupId:
Description: Front Security Group ID
Value: !Ref FrontSecurityGroup

「次へ」をクリックし、スタックの詳細を設定します。
| 設定項目 | 値 | 設定の基準 |
|---|---|---|
| スタック名 | ecs-handson | ハンズオン用のスタックであることを識別するため |
| DBMasterPassword | 任意のパスワード(8文字以上) | RDSへの接続パスワード。後ほど使用するため控えておくこと |

「次へ」をクリックします。
スタックオプションはデフォルトのまま「次へ」をクリックします。

確認画面で内容を確認し、「送信」をクリックします。

CloudFormationのスタックが作成されます。ステータスがCREATE_IN_PROGRESSとなり、作成中であることがわかります。スタックの作成には10〜15分ほどかかります。

ステータスがCREATE_COMPLETEになれば、リソースの作成が完了です。

3. CloudFormation出力値の確認
スタックの詳細画面で「出力」タブをクリックしてください。この後のハンズオンで使用する値が表示されます。以下の表を参考に、各値を控えておいてください。

| キー | 意味 | ハンズオンでの用途 |
|---|---|---|
| VPCId | 作成されたVPCのID | ECSサービス・ALBの作成時にVPCを指定するため |
| PublicSubnet1Id | パブリックサブネット1のID | ALBの配置先として指定するため |
| PublicSubnet2Id | パブリックサブネット2のID | ALBの配置先として指定するため |
| PrivateSubnet1Id | プライベートサブネット1のID | ECSサービスの作成時にタスクの配置先として指定するため |
| PrivateSubnet2Id | プライベートサブネット2のID | ECSサービスの作成時にタスクの配置先として指定するため |
| RDSEndpoint | RDSの接続先ホスト名 | APIコンテナの環境変数(DB_HOST)に設定するため |
| ALBSecurityGroupId | ALB用セキュリティグループのID | ALBの作成時にセキュリティグループを指定するため |
| APISecurityGroupId | APIタスク用セキュリティグループのID | APIのECSサービス作成時にセキュリティグループを指定するため |
| FrontSecurityGroupId | フロントタスク用セキュリティグループのID | フロントのECSサービス作成時にセキュリティグループを指定するため |
また、スタック作成時に指定した DBMasterPassword(RDSのパスワード)も、後でAPIコンテナの環境変数に設定するため控えておいてください。