この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
AWSチームのすずきです。
オンデマンド自動スケーリング機能を備えるAmazon Aurora Serverless をCloudFormationを利用して設置。 メンテナンスウィンドウやパラメータグループの設定をテンプレートで管理する機会がありましたので、紹介させていただきます。
AWS::RDS::DBCluster
Use the EngineMode property to specify the DB engine mode of the DB cluster.
Use the ScalingConfiguration property to specify the scaling properties of the DB cluster, for DB clusters in serverless DB engine mode.
設定
DBSubnetGroup
- 異なるAZの2つ以上のサブネットを含む、DBサブネットグループを作成します。
- 既存のRDS用サブネットグループが存在する場合には、流用する事も可能です。
Resources:
RdsDbSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
SubnetIds:
- !Ref 'VpcRdsSubnetId1'
- !Ref 'VpcRdsSubnetId2'
RdsDbCluster:
Type: AWS::RDS::DBCluster
Properties:
DBSubnetGroupName: !Ref 'RdsDbSubnetGroup'
SecurityGroup
- DB接続許可に利用するセキュリティグループを設置します
Parameters:
VpcId:
Description: VPC ID
Type: AWS::EC2::VPC::Id
VpcApSubnet:
Description: AP VPC subnet
Type: String
Default: 172.31.0.0/16
Resources:
RdsSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref 'VpcId'
GroupDescription: Allow MySQL (TCP3306)
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
CidrIp: !Ref 'VpcApSubnet'
RdsDbCluster:
Type: AWS::RDS::DBCluster
Properties:
VpcSecurityGroupIds:
- !Ref 'RdsSecurityGroup'
DBClusterParameterGroup
- DBクラスタのパラメータグループ、aurora5.6のデフォルト設定を踏襲して作成します。
- DBのデフォルト文字コードやタイムゾーンの変更を行う場合、設定を追加します。
- 2019年1月時点のServerlessでは評価されない項目、証跡ログ設定(server_audit_logging)などに注意してご利用ください。
Resources:
RdsDbClusterParameterGroup:
Type: AWS::RDS::DBClusterParameterGroup
Properties:
Description: CloudFormation Aurora Cluster Parameter Group
Family: aurora5.6
Parameters:
server_audit_logging: 0
RdsDbCluster:
Type: AWS::RDS::DBCluster
Properties:
DBClusterParameterGroupName: !Ref 'RdsDbClusterParameterGroup'
DBCluster
- 「EngineMode」をserverlessとする事で、Aurora Serverlessが設置されます。
Parameters:
RdsDBClusterIdentifier:
Description: DBClusterIdentifier
Type: String
RdsDbMasterUsername:
Description: RdsDbMasterUsername
Type: String
RdsDbMasterUserPassword:
Description: RdsDbMasterUserPassword
Type: String
NoEcho: true
Resources:
RdsDbCluster:
Type: AWS::RDS::DBCluster
Properties:
BackupRetentionPeriod: 7
DBClusterIdentifier: !Ref 'RdsDBClusterIdentifier'
DBClusterParameterGroupName: !Ref 'RdsDbClusterParameterGroup'
DBSubnetGroupName: !Ref 'RdsDbSubnetGroup'
Engine: aurora
EngineMode: serverless
EngineVersion: 5.6.10a
MasterUsername: !Ref 'RdsDbMasterUsername'
MasterUserPassword: !Ref 'RdsDbMasterUserPassword'
Port: 3306
PreferredBackupWindow: 15:25-15:55
PreferredMaintenanceWindow: Tue:16:05-Tue:16:35
ScalingConfiguration:
MinCapacity: 2
AutoPause: true
MaxCapacity: 4
SecondsUntilAutoPause: 1800
StorageEncrypted: true
VpcSecurityGroupIds:
- !Ref 'RdsSecurityGroup'
StorageEncrypted
- Aurora Serverless はストレージの暗号化は必須、KMSのキーにより保護されます。
- 今回「KmsKeyId」の指定は省略、デフォルトキー「aws/rds」を利用しています。
PreferredBackupWindow、PreferredMaintenanceWindow
- 夜間バッチ処理などとの干渉を回避するため、バックアップウィンドウ、メンテナンスウィンドウの設定を実施しています。
- 省略した場合、東京リージョンでは13:00–21:00 UTC の時間ブロックから30分のメンテナンスウィンドウがランダムに設定されます。
- 2019年1月時点、Aurora Serverlessのバックアップウィンドウ、メンテナンスウィンドウ設定は作成時のみ可能です。設置後の変更はできない点に注意ください。
ScalingConfiguration
- Aurora Serverlessの自動スケーリング機能の設定を実施します。
実行例
- CloudFormationによるAurora Serverlessの作成、約7分で完了しました。
CloudFormationイベント
RDSコンソール
まとめ
CloudFormationを利用してAurora Serverlessを設置する事ができました。
メンテナンスウィンドウ時間の指定やパラメータグループの設定値を変更する場合や、 AWSリソースのタグ管理の効率化手段としてご活用ください。
テンプレート
今回の検証、以下のCloudFormationテンプレートを利用しました。