以下の内容はhttps://htn20190109.hatenablog.com/entry/2024/12/18/010125より取得しました。


{RDS}ブルー/グリーンデプロイの作成

 

https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/UserGuide/blue-green-deployments-creating.html
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/mysql-install-cli.html


RDS for MySQL DB インスタンスのブルー/グリーンデプロイを作成する前に、自動バックアップを有効にする必要があります。

 


-- 1. VPC、サブネット、RDS、EC2作成


cat <<-'EOF' > main.tf

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

resource "aws_vpc" "vpc01" {
  cidr_block = "10.0.0.0/16"
  enable_dns_support  = true
  enable_dns_hostnames = true
  
  tags = {
    Name = "vpc01"
  }
}


resource "aws_subnet" "subnet01" {
  vpc_id = aws_vpc.vpc01.id
  availability_zone = "ap-northeast-1a"
  cidr_block        = "10.0.1.0/24"

  tags = {
    Name = "subnet01"
  }
}

resource "aws_subnet" "subnet02" {
  vpc_id = aws_vpc.vpc01.id
  availability_zone = "ap-northeast-1c"
  cidr_block        = "10.0.2.0/24"

  tags = {
    Name = "subnet02"
  }
}

resource "aws_subnet" "subnet03" {
  vpc_id = aws_vpc.vpc01.id
  availability_zone = "ap-northeast-1d"
  cidr_block        = "10.0.3.0/24"

  tags = {
    Name = "subnet03"
  }
}


resource "aws_internet_gateway" "igw01" {
  vpc_id = aws_vpc.vpc01.id

  tags = {
    Name = "igw01"
  }
}


resource "aws_route_table" "rt01" {
  vpc_id = aws_vpc.vpc01.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw01.id
  }

  tags = {
    Name = "rt01"
  }
}


resource "aws_route_table_association" "rt01_subnet01" {
  route_table_id = aws_route_table.rt01.id
  subnet_id      = aws_subnet.subnet01.id
}


resource "aws_security_group" "sg01" {
  name ="sg01"
  vpc_id = aws_vpc.vpc01.id
  
  ingress {
    from_port = 22
    to_port = 22
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    from_port = 3306
    to_port = 3306
    protocol = "tcp"
    cidr_blocks = ["10.0.0.0/16"]
  }
  
  
  egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# サブネットグループの作成

resource "aws_db_subnet_group" "subnetg01" {
  name       = "subnetg01"
  subnet_ids = [aws_subnet.subnet01.id, aws_subnet.subnet02.id, aws_subnet.subnet03.id]

  tags = {
    Name = "subnetg01"
  }
}

 


# 接続確認用EC2
resource "aws_instance" "vm01"{
  ami = "ami-0404778e217f54308"
  instance_type = "t3.nano"
  key_name = "key1"
  
  tags = {
    Name = "vm01"
  }
  subnet_id     = aws_subnet.subnet01.id
  instance_market_options {
    market_type = "spot"
    spot_options {
      spot_instance_type = "one-time"
    }
  }
  vpc_security_group_ids =[aws_security_group.sg01.id]
  associate_public_ip_address = true
}


# パラメータグループの作成(ブルー)

resource "aws_db_parameter_group" "pmg01" {
  name   = "pmg01"
  family = "mysql8.0"

  parameter {
    name  = "max_connections"
    value = "300"
  }
}

 


# RDSインスタンス作成(ブルー)

resource "aws_db_instance" "mysql01" {
  identifier                   = "mysql01"
  allocated_storage            = 20
  storage_type                 = "gp2"
  engine                       = "mysql"
  engine_version               = "8.0.39"
  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
  parameter_group_name         = aws_db_parameter_group.pmg01.name
  db_subnet_group_name         = aws_db_subnet_group.subnetg01.name
  vpc_security_group_ids       = [aws_security_group.sg01.id]
  backup_retention_period      = 1
}


# パラメータグループの作成(グリーン)
resource "aws_db_parameter_group" "pmg02" {
  name   = "pmg02"
  family = "mysql8.0"

  parameter {
    name  = "max_connections"
    value = "600"
  }
}

 

 

 

EOF


cat <<-'EOF' > outputs.tf

output "subnet01_id" {
  value = aws_subnet.subnet01.id
  description = "subnet01.id"
}

output "subnet02_id" {
  value = aws_subnet.subnet02.id
  description = "subnet02.id"
}

output "subnet03_id" {
  value = aws_subnet.subnet03.id
  description = "subnet03.id"
}


output "sg01_id" {
  value = aws_security_group.sg01.id
  description = "sg01.id"
}

EOF

 

terraform init
terraform validate
terraform fmt
terraform -version

terraform plan

terraform apply -auto-approve


# terraform destroy -auto-approve

※ destroy中、下記エラー発生
Error: deleting RDS DB Parameter Group (pmg02)
手動削除

 


-- 2. 接続確認(ブルー)


sudo yum install mariadb
mysql --version
mysql --help

 

 

while true; do
date

MYSQL_PWD='password' mysql -h mysql01.111111111111.ap-northeast-1.rds.amazonaws.com -P 3306 -u root -e "select now()"

sleep 5
done

 


-- 3. ブルー/グリーンデプロイの作成


aws rds create-blue-green-deployment \
--blue-green-deployment-name bgd01 \
--source arn:aws:rds:ap-northeast-1:999999999999:db:mysql01 \
--target-engine-version 8.0.40 \
--target-db-parameter-group-name pmg02

aws rds describe-blue-green-deployments

while true; do
date

aws rds describe-blue-green-deployments

sleep 5
done

-- 4. 接続確認(グリーン)

while true; do
date

MYSQL_PWD='password' mysql -h mysql01-green-111111.111111111111.ap-northeast-1.rds.amazonaws.com -P 3306 -u root -e "select now()"

sleep 5
done

 

-- 5. ブルー/グリーンデプロイの切り替え

aws rds switchover-blue-green-deployment \
--blue-green-deployment-identifier bgd-1111111111111111 \
--switchover-timeout 600

aws rds describe-blue-green-deployments

 

-- 6. ブルー/グリーンデプロイの削除


aws rds delete-blue-green-deployment \
--blue-green-deployment-identifier bgd-1111111111111111 \
--no-delete-target


aws rds describe-blue-green-deployments


-- 7. 古いブルーの削除

aws rds delete-db-instance --db-instance-identifier mysql01-old1 --skip-final-snapshot

aws rds describe-db-instances | jq -c '.DBInstances[] | [ .DBInstanceIdentifier , .DBInstanceStatus ] '

 

 

 




以上の内容はhttps://htn20190109.hatenablog.com/entry/2024/12/18/010125より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14