6
votes

Any recommendations on how to wait for an AWS RDS database host to be provisioned before creating a database or user? To accomplish this, I am attempting to use depends_on parameter in order for it to wait for the RDS database to be provisioned first. It seems the provider cannot wait for the resource.

I get the following error when running this:

The provider argument name "depends_on" is reserved for use by Terraform in a future version.

--- Terraform Plan Snippet ----

# Provision AWS PostgreSQL Dev Database
resource "aws_db_instance" "dev_db" {
    identifier = "dev"
    allocated_storage    = 100
    storage_type         = "gp2"
    engine               = "postgres"
    engine_version       = "10.9"
    port                 = 1433
    instance_class       = "db.t3.medium"
    name                 = "dev"
    username             = "dev"
    password             = "mydevpassword"
    parameter_group_name = "postgress10"
    tags = {
        Name = "dev"
    }
    skip_final_snapshot = true
}
# Setup PostgreSQL Provider After RDS Database is Provisioned
provider "postgresql" {
    host            = "${aws_db_instance.dev_db.address}"
    port            = 1433
    username        = "dev"
    password        = "mydevpassword"
    depends_on      = [aws_db_instance.dev_db]
}
# Create App User
resource "postgresql_role" "application_role" {
    name                = "dev_appuser"
    login               = true
    password            = "myappuserpassword"
    encrypted_password  = true
    depends_on          = [aws_db_instance.dev_db]
}
# Create Database 
resource "postgresql_database" "dev_db" {
    name              = "mydatabase1"
    owner             = "dev"
    template          = "template0"
    lc_collate        = "C"
    connection_limit  = -1
    allow_connections = true
    depends_on        = [aws_db_instance.dev_db]
}
1
depends_on should be a list, like: depends_on = [aws_db_instance.dev_db]Max Smolens
Thanks! Now I'm having an issue with telling the provider connection to wait until the database is provisioned. Seems that depends_on is not designed for a provider or not available yet in Terraform. I get this error "The provider argument name "depends_on" is reserved for use by Terraform in a future version." Terraform v0.12.7 + provider.aws v2.25.0 + provider.postgresql v1.1.0user1869257
Terraform handles most dependencies implicitly. If you omit depends_on altogether do you still have trouble?Max Smolens
I tried that. The Posgress provider seems to try to make a connection before the database is ready. "Error: Error initializing PostgreSQL client: error detecting capabilities: error PostgreSQL version: dial tcp 172.31.68.174:1433: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."user1869257
I see. It looks like you're hitting this issue: github.com/terraform-providers/terraform-provider-postgresql/…Max Smolens

1 Answers

5
votes

I asked about this in the postgres provider repository in a GitHub Issue, and the maintainers were able to provide a workaround for this - if you specify the expected_version in the provider configuration, it does not attempt to connect until a resource actually uses the connection.

This means that the dependency behavior of resources can be used to prevent connection until Terraform finishes the setup of the RDS resource. This seems to work in both the plan case and the apply case.