简体   繁体   中英

Terraform for AWS: Pass specific resource address based on AZ of the beanstalk instance

Setup

I have a system with a beanstalk application that can span multiple AZs. The application needs to get data from ElastiCache instances, but they need to be the closest to each other.

For instance, The beanstalk spins up instances B1 and B2 in AZs A and B. There is the primary ElastiCache instance E1 in AZ A, with a read-replica in B , being E2.

B1 needs to contact E1, and B2 should get data from E2 for efficiency.

Question

In Terraform, how do I tell the system to pass the specific address of the ElastiCache instances based on what AZ they are created in? If an auto-scaling rule for the beanstalk detects a high CPU usage and creates a new instance in AZ A, I need the address of E1 to be passed as a variable, but if a new instance is spun up in AZ B, then I need E2's address to be passed.

Is this possible in Terraform (0.12), and if so, how?

A coworker suggested this: When creating the Redis instances, register their addresses in the Parameter Store with the name having the zone identifier in it. Then, when an EC2 instance spins up, part of the initialization code will call 169.254.169.254 to get the metadata for the instance, which includes the AZ identifier. It can then grab the address for the Redis instance in that zone. Short and sweet!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM