Hello, I have question related to obtaining parameters in a module I have written.
The module provisions EC2 instances based on the application component that is being requested. As part of creating the instance, I query AWS to get the right AMI for that component.
The module expects a string which defines the application component. The component defines the AMI that the instance will use. Another thing to note is that there are some components that share the same AMI.
I’ve achieved this in a couple different ways, but I’m sure there is a “better” way to do it.
So far, I’ve tried:
- 1:1 mapping of AMI to application component, but there is some repetition of code which I’d like to avoid.
- nested if statements. This works as well, however it makes the code somewhat difficult to read
What I would like to do is something like map a list of strings to an AMI which will get me right AMI if my input string matches one of the elements in the list.
The code for what I’ve tried so far is below:
locals {
image_mapping = {
"foo" = ami1.id #(obtained using data block not listed here)
"bar" = ami2.id
"foo_bar" = ami1.id
}
# what I would like to do is have a mapping like below, and query for the ami id
# ami1.id = ["foo", "foo_bar"]
# ami2.id = ["bar"]
instance_ami = local.image_mapping[var.input_string]
}
What I’m trying to do seems fairly simple, but it seems to be escaping me on the “cleanest” method.
Any assistance would be appreciated.