5

I have a Terraform project which uses the MySQL provider. Because of the way Terraform works, the MySQL provider will fail to load correctly because the AWS_RDS instance doesn't exist. Is there a way for me to delay or split my project so that the MySQL provider doesn't load during the main "apply"?

I've examined workspaces, and I see that you can use a count to trigger a resource to load or not load based off of the workspace name. But that didn't work anyway I thought.

I've also seen this issue mentioned on the Hashicorp GitHub, but on one seems to be offering a workaround

Any help is greatly appreciated.

Here is a snippet of my data-storage configuration thus far:

resource "aws_db_subnet_group" "default" {
  name       = "${var.database_subnet_group_name}"
  subnet_ids = ["${var.database_subnets}"]

  tags {
    Name = "${var.database_subnet_group_name}"
  }
}

resource "aws_rds_cluster_instance" "cluster_instances" {
  count              = 1
  identifier         = "app-aurora-cluster-${count.index}"
  cluster_identifier = "${aws_rds_cluster.some_cluster.id}"
  instance_class     = "db.t2.medium"
  db_subnet_group_name = "${var.database_subnet_group_name}"
  publicly_accessible = true
}

resource "aws_rds_cluster" "some_cluster" {
  cluster_identifier = "app-aurora-cluster"
  availability_zones = ["${var.database_azs}"]
  database_name      = "${var.database_name}"
  db_subnet_group_name = "${var.database_subnet_group_name}"
  master_username    = "auroradmin"
  master_password    = "%XwPn}gU6sX<y8Wx"
  skip_final_snapshot =  true
  vpc_security_group_ids = ["${var.database_security_groups}"]
}
/*
# This has to be commented out during the first pass
# because Terraform will try to connect to the 
# MySQL Aurora server to refresh the state even 
# though it currently doesn't exist.
# Configure the MySQL provider
provider "mysql" {

  endpoint = "${aws_rds_cluster.some_cluster.endpoint}"
  username = "auroradmin"
  password = "%XwPn}gU6sX<y8Wx"
}

1 Answers1

1

I think you could achieve something similar by first running Terraform against your RDS resources, and then running Terraform again on all resources.

On your first pass, you can use -target option to target only a subset of the resources. (ie. RDS resources).

Be aware that using this option is not recommended in Terraform docs. If I were you, I would use it for time being and change it as soon as one of these issues below is solved: https://github.com/hashicorp/terraform/issues/10462 https://github.com/hashicorp/terraform/issues/2430 https://github.com/hashicorp/terraform/issues/4149

For details on how to use -target and its caveats, see: https://www.terraform.io/docs/commands/plan.html#resource-targeting

Yekta Leblebici
  • 323
  • 2
  • 7