9625

terraform: create list based on resource count

<h3>Question</h3>

We have a bunch of instances (I know... cattle, not pets, but in this case, these are really pets)

resource "aws_instance" "read_00" { count = "${var.read_00_count}" resource "aws_instance" "read_01" { count = "${var.read_01_count}"

And we have an ELB where we want to dynamically add the instances based on their count variable, like so:

resource "aws_elb" "read_slaves" { instances = ["${aws_instance.read_.*.id}"]

But that doesn't work, of course.

Is it possible to dynamically create a list of instance ids ONLY if their count is not zero?

I know this goes against the grain, but if this is possible, that would be awesome.


<h3>Answer1:</h3>

With Terraform 0.12 that will be much easier, but for now something like this would do:

[...] resource "aws_instance" "read_01" { [...] count = "${var.read_01_count}" tags { Role = "read_slave" } } data "aws_instances" "read-slaves" { instance_tags { Role = "read_slave" } // optional filters } resource "aws_elb" "read_slaves" { instances = ["${data.aws_instances.read-slaves.ids}"] listener { ... } }

Thus:

<ul><li>tagging each instance which acts as a read slave</li> <li>collect the list of aws_intances</li> <li>create the aws_elb based on the collected data</li> </ul>

来源:https://stackoverflow.com/questions/53280855/terraform-create-list-based-on-resource-count

Recommend

  • Possible steps to improve SQL Server query performance
  • Plotting multiple variables from same data frame in ggplot [duplicate]
  • Using Facebook Graph API with ASP.NET
  • Highcharts Drawing a line with chart.renderer.path
  • Move UIView per UIBezierPath [closed]
  • Can't perform CORS request using Angularjs
  • Creating and managing two independent random number sequences
  • DocuSign API Replace template document but keep fields
  • Can I switch the 'connected' user within an sql script that is sourced by mysql?
  • TypeError: Cannot compare type 'Timestamp' with type 'date'
  • How to add ng-app after page load?
  • Complex multiple if statements
  • Jekyll - How do I create pages in the root directory?
  • Battery life if using GPS and background app ios/android
  • Use awk to convert GPS Position to Latitude & Longitude
  • Unable to connect to Azure MySQL Database through Azure Function - C#
  • Run git bash script in Windows 7/64bit
  • Wicket - getting body of markup element
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • Create .java file and compile it to a .class file at runtime
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Thumbnails for mxml components in Flex
  • Regex not working in java 1.5
  • Possible to set default CloudKit container not based on application name?
  • PHPMailer return to AJAX
  • How to make 100% div height between header and footer?
  • Conflicting declaration using constexpr and auto in C++11
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to warp text around image in iOS?
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • XSLT Transformation to validate rules in XML document