LoginSignup
10
6

More than 5 years have passed since last update.

AWS CLI を使って RDS を作成する (自分用メモ)

Last updated at Posted at 2019-04-15

AWS CLI を使って外部からアクセス出来る RDS インスタンスを作る方法が公式ドキュメントに無かったのでメモ。
aws rds コマンドを使って rds を作成するには、DB subnet group を指定する必要があるらしい。さもなければ以下のようなエラーが出る。

An error occurred (InsufficientDBInstanceCapacity) when calling the CreateDBInstance operation: Cannot create a db.t2.micro database instance because there are no availability zones with sufficient capacity for non-VPC and storage type

新しい DB subnet group を作るには チュートリアル: Amazon RDS DB インスタンスで使用する Amazon VPC の作成 の手順が近い。ここでは Single Public Subnet を作る(us-west-2 の例)。

VPC と DB Subnet Group の作成

  • VPC 作成
    • https://console.aws.amazon.com/vpc/
    • Launch VPC Wizard
    • VPC with a Single Public Subnet
    • VPC name: hogehoge-db-vpc
    • Availability Zone: us-west-2a
    • Subnet name: hogehoge-db-subnet
  • Subnet 作成
    • https://console.aws.amazon.com/vpc/
    • Subnets
    • Create subnet
    • Name tag: hogehoge-db-subnet2
    • VPC: hogehoge-db-vpc
    • Availability zone: us-west-2b (VPC 作成で作った subnet と違う zone)
    • IPv4 CIDR: 10.0.1.0/24
    • hogehoge-db-subnet > Route Table > Route Table をメモ (例 rtb-0f80a908802976794)
    • hogehoge-db-subnet2 > Route Table > Edit route table association > メモした Route Table を選択
  • セキュリティグループ作成
    • https://console.aws.amazon.com/vpc/
    • Security Groups
    • Create Security Groups
    • Security group name: hogehoge-db-security-group
    • VPC: hogehoge-db-vpc
    • hogehoge-db-security-group > Inbound Rules > Edit Rules > Add Rule
    • Port: 3306
    • Source: My IP
    • あとはデフォルト
    • Security Group 名をメモ: 例 sg-0ffa93ab8dc49aea1
  • DB サブネットグループ作成
    • https://console.aws.amazon.com/rds/
    • Subnet groups
    • Create DB subnet groups
    • Name: hogehoge-db-subnet-group
    • VPC: hogehoge-db-vpc
    • Add subnets > Add all the subnets related to this VPC

RDS 作成

export AWS_PROFILE=hogehoge_profile
export AWS_REGION=us-west-2

aws rds create-db-instance \
    --db-instance-identifier hogehoge-db-instance \
    --db-instance-class db.t2.micro \
    --engine MySQL \
    --engine-version 8.0.11 \
    --allocated-storage 20 \
    --master-username root \
    --master-user-password password \
    --backup-retention-period 3 \
    --vpc-security-group-ids sg-0ffa93ab8dc49aea1 \
    --publicly-accessible \
    --db-subnet-group-name hogehoge-db-subnet-group

進捗表示

aws rds describe-db-instances | jq '.DBInstances[] | {"name": .DBInstanceIdentifier, "status":.DBInstanceStatus}'

Endpoint 表示

aws rds describe-db-instances | jq '.DBInstances[].Endpoint'

ログイン

mysql -uroot -ppassword -hhogehoge-db-instance.higehoge.us-west-2.rds.amazonaws.com

RDS 削除

削除

aws rds delete-db-instance --db-instance-identifier hogehoge-db-instance --skip-final-snapshot

DB サブネットグループと VPC も削除

Terraform で同じ事をやる

(TBD)

10
6
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
10
6