Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a user and database only after an AWS RDS instance is provisioned?

Tags:

terraform

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]
}
like image 301
user1869257 Avatar asked Aug 29 '19 19:08

user1869257


People also ask

How do I add a database to an existing RDS instance?

In the upper-right corner of the Amazon RDS console, choose the AWS Region in which you want to create the DB instance. In the navigation pane, choose Databases. Choose Create database. In Choose a database creation method, select Standard Create.

Can RDS instance have multiple Databases?

It is the basic building block of Amazon RDS. A DB instance can contain multiple user-created databases, and can be accessed using the same client tools and applications you might use to access a standalone database instance.

How many Databases can I create on a single Amazon RDS instance?

Note that RDS for SQL Server has a limit of up to 100 databases on a single DB instance to learn more see the Amazon RDS for SQL Server User Guide. If your application requires more DB instances, you can request additional DB instances via this request form.

What is RDS provisioned storage?

Provisioned IOPS storage is a storage type that delivers predictable performance, and consistently low latency. Provisioned IOPS storage is optimized for online transaction processing (OLTP) workloads that have consistent performance requirements. Provisioned IOPS helps performance tuning of these workloads.


1 Answers

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.

like image 72
romeara Avatar answered Sep 18 '22 06:09

romeara