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:
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.
Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.
Volunteering to Work on This Issue
If you are interested in working on this issue, please leave a comment.
If this would be your first contribution, please review the contribution guide.
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:
Then I could use a local variable to check if the list is empty:
If the above is true, then I could now deploy:
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