{Terraform} RDS/AWS

https://qiita.com/Brutus/items/cd5aab062ea6cebe436c
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance

 

 

-- 1. macにTerraformインストール

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
brew update
brew upgrade hashicorp/tap/terraform
terraform -help
terraform -version


-- 2. macAWS CLIインストール

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

which aws
aws --version

 


-- 3. tfファイル作成

vim main.tf


provider "aws" {
  region = "ap-northeast-1"
}


resource "aws_db_instance" "mysql01" {
  identifier                   = "mysql01"
  allocated_storage            = 20
  storage_type                 = "gp2"
  engine                       = "mysql"
  engine_version               = "8.0.28"
  instance_class               = "db.t3.micro"
  username                     = "root"
  password                     = "password"
  skip_final_snapshot          = true
  allow_major_version_upgrade  = false
  auto_minor_version_upgrade   = false
  delete_automated_backups     = true
  deletion_protection          = false
  multi_az                     = false
  performance_insights_enabled = false
  publicly_accessible          = true
}

 


-- 4. terraform 実行

terraform init -upgrade

terraform plan
terraform apply -auto-approve
terraform output

terraform state list

 

 

aws rds describe-db-instances

mysql -h mysql01.xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com  -P 3306 -u root -p

 

-- 5. クリーンアップ


terraform destroy -auto-approve