Create multiple resources terraformTerraform forcing new resources on every applyWhy Terraform is updating volume attachment resources?How to create temporary one-off terraform resources?can I use same state file in terraform for multiple terraform filesDeploying to multiple accounts with Terraform?Terraform multiple environments on awsTerraform - how to use multiple modules to create temporary ressourcesCreate TPU instance using TerraformUnable to create Kubernetes resources with terraform

How can I find where certain bash function is defined?

Why are C64 games inconsistent with which joystick port they use?

Does linking adjectives allow you to talk about multiple variations of something?

Looking for a soft substance that doesn't dissolve underwater

Does revoking a certificate result in revocation of its key?

Is CD audio quality good enough for the final delivery of music?

Rename photos to match video titles

What are these arcade games in Ghostbusters 1984?

Identify this in soil?

Can a wire having 610-670 THz (frequency of blue light) A.C frequency supply, generate blue light?

Why do airplanes use an axial flow jet engine instead of a more compact centrifugal jet engine?

What is the most important source of natural gas? coal, oil or other?

What is the object moving across the ceiling in this stock footage?

How many chess players are over 2500 Elo?

How can I get exact maximal value of this expression?

Would jet fuel for an F-16 or F-35 be producible during WW2?

Canon 70D often overexposing or underexposing shots

Why does the 'metric Lagrangian' approach appear to fail in Newtonian mechanics?

What does the view outside my ship traveling at light speed look like?

Python program to convert a 24 hour format to 12 hour format

analysis of BJT PNP type - why they can use voltage divider?

Is there a way to make it so the cursor is included when I prtscr key?

Is there a public standard for 8 and 10 character grid locators?

How can people dance around bonfires on Lag Lo'Omer - it's darchei emori?



Create multiple resources terraform


Terraform forcing new resources on every applyWhy Terraform is updating volume attachment resources?How to create temporary one-off terraform resources?can I use same state file in terraform for multiple terraform filesDeploying to multiple accounts with Terraform?Terraform multiple environments on awsTerraform - how to use multiple modules to create temporary ressourcesCreate TPU instance using TerraformUnable to create Kubernetes resources with terraform






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















First time asking a question so if I've left anything important out let me know!



Terraform v0.11.1
provider.aws v0.1.4



I'm trying to create multiple cloudwatch alarms for multiple instances. I was wondering if there was a way to have the config iterating though a list. I have tried using the following code, but I get "Error: variable "instances": default may not contain interpolations".



variable "instances" 
description = "Run the EC2 Instances in these Availability Zones"
type = "list"
default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


############ Cloudwatch monitoring
resource "aws_cloudwatch_metric_alarm" "cpu"
count = 5
alarm_name = "terraform-cpu-high-test"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "180"
statistic = "Average"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
insufficient_data_actions = []
dimensions
InstanceId = "$element(var.instances, count.index)"




Is something like this even possible with terraform and if so what's the best way to do it?



Thanks!










share|improve this question




























    1















    First time asking a question so if I've left anything important out let me know!



    Terraform v0.11.1
    provider.aws v0.1.4



    I'm trying to create multiple cloudwatch alarms for multiple instances. I was wondering if there was a way to have the config iterating though a list. I have tried using the following code, but I get "Error: variable "instances": default may not contain interpolations".



    variable "instances" 
    description = "Run the EC2 Instances in these Availability Zones"
    type = "list"
    default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


    ############ Cloudwatch monitoring
    resource "aws_cloudwatch_metric_alarm" "cpu"
    count = 5
    alarm_name = "terraform-cpu-high-test"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "CPUUtilization"
    namespace = "AWS/EC2"
    period = "180"
    statistic = "Average"
    threshold = "80"
    alarm_description = "This metric monitors ec2 cpu utilization"
    insufficient_data_actions = []
    dimensions
    InstanceId = "$element(var.instances, count.index)"




    Is something like this even possible with terraform and if so what's the best way to do it?



    Thanks!










    share|improve this question
























      1












      1








      1








      First time asking a question so if I've left anything important out let me know!



      Terraform v0.11.1
      provider.aws v0.1.4



      I'm trying to create multiple cloudwatch alarms for multiple instances. I was wondering if there was a way to have the config iterating though a list. I have tried using the following code, but I get "Error: variable "instances": default may not contain interpolations".



      variable "instances" 
      description = "Run the EC2 Instances in these Availability Zones"
      type = "list"
      default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


      ############ Cloudwatch monitoring
      resource "aws_cloudwatch_metric_alarm" "cpu"
      count = 5
      alarm_name = "terraform-cpu-high-test"
      comparison_operator = "GreaterThanOrEqualToThreshold"
      evaluation_periods = "2"
      metric_name = "CPUUtilization"
      namespace = "AWS/EC2"
      period = "180"
      statistic = "Average"
      threshold = "80"
      alarm_description = "This metric monitors ec2 cpu utilization"
      insufficient_data_actions = []
      dimensions
      InstanceId = "$element(var.instances, count.index)"




      Is something like this even possible with terraform and if so what's the best way to do it?



      Thanks!










      share|improve this question














      First time asking a question so if I've left anything important out let me know!



      Terraform v0.11.1
      provider.aws v0.1.4



      I'm trying to create multiple cloudwatch alarms for multiple instances. I was wondering if there was a way to have the config iterating though a list. I have tried using the following code, but I get "Error: variable "instances": default may not contain interpolations".



      variable "instances" 
      description = "Run the EC2 Instances in these Availability Zones"
      type = "list"
      default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


      ############ Cloudwatch monitoring
      resource "aws_cloudwatch_metric_alarm" "cpu"
      count = 5
      alarm_name = "terraform-cpu-high-test"
      comparison_operator = "GreaterThanOrEqualToThreshold"
      evaluation_periods = "2"
      metric_name = "CPUUtilization"
      namespace = "AWS/EC2"
      period = "180"
      statistic = "Average"
      threshold = "80"
      alarm_description = "This metric monitors ec2 cpu utilization"
      insufficient_data_actions = []
      dimensions
      InstanceId = "$element(var.instances, count.index)"




      Is something like this even possible with terraform and if so what's the best way to do it?



      Thanks!







      terraform






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 11 '17 at 17:49









      Matthew SlaterMatthew Slater

      83




      83




















          2 Answers
          2






          active

          oldest

          votes


















          2














          Your issue is



          default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


          That either has to be passed in or set statically.



          If my_instance1-5 are the same and can be set with a count in them like above. Then its better to do



          InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"





          share|improve this answer























          • Thanks Mike, that’s a shame. So every time I destroy and recreate servers I would have to manually update those values?

            – Matthew Slater
            Dec 12 '17 at 7:44



















          0














          It would help if you show us the code on how you're creating the EC2 instances. If they are created with a count then this should be easier. If they are created in the same template as this Cloudwatch code, then again even easier.



          If it's not in the same template, then you will need to use outputs. https://www.terraform.io/docs/configuration/outputs.html
          This allows a module to output information for other modules to use.
          To gain information from an output, you need a data source that will allow you to look inside the state file and abstract the EC2 instance ID's. This helps if you're using a remote backend, such as S3.



          If it's in the same module and it's using a count to build the instances, then as Mike said, you can use



          InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"





          share|improve this answer























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "2"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f887563%2fcreate-multiple-resources-terraform%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Your issue is



            default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


            That either has to be passed in or set statically.



            If my_instance1-5 are the same and can be set with a count in them like above. Then its better to do



            InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"





            share|improve this answer























            • Thanks Mike, that’s a shame. So every time I destroy and recreate servers I would have to manually update those values?

              – Matthew Slater
              Dec 12 '17 at 7:44
















            2














            Your issue is



            default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


            That either has to be passed in or set statically.



            If my_instance1-5 are the same and can be set with a count in them like above. Then its better to do



            InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"





            share|improve this answer























            • Thanks Mike, that’s a shame. So every time I destroy and recreate servers I would have to manually update those values?

              – Matthew Slater
              Dec 12 '17 at 7:44














            2












            2








            2







            Your issue is



            default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


            That either has to be passed in or set statically.



            If my_instance1-5 are the same and can be set with a count in them like above. Then its better to do



            InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"





            share|improve this answer













            Your issue is



            default = ["$aws_instance.my_instance1.id", "$aws_instance.my_instance2.id", "$aws_instance.my_instance3.id", "$aws_instance.my_instance4.id", "$aws_instance.my_instance5.id"]


            That either has to be passed in or set statically.



            If my_instance1-5 are the same and can be set with a count in them like above. Then its better to do



            InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 11 '17 at 21:15









            MikeMike

            18.9k44270




            18.9k44270












            • Thanks Mike, that’s a shame. So every time I destroy and recreate servers I would have to manually update those values?

              – Matthew Slater
              Dec 12 '17 at 7:44


















            • Thanks Mike, that’s a shame. So every time I destroy and recreate servers I would have to manually update those values?

              – Matthew Slater
              Dec 12 '17 at 7:44

















            Thanks Mike, that’s a shame. So every time I destroy and recreate servers I would have to manually update those values?

            – Matthew Slater
            Dec 12 '17 at 7:44






            Thanks Mike, that’s a shame. So every time I destroy and recreate servers I would have to manually update those values?

            – Matthew Slater
            Dec 12 '17 at 7:44














            0














            It would help if you show us the code on how you're creating the EC2 instances. If they are created with a count then this should be easier. If they are created in the same template as this Cloudwatch code, then again even easier.



            If it's not in the same template, then you will need to use outputs. https://www.terraform.io/docs/configuration/outputs.html
            This allows a module to output information for other modules to use.
            To gain information from an output, you need a data source that will allow you to look inside the state file and abstract the EC2 instance ID's. This helps if you're using a remote backend, such as S3.



            If it's in the same module and it's using a count to build the instances, then as Mike said, you can use



            InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"





            share|improve this answer



























              0














              It would help if you show us the code on how you're creating the EC2 instances. If they are created with a count then this should be easier. If they are created in the same template as this Cloudwatch code, then again even easier.



              If it's not in the same template, then you will need to use outputs. https://www.terraform.io/docs/configuration/outputs.html
              This allows a module to output information for other modules to use.
              To gain information from an output, you need a data source that will allow you to look inside the state file and abstract the EC2 instance ID's. This helps if you're using a remote backend, such as S3.



              If it's in the same module and it's using a count to build the instances, then as Mike said, you can use



              InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"





              share|improve this answer

























                0












                0








                0







                It would help if you show us the code on how you're creating the EC2 instances. If they are created with a count then this should be easier. If they are created in the same template as this Cloudwatch code, then again even easier.



                If it's not in the same template, then you will need to use outputs. https://www.terraform.io/docs/configuration/outputs.html
                This allows a module to output information for other modules to use.
                To gain information from an output, you need a data source that will allow you to look inside the state file and abstract the EC2 instance ID's. This helps if you're using a remote backend, such as S3.



                If it's in the same module and it's using a count to build the instances, then as Mike said, you can use



                InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"





                share|improve this answer













                It would help if you show us the code on how you're creating the EC2 instances. If they are created with a count then this should be easier. If they are created in the same template as this Cloudwatch code, then again even easier.



                If it's not in the same template, then you will need to use outputs. https://www.terraform.io/docs/configuration/outputs.html
                This allows a module to output information for other modules to use.
                To gain information from an output, you need a data source that will allow you to look inside the state file and abstract the EC2 instance ID's. This helps if you're using a remote backend, such as S3.



                If it's in the same module and it's using a count to build the instances, then as Mike said, you can use



                InstanceId = "$element(aws_instance.my_instance.*.id, count.index)"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 14 at 13:21









                Steve TownsendSteve Townsend

                1




                1



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Server Fault!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f887563%2fcreate-multiple-resources-terraform%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

                    What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

                    Esgonzo ibérico Índice Descrición Distribución Hábitat Ameazas Notas Véxase tamén "Acerca dos nomes dos anfibios e réptiles galegos""Chalcides bedriagai"Chalcides bedriagai en Carrascal, L. M. Salvador, A. (Eds). Enciclopedia virtual de los vertebrados españoles. Museo Nacional de Ciencias Naturales, Madrid. España.Fotos