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.84k stars 9.19k forks source link

[New Data Source]: Plural `aws_elasticache_replication_group` data source #33942

Open mhintz-clickup opened 1 year ago

mhintz-clickup commented 1 year ago

Description

Elasticache Redis doesn't have a "list" data object that I can use. If a resource can't be put into a list then we can't "check to see if it exists" first BEFORE we try to deploy. I have some middleware that connects to elasticache redis for auditing and what we typically do is use a "list" type of object and check if its greater than 0.

This is the ONLY way I know of with terraform to check if something exists or not. The problem is if 1 environment doesn't have this an elasticache database, terraform fails. It can't handle errors when something doesn't exist gracefully.

An example of a resource that does have this is data "aws_rds_clusters". I'd start doing this:

data "aws_rds_clusters" "example" {
  filter {
    name   = "db-cluster-id"
    values = ["${local.my_variable}-example"]
  }
}

Then I could use a local variable to check if the list is empty:

locals {
  deploy_rds_example_resource = length(data.aws_rds_clusters.example.cluster_identifiers) > 0 ? 1 : 0
}

If the above is true, then I could now deploy:

data "aws_rds_cluster" "example" {
  count              = local.deploy_rds_example_resource
  cluster_identifier = "${local.my_variable}-example"
}

This allows me to re-use this module on as many environments as I want. If one of our environments doesn't have this database deployed, it will be skipped and everything else can still work.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elasticache_replication_group

Affected Resource(s) and/or Data Source(s)

data "aws_elasticache_replication_group"

Potential Terraform Configuration

No response

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