Home > Software engineering >  Use Terraform to deploy MySQL 8.0 in AWS Aurora V2
Use Terraform to deploy MySQL 8.0 in AWS Aurora V2

Time:04-30

I'm attempting to deploy a serverless MySQL 8.0 service using AWS Aurora V2 using Terraform.

Terraform details (not on latest version, but should be compatible with latest AWS provider version):

Terraform v0.15.4
on linux_amd64
  provider registry.terraform.io/hashicorp/aws v4.12.0
  provider registry.terraform.io/hashicorp/consul v2.15.1
  provider registry.terraform.io/hashicorp/random v3.1.3
  provider registry.terraform.io/hashicorp/template v2.2.0
  provider registry.terraform.io/hashicorp/vault v3.5.0

Here's the relevant main.tf file:

resource "aws_rds_cluster" "database" {
  cluster_identifier      = var.cluster_identifier
  db_subnet_group_name    = aws_db_subnet_group.db_subnet_group.name
  vpc_security_group_ids  = var.vpc_security_group_ids
  engine_mode             = "serverless"
  enable_http_endpoint    = var.enable_http_endpoint
  master_username         = var.master_username
  master_password         = random_password.rng.result
  database_name           = var.name
  backup_retention_period = var.backup_retention_period
  skip_final_snapshot     = var.skip_final_snapshot
  deletion_protection     = var.deletion_protection
  engine                  = "aurora-mysql"
  engine_version          = "8.0.mysql_aurora.3.02.0"

  serverlessv2_scaling_configuration {
    max_capacity = var.max_capacity
    min_capacity = var.min_capacity
  }

  lifecycle {
    ignore_changes = [
      engine_version,
      availability_zones,
      master_username,
      master_password,
    ]
  }

  tags = {
    Environment = var.env
    Name        = var.name
  }
}

resource "aws_rds_cluster_instance" "cluster_instances" {
  identifier         = "${var.cluster_identifier}-serverless"
  cluster_identifier = aws_rds_cluster.database.id
  instance_class     = "db.serverless"
  engine             = aws_rds_cluster.database.engine
  engine_version     = aws_rds_cluster.database.engine_version
}

resource "aws_db_subnet_group" "db_subnet_group" {
  name       = "${var.cluster_identifier}-subnet-group"
  subnet_ids = var.subnet_ids

  tags = {
    Environment = var.env
  }
}

resource "random_password" "rng" {
  length  = 16
  special = false

  keepers = {
    cluster_identifier = var.cluster_identifier
  }
}

The above file was originally a serverless MySQL 5.7 service using Aurora V1. I modified this existing main.tf file using these resources:

The terraform plan goes fine. When running terraform apply this error is present:

module.aurora.aws_rds_cluster.database: Creating...
╷
│ Error: error creating RDS cluster: InvalidParameterValue: The engine mode serverless you requested is currently unavailable.
│   status code: 400, request id: 060f8bce-4bc4-4462-9735-78495ecaf308
│ 
│   with module.aurora.aws_rds_cluster.database,
│   on modules/aws/rds/main.tf line 1, in resource "aws_rds_cluster" "database":
│    1: resource "aws_rds_cluster" "database" {
│ 
╵

I can't infer much from this error, I'm guessing something isn't supporting this since the AWS provider v4.12.0 was released yesterday. I assume it could also be the version of Terraform itself, but again, I believe Terraform v0.15.4 is compatible with the v4.12.0 of the AWS provider.

My main goal is to switch the service from MySQL 5.7 serverless to MySQL 8.0 serverless.

CodePudding user response:

The engine_mode for Aurora Serverless 2 is provisioned, not serverless.

    engine_mode               = "provisioned"

CodePudding user response:

Try putting these:

  engine                  = "aurora-mysql"
  engine_version          = "8.0.mysql_aurora.3.02.0"

both in aws_rds_cluster_instance and aws_rds_cluster.

EDIT:

Since you're actually converting an Aurora Serverless V1 to V2, you might want to consider the following:

  • Add one or more Aurora Serverless v2 reader DB instances to an existing provisioned cluster. To use Aurora Serverless v2 for the writer, perform a failover to one of the Aurora Serverless v2 DB instances. For the entire cluster to use Aurora Serverless v2 DB instances, remove any provisioned writer DB instances after promoting the Aurora Serverless v2 DB instance to the writer.

  • Create a new cluster with the appropriate DB engine and engine version. Use any of the standard methods. For example, restore a cluster snapshot or create a clone of an existing cluster. Choose Aurora Serverless v2 for some or all of the DB instances in the new cluster.

If you create the new cluster through cloning, you can't upgrade the engine version at the same time. Make sure that the original cluster is already running an engine version that's compatible with Aurora Serverless v2.

There might be more difference, check this.

Also, make sure you're in the region that supports Aurora Serverless V2 - full info here.

  • Related