前提
AWS CloudFormationで簡単なネットワーク構成を作っています。
実現したいこと
CloudFormationでRDSを作成した際にテーブルも自動で作成したい
発生している問題・エラーメッセージ
AWS CloudFormationでスタック作成時にはエラーなしだが、
CloudFormationのAWS::EC2::Instanceで
UserDataの欄に色々書いたコマンドが意図通りに機能しない
該当のソースコード
yml
1AWSTemplateFormatVersion: 2010-09-09 2Transform: AWS::Serverless-2016-10-31 3Description: testDemo 4 5Resources: 6 # VPCの設定 7 VPC: 8 Type: 'AWS::EC2::VPC' 9 Properties: 10 CidrBlock: 10.0.0.0/16 11 Tags: 12 - Key: Name 13 Value: testVPC 14 15 # サブネットの設定 16 PublicSubnet1a: 17 Type: 'AWS::EC2::Subnet' 18 Properties: 19 VpcId: !Ref VPC 20 CidrBlock: 10.0.0.0/20 21 AvailabilityZone: "ap-northeast-1a" 22 Tags: 23 - Key: Name 24 Value: testPublicSubnet1a 25 26 PrivateSubnet1a: 27 Type: 'AWS::EC2::Subnet' 28 Properties: 29 VpcId: !Ref VPC 30 CidrBlock: 10.0.128.0/20 31 AvailabilityZone: "ap-northeast-1a" 32 Tags: 33 - Key: Name 34 Value: testPrivateSubnet1a 35 36 PrivateSubnet1c: 37 Type: 'AWS::EC2::Subnet' 38 Properties: 39 VpcId: !Ref VPC 40 CidrBlock: 10.0.240.0/20 41 AvailabilityZone: "ap-northeast-1c" 42 Tags: 43 - Key: Name 44 Value: testPrivateSubnet1c 45 46 # インターネットゲートウェイの設定 47 InternetGateway: 48 Type: 'AWS::EC2::InternetGateway' 49 Properties: 50 Tags: 51 - Key: Name 52 Value: testInternetGateway 53 54 # インターネットゲートウェイをVPCにアタッチ 55 VPCGatewayAttachment: 56 Type: 'AWS::EC2::VPCGatewayAttachment' 57 Properties: 58 VpcId: !Ref VPC 59 InternetGatewayId: !Ref InternetGateway 60 61 # ルートテーブルの設定 62 PublicRouteTable: 63 Type: 'AWS::EC2::RouteTable' 64 Properties: 65 VpcId: !Ref VPC 66 Tags: 67 - Key: Name 68 Value: testPublicRouteTable 69 70 # ルートの設定 71 PublicRoute: 72 Type: 'AWS::EC2::Route' 73 Properties: 74 RouteTableId: !Ref PublicRouteTable 75 DestinationCidrBlock: 0.0.0.0/0 76 GatewayId: !Ref InternetGateway 77 78 # サブネットとルートテーブルの関連付け 79 PublicSubnet1aRouteTableAssociation: 80 Type: AWS::EC2::SubnetRouteTableAssociation 81 Properties: 82 SubnetId: !Ref PublicSubnet1a 83 RouteTableId: !Ref PublicRouteTable 84 85 # EC2用セキュリティグループの設定 86 EC2security: 87 Type: 'AWS::EC2::SecurityGroup' 88 Properties: 89 GroupDescription: EC2security 90 VpcId: !Ref VPC 91 SecurityGroupIngress: 92 - IpProtocol: tcp 93 FromPort: 80 94 ToPort: 80 95 CidrIp: 0.0.0.0/0 96 - IpProtocol: tcp 97 FromPort: 22 98 ToPort: 22 99 CidrIp: 0.0.0.0/0 100 SecurityGroupEgress: 101 - IpProtocol: -1 102 CidrIp: 0.0.0.0/0 103 Tags: 104 - Key: Name 105 Value: testEC2security 106 107 # RDS(PostgreSQL)用セキュリティグループの設定 108 RDSsecurity: 109 Type: 'AWS::EC2::SecurityGroup' 110 Properties: 111 GroupDescription: RDSsecurity 112 VpcId: !Ref VPC 113 SecurityGroupIngress: 114 - IpProtocol: tcp 115 FromPort: 5432 116 ToPort: 5432 117 - SourceSecurityGroupId: !Ref EC2security 118 IpProtocol: -1 119 SecurityGroupEgress: 120 - IpProtocol: -1 121 CidrIp: 0.0.0.0/0 122 Tags: 123 - Key: Name 124 Value: testRDSsecurity 125 126 # DBサブネットグループの設定 127 DBSubnetGroup: 128 Type: 'AWS::RDS::DBSubnetGroup' 129 Properties: 130 DBSubnetGroupDescription: testDemoDbSubnetGroup 131 SubnetIds: # 紐づけるサブネット(2個以上) 132 - !Ref PrivateSubnet1a 133 - !Ref PrivateSubnet1c 134 135 # RDS(PostgreSQL)の設定 136 RDS: 137 Type: 'AWS::RDS::DBInstance' 138 Properties: 139 Engine: postgres # DBエンジンの設定 140 EngineVersion: 13.7 # PostgreSQLのversion 141 DBInstanceIdentifier: testDemoRdsPostgres # DBインスタンスの識別子 142 DBName: testDemoRdsPostgres # DB名 143 MasterUsername: postgres # マスターユーザー名 144 MasterUserPassword: postgres # マスターパスワード 145 DBInstanceClass: db.t3.micro # DBインスタンスクラス 146 StorageType: gp2 # ストレージタイプ 147 AllocatedStorage: '20' # ストレージ割り当て 148 VPCSecurityGroups: 149 - !Ref RDSsecurity # VPCセキュリティグループ(ファイアウォール) 150 DBSubnetGroupName: !Ref DBSubnetGroup # DBサブネットの指定 151 PubliclyAccessible: false # パブリックアクセスの有無設定 152 AvailabilityZone: ap-northeast-1a # アベイラビリティゾーンの設定 153 MultiAZ: false # マルチAZしない 154 EnablePerformanceInsights: false # Performance Insights有無の設定 155 MonitoringInterval: 0 # 拡張モニタリングの詳細度の設定(0で拡張モニタリング無効) 156 Tags: 157 - Key: Name 158 Value: testRDS 159 160 # EC2の設定 161 EC2: 162 Type: 'AWS::EC2::Instance' 163 Properties: 164 ImageId: ami-0bba69335379e17f8 165 InstanceType: t2.micro 166 KeyName: test_demo # 作成済のキーペア名を入力 167 NetworkInterfaces: 168 - AssociatePublicIpAddress: "true" 169 DeviceIndex: "0" 170 SubnetId: !Ref PublicSubnet1a 171 GroupSet: 172 - !Ref EC2security 173 UserData: 174 # psqlクライアント(ver.13)をインストール 175 # PostgreSQLサーバをインストール 176 # PostgreSQLクライアントのセットアップ 177 # RDS(PostgreSQL)に接続し、testdbデータベースを新規作成 178 # 一旦抜けてからtestdbデータベース指定して再接続 179 # TESTテーブルの作成 180 Fn::Base64: | 181 #!/bin/bash 182 sudo amazon-linux-extras install postgresql13 183 sudo yum -y install postgresql-server 184 sudo /usr/bin/postgresql-setup initdb 185 PGPASSWORD=postgres psql -h testdemordspostgres.cjetlonhplob.ap-northeast-1.rds.amazonaws.com -U postgres 186 Create database testdb; 187 exit 188 PGPASSWORD=postgres psql -h testdemordspostgres.cjetlonhplob.ap-northeast-1.rds.amazonaws.com -U postgres -d testdb 189 Create table TEST( 190 id serial, 191 name varchar(32), 192 score varchar(32), 193 PRIMARY KEY(id) 194 ); 195 Tags: 196 - Key: Name 197 Value: testEC2 198 199 # EC2用のElasticIPを取得 200 ElasticIP: 201 Type: 'AWS::EC2::EIP' 202 Properties: 203 # InstanceId: !Ref EC2 204 Domain: vpc 205 206 # EC2にElasticIPを関連付ける 207 ElasticIPAssociate: 208 Type: AWS::EC2::EIPAssociation 209 Properties: 210 AllocationId: !GetAtt ElasticIP.AllocationId 211 InstanceId: !Ref EC2
試したこと
UserDataで
sudo amazon-linux-extras install postgresql13 sudo yum -y install postgresql-server sudo /usr/bin/postgresql-setup initd
によってpsqlクライアント(ver.13)、PostgreSQLサーバをインストールし、
PostgreSQLクライアントのセットアップまで出来ていることは確認済みだが、
RDS(PostgreSQL)に接続してtestdbデータベースを新規作成する部分は出来ていない。
sshクライアントツールのpuTTYを使ってEC2にSSHログインし、EC2のコンソール上から
PGPASSWORD=postgres psql -h testdemordspostgres.cjetlonhplob.ap-northeast-1.rds.amazonaws.com -U postgres Create database testdb; exit PGPASSWORD=postgres psql -h testdemordspostgres.cjetlonhplob.ap-northeast-1.rds.amazonaws.com -U postgres -d testdb Create table TEST( id serial, name varchar(32), score varchar(32), PRIMARY KEY(id) );
のような順番で手打ち入力していけばtestテーブルが作成されることは確認出来ているが、
CloudFormationでRDSを作成した際にテーブルも自動で作成する方法があれば知りたいです。
よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/06 01:31