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"
}
  • Maybe show your (related) terraform configuration(s)? – Dan Cornilescu Feb 14 '18 at 2:55
  • 1
    @DanCornilescu I'm a Terra-Scrub right now, but I've edited my post with what I believe you mean as my configuration. Basically, I create the RDS stuff first, then uncomment the mysql stuff, and run my "apply" again. Clearly a suboptimal and unintelligent way to go about things. – Black Dynamite Feb 14 '18 at 2:58
  • Wouldn't you need the "mysql" provider to reference the cluster instance instead of the cluster? (sorry, my Q may be dumb, I only read a bit about terraform) – Dan Cornilescu Feb 14 '18 at 3:42
  • I was looking at this example: terraform.io/docs/providers/mysql/index.html – Dan Cornilescu Feb 14 '18 at 3:44
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.