hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.86k stars 9.21k forks source link

[Docs]: #28375

Open dovka opened 1 year ago

dovka commented 1 year ago

Documentation Link

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster_instance.html

Description

The doc says:

Unlike other RDS resources that support replication, with Amazon Aurora you do not designate a primary and subsequent replicas. Instead, you simply add RDS Instances and Aurora manages the replication. You can use the count meta-parameter to make multiple instances and join them all to the same RDS Cluster, or you may specify different Cluster Instance resources with various instance_class sizes.

This makes it unclear to me how to create AWS Postgres Aurora read replica with instance type different from writer or one replica different form another.

I create AWS Postgres Aurora writer + 1 read replicas using the documentation example - it works with no issues.

However I need the flexibility of creating the read replicas of different instance types or even another instance class like db.serverless (Aurora serverless V2)

I can easily create and add alternative read replicas to my rds cluster via AWS UI or AWS CLI but can't figure out how to do it via terraform. Is it implemented and this is only a case of unclear documentation?

This line makes me assume that I can define multiple aws_rds_cluster_instance objects and add them up to the same cluster: "or you may specify different Cluster Instance resources with various instance_class sizes."

but docs has no examples of how to do it and my attempt to do so casues an error:

resource "aws_rds_cluster_instance" "cluster_instances_ro" {
  count              = 1
  identifier         = "aurora-cluster-ro-demo-${count.index}"
  cluster_identifier = aws_rds_cluster.default.id
  instance_class     = "db.r6g.xlarge"
  engine             = aws_rds_cluster.default.engine
  engine_version     = aws_rds_cluster.default.engine_version
}
aws_rds_cluster_instance.cluster_instances_ro[0]: Creating...
Error: error creating RDS Cluster (aurora-cluster-demo) Instance: DBInstanceAlreadyExists: DB instance >already exists
status code: 400, request id: 3e33b2c9-1284-4c2d-b8c7-2d8eedd997fe
on db_aurora.tf line 65, in resource "aws_rds_cluster_instance" "cluster_instances_ro":
65: resource "aws_rds_cluster_instance" "cluster_instances_ro" {

Thank you very much for your time! David

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

AdamJCavanaugh commented 1 year ago

My guess from what you provided is that you have another aws_rds_cluster_instance resource with the same identifier set? You'll need to have a unique string or omit it to allow Terraform to assign a random, unique identifier.

If this is not the case, please provide a complete example?

justinretzolk commented 1 year ago

Hey @dovka 👋 Thank you for taking the time to raise this! I think I might see what's happening here. The documentation quote that you called out mentioned that you can either use the count meta-parameter or create distinct resources. What this means is that if you just need multiple instances, but don't need varying sizes, you can use count, as is shown in the example in the docs:

resource "aws_rds_cluster_instance" "cluster_instances" {
  count              = 2
  identifier         = "aurora-cluster-demo-${count.index}"
  cluster_identifier = aws_rds_cluster.default.id
  instance_class     = "db.r4.large"
  engine             = aws_rds_cluster.default.engine
  engine_version     = aws_rds_cluster.default.engine_version
}

On the other hand, if you need varying sizes, you'd omit the count parameter, and instead use multiple distinct resources, similar to:

resource "aws_rds_cluster_instance" "cluster_instance_large" {
  identifier         = "aurora-cluster-demo-large"
  cluster_identifier = aws_rds_cluster.default.id
  instance_class     = "db.r4.large"
  engine             = aws_rds_cluster.default.engine
  engine_version     = aws_rds_cluster.default.engine_version
}

resource "aws_rds_cluster_instance" "cluster_instance_xlarge" {
  identifier         = "aurora-cluster-demo-xlarge"
  cluster_identifier = aws_rds_cluster.default.id
  instance_class     = "db.r4.xlarge"
  engine             = aws_rds_cluster.default.engine
  engine_version     = aws_rds_cluster.default.engine_version
}

Note that the identifier is unique; as called out above, I suspect that the error you received was due to using the same identifier for multiple instances.

justinretzolk commented 2 weeks ago

Hey @dovka 👋 I wanted to follow up here and see if you're okay with this issue being closed, or whether you still need assistance.