creating mutilple load balancers ,target groups and listeners in terraform












0
















  • I am trying to create a loadbalancer with a target group attached to the ALB and listeners and instances attached to the target groups.


  • I have gotten it working for a single load balancer , but i cannot get it working for multiple ALB's with out having to duplicate the code.


  • I also tried passing input variables in command line appending it with scripting in powershell , but everytime the resources are created ,the state file is over written with the next resource name .



Is there a way to append to existing state file with new resources or a way to create multiple albs with all the other associated resources without duplicating ?



Below is the code:



variable` "name" {}

variable "environment" {

default = "Beta"

}

variable "security_group_id" {

default = ["sg-xxxxxx"]

}

variable "subnet_ids" {

type = "list"
default = ["subnet-xxxxxx","subnet-xxxxxxx","subnet-xxxxxxxxxxx"]
}

variable "instance_ids" {

type = "list"
default = ["xxxxxxx","xxxxxxx"]
}


variable "vpc_id" {

default = "vpc-xxxxxxxxxxxx"

}

variable "ssl_certificate_arn" {

default = "vpc-xxxxxxxxxxx"

}


provider "aws" {
region = "us-west-2"
access_key = "xxxxxxxxxx"
secret_key = "xxxxxxxxxx"

}
resource "aws_alb" "alb" {
count = "1"
name = "${var.name}-${var.environment}"
internal = false
security_groups = ["${var.security_group_id}"]
subnets = ["${var.subnet_ids}"]



tags {
Environment = "${var.environment}"
}

}

resource "aws_alb_target_group" "alb_targets" {
count = "1"
name = "${var.name}-${var.environment}"
port = "80"
protocol = "HTTP"
vpc_id = "${var.vpc_id}"
health_check {
healthy_threshold = 2
interval = 15
path = "/api/health"
timeout = 10
unhealthy_threshold = 2
}



tags {

Environment = "${var.environment}"
}
}
resource "aws_alb_listener" "alb_listener" {
count = "1"
load_balancer_arn = "${aws_alb.alb.arn}"
port = "80"
protocol = "HTTP"
#ssl_policy = "ELBSecurityPolicy-2015-05"
#certificate_arn = "${var.ssl_certificate_arn}"
default_action {
target_group_arn = "${element(aws_alb_target_group.alb_targets.*.arn, 0)}"
type = "forward"
}
}



resource "aws_lb_target_group_attachment" "test" {
target_group_arn = "${aws_alb_target_group.alb_targets.arn}"
target_id = "${element(var.instance_ids,count.index)}"
port = 80
}









share|improve this question





























    0
















    • I am trying to create a loadbalancer with a target group attached to the ALB and listeners and instances attached to the target groups.


    • I have gotten it working for a single load balancer , but i cannot get it working for multiple ALB's with out having to duplicate the code.


    • I also tried passing input variables in command line appending it with scripting in powershell , but everytime the resources are created ,the state file is over written with the next resource name .



    Is there a way to append to existing state file with new resources or a way to create multiple albs with all the other associated resources without duplicating ?



    Below is the code:



    variable` "name" {}

    variable "environment" {

    default = "Beta"

    }

    variable "security_group_id" {

    default = ["sg-xxxxxx"]

    }

    variable "subnet_ids" {

    type = "list"
    default = ["subnet-xxxxxx","subnet-xxxxxxx","subnet-xxxxxxxxxxx"]
    }

    variable "instance_ids" {

    type = "list"
    default = ["xxxxxxx","xxxxxxx"]
    }


    variable "vpc_id" {

    default = "vpc-xxxxxxxxxxxx"

    }

    variable "ssl_certificate_arn" {

    default = "vpc-xxxxxxxxxxx"

    }


    provider "aws" {
    region = "us-west-2"
    access_key = "xxxxxxxxxx"
    secret_key = "xxxxxxxxxx"

    }
    resource "aws_alb" "alb" {
    count = "1"
    name = "${var.name}-${var.environment}"
    internal = false
    security_groups = ["${var.security_group_id}"]
    subnets = ["${var.subnet_ids}"]



    tags {
    Environment = "${var.environment}"
    }

    }

    resource "aws_alb_target_group" "alb_targets" {
    count = "1"
    name = "${var.name}-${var.environment}"
    port = "80"
    protocol = "HTTP"
    vpc_id = "${var.vpc_id}"
    health_check {
    healthy_threshold = 2
    interval = 15
    path = "/api/health"
    timeout = 10
    unhealthy_threshold = 2
    }



    tags {

    Environment = "${var.environment}"
    }
    }
    resource "aws_alb_listener" "alb_listener" {
    count = "1"
    load_balancer_arn = "${aws_alb.alb.arn}"
    port = "80"
    protocol = "HTTP"
    #ssl_policy = "ELBSecurityPolicy-2015-05"
    #certificate_arn = "${var.ssl_certificate_arn}"
    default_action {
    target_group_arn = "${element(aws_alb_target_group.alb_targets.*.arn, 0)}"
    type = "forward"
    }
    }



    resource "aws_lb_target_group_attachment" "test" {
    target_group_arn = "${aws_alb_target_group.alb_targets.arn}"
    target_id = "${element(var.instance_ids,count.index)}"
    port = 80
    }









    share|improve this question



























      0












      0








      0









      • I am trying to create a loadbalancer with a target group attached to the ALB and listeners and instances attached to the target groups.


      • I have gotten it working for a single load balancer , but i cannot get it working for multiple ALB's with out having to duplicate the code.


      • I also tried passing input variables in command line appending it with scripting in powershell , but everytime the resources are created ,the state file is over written with the next resource name .



      Is there a way to append to existing state file with new resources or a way to create multiple albs with all the other associated resources without duplicating ?



      Below is the code:



      variable` "name" {}

      variable "environment" {

      default = "Beta"

      }

      variable "security_group_id" {

      default = ["sg-xxxxxx"]

      }

      variable "subnet_ids" {

      type = "list"
      default = ["subnet-xxxxxx","subnet-xxxxxxx","subnet-xxxxxxxxxxx"]
      }

      variable "instance_ids" {

      type = "list"
      default = ["xxxxxxx","xxxxxxx"]
      }


      variable "vpc_id" {

      default = "vpc-xxxxxxxxxxxx"

      }

      variable "ssl_certificate_arn" {

      default = "vpc-xxxxxxxxxxx"

      }


      provider "aws" {
      region = "us-west-2"
      access_key = "xxxxxxxxxx"
      secret_key = "xxxxxxxxxx"

      }
      resource "aws_alb" "alb" {
      count = "1"
      name = "${var.name}-${var.environment}"
      internal = false
      security_groups = ["${var.security_group_id}"]
      subnets = ["${var.subnet_ids}"]



      tags {
      Environment = "${var.environment}"
      }

      }

      resource "aws_alb_target_group" "alb_targets" {
      count = "1"
      name = "${var.name}-${var.environment}"
      port = "80"
      protocol = "HTTP"
      vpc_id = "${var.vpc_id}"
      health_check {
      healthy_threshold = 2
      interval = 15
      path = "/api/health"
      timeout = 10
      unhealthy_threshold = 2
      }



      tags {

      Environment = "${var.environment}"
      }
      }
      resource "aws_alb_listener" "alb_listener" {
      count = "1"
      load_balancer_arn = "${aws_alb.alb.arn}"
      port = "80"
      protocol = "HTTP"
      #ssl_policy = "ELBSecurityPolicy-2015-05"
      #certificate_arn = "${var.ssl_certificate_arn}"
      default_action {
      target_group_arn = "${element(aws_alb_target_group.alb_targets.*.arn, 0)}"
      type = "forward"
      }
      }



      resource "aws_lb_target_group_attachment" "test" {
      target_group_arn = "${aws_alb_target_group.alb_targets.arn}"
      target_id = "${element(var.instance_ids,count.index)}"
      port = 80
      }









      share|improve this question

















      • I am trying to create a loadbalancer with a target group attached to the ALB and listeners and instances attached to the target groups.


      • I have gotten it working for a single load balancer , but i cannot get it working for multiple ALB's with out having to duplicate the code.


      • I also tried passing input variables in command line appending it with scripting in powershell , but everytime the resources are created ,the state file is over written with the next resource name .



      Is there a way to append to existing state file with new resources or a way to create multiple albs with all the other associated resources without duplicating ?



      Below is the code:



      variable` "name" {}

      variable "environment" {

      default = "Beta"

      }

      variable "security_group_id" {

      default = ["sg-xxxxxx"]

      }

      variable "subnet_ids" {

      type = "list"
      default = ["subnet-xxxxxx","subnet-xxxxxxx","subnet-xxxxxxxxxxx"]
      }

      variable "instance_ids" {

      type = "list"
      default = ["xxxxxxx","xxxxxxx"]
      }


      variable "vpc_id" {

      default = "vpc-xxxxxxxxxxxx"

      }

      variable "ssl_certificate_arn" {

      default = "vpc-xxxxxxxxxxx"

      }


      provider "aws" {
      region = "us-west-2"
      access_key = "xxxxxxxxxx"
      secret_key = "xxxxxxxxxx"

      }
      resource "aws_alb" "alb" {
      count = "1"
      name = "${var.name}-${var.environment}"
      internal = false
      security_groups = ["${var.security_group_id}"]
      subnets = ["${var.subnet_ids}"]



      tags {
      Environment = "${var.environment}"
      }

      }

      resource "aws_alb_target_group" "alb_targets" {
      count = "1"
      name = "${var.name}-${var.environment}"
      port = "80"
      protocol = "HTTP"
      vpc_id = "${var.vpc_id}"
      health_check {
      healthy_threshold = 2
      interval = 15
      path = "/api/health"
      timeout = 10
      unhealthy_threshold = 2
      }



      tags {

      Environment = "${var.environment}"
      }
      }
      resource "aws_alb_listener" "alb_listener" {
      count = "1"
      load_balancer_arn = "${aws_alb.alb.arn}"
      port = "80"
      protocol = "HTTP"
      #ssl_policy = "ELBSecurityPolicy-2015-05"
      #certificate_arn = "${var.ssl_certificate_arn}"
      default_action {
      target_group_arn = "${element(aws_alb_target_group.alb_targets.*.arn, 0)}"
      type = "forward"
      }
      }



      resource "aws_lb_target_group_attachment" "test" {
      target_group_arn = "${aws_alb_target_group.alb_targets.arn}"
      target_id = "${element(var.instance_ids,count.index)}"
      port = 80
      }






      amazon-web-services powershell terraform






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 9:51









      atline

      3,159102138




      3,159102138










      asked Nov 16 '18 at 8:17









      Jay PatJay Pat

      205




      205
























          1 Answer
          1






          active

          oldest

          votes


















          1














          First, let me explain why your ALBs are getting overwritten:



          Terraform is a declarative, i.e. it makes the Environment exactly what it looks in the file. So if you create ALB with name ALB1 and some configuration, run Terraform, then change the name in the file to ALB2, call Terraform apply, Terraform will delete the first one (since you need new resource to rename an ALB) and create a new one.



          What you want can be easily achieved using Terraform Modules. What you can do is the following:




          1. Export all your information, along with the variables (you might need some more variables) into a Module. A module is just a folder where you have, e.g. Main.tf , vars.tf, output.tf

          2. Then from another Terraform file, you would call your module a couple of times with the appropriate values for every load balancer you want.


          Check this for more information on modules.



          P.S. if you find yourself stuck with this, post a comment and we will solve it.






          share|improve this answer
























          • thanks for the help . we have a unique situation where we create multiple albs ( 20 of them ) and associated resources such as target groups, listeners and target id association. The problem with the approach you mentioned is that it does not allow me to loop and associate all the resources simultaneously or atleast i am missing something. I was able to create an input variable for the alb name and get the rest of the resources associated with the alb. and loop thorugh multiple albs using powershell.

            – Jay Pat
            Nov 30 '18 at 6:20











          • What do you mean by "looping using powershell"? Do you just generate your code by looping or you make a loop of terraformy apply ?

            – AlexK
            Nov 30 '18 at 8:44











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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%2fstackoverflow.com%2fquestions%2f53333888%2fcreating-mutilple-load-balancers-target-groups-and-listeners-in-terraform%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          First, let me explain why your ALBs are getting overwritten:



          Terraform is a declarative, i.e. it makes the Environment exactly what it looks in the file. So if you create ALB with name ALB1 and some configuration, run Terraform, then change the name in the file to ALB2, call Terraform apply, Terraform will delete the first one (since you need new resource to rename an ALB) and create a new one.



          What you want can be easily achieved using Terraform Modules. What you can do is the following:




          1. Export all your information, along with the variables (you might need some more variables) into a Module. A module is just a folder where you have, e.g. Main.tf , vars.tf, output.tf

          2. Then from another Terraform file, you would call your module a couple of times with the appropriate values for every load balancer you want.


          Check this for more information on modules.



          P.S. if you find yourself stuck with this, post a comment and we will solve it.






          share|improve this answer
























          • thanks for the help . we have a unique situation where we create multiple albs ( 20 of them ) and associated resources such as target groups, listeners and target id association. The problem with the approach you mentioned is that it does not allow me to loop and associate all the resources simultaneously or atleast i am missing something. I was able to create an input variable for the alb name and get the rest of the resources associated with the alb. and loop thorugh multiple albs using powershell.

            – Jay Pat
            Nov 30 '18 at 6:20











          • What do you mean by "looping using powershell"? Do you just generate your code by looping or you make a loop of terraformy apply ?

            – AlexK
            Nov 30 '18 at 8:44
















          1














          First, let me explain why your ALBs are getting overwritten:



          Terraform is a declarative, i.e. it makes the Environment exactly what it looks in the file. So if you create ALB with name ALB1 and some configuration, run Terraform, then change the name in the file to ALB2, call Terraform apply, Terraform will delete the first one (since you need new resource to rename an ALB) and create a new one.



          What you want can be easily achieved using Terraform Modules. What you can do is the following:




          1. Export all your information, along with the variables (you might need some more variables) into a Module. A module is just a folder where you have, e.g. Main.tf , vars.tf, output.tf

          2. Then from another Terraform file, you would call your module a couple of times with the appropriate values for every load balancer you want.


          Check this for more information on modules.



          P.S. if you find yourself stuck with this, post a comment and we will solve it.






          share|improve this answer
























          • thanks for the help . we have a unique situation where we create multiple albs ( 20 of them ) and associated resources such as target groups, listeners and target id association. The problem with the approach you mentioned is that it does not allow me to loop and associate all the resources simultaneously or atleast i am missing something. I was able to create an input variable for the alb name and get the rest of the resources associated with the alb. and loop thorugh multiple albs using powershell.

            – Jay Pat
            Nov 30 '18 at 6:20











          • What do you mean by "looping using powershell"? Do you just generate your code by looping or you make a loop of terraformy apply ?

            – AlexK
            Nov 30 '18 at 8:44














          1












          1








          1







          First, let me explain why your ALBs are getting overwritten:



          Terraform is a declarative, i.e. it makes the Environment exactly what it looks in the file. So if you create ALB with name ALB1 and some configuration, run Terraform, then change the name in the file to ALB2, call Terraform apply, Terraform will delete the first one (since you need new resource to rename an ALB) and create a new one.



          What you want can be easily achieved using Terraform Modules. What you can do is the following:




          1. Export all your information, along with the variables (you might need some more variables) into a Module. A module is just a folder where you have, e.g. Main.tf , vars.tf, output.tf

          2. Then from another Terraform file, you would call your module a couple of times with the appropriate values for every load balancer you want.


          Check this for more information on modules.



          P.S. if you find yourself stuck with this, post a comment and we will solve it.






          share|improve this answer













          First, let me explain why your ALBs are getting overwritten:



          Terraform is a declarative, i.e. it makes the Environment exactly what it looks in the file. So if you create ALB with name ALB1 and some configuration, run Terraform, then change the name in the file to ALB2, call Terraform apply, Terraform will delete the first one (since you need new resource to rename an ALB) and create a new one.



          What you want can be easily achieved using Terraform Modules. What you can do is the following:




          1. Export all your information, along with the variables (you might need some more variables) into a Module. A module is just a folder where you have, e.g. Main.tf , vars.tf, output.tf

          2. Then from another Terraform file, you would call your module a couple of times with the appropriate values for every load balancer you want.


          Check this for more information on modules.



          P.S. if you find yourself stuck with this, post a comment and we will solve it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 8:41









          AlexKAlexK

          889613




          889613













          • thanks for the help . we have a unique situation where we create multiple albs ( 20 of them ) and associated resources such as target groups, listeners and target id association. The problem with the approach you mentioned is that it does not allow me to loop and associate all the resources simultaneously or atleast i am missing something. I was able to create an input variable for the alb name and get the rest of the resources associated with the alb. and loop thorugh multiple albs using powershell.

            – Jay Pat
            Nov 30 '18 at 6:20











          • What do you mean by "looping using powershell"? Do you just generate your code by looping or you make a loop of terraformy apply ?

            – AlexK
            Nov 30 '18 at 8:44



















          • thanks for the help . we have a unique situation where we create multiple albs ( 20 of them ) and associated resources such as target groups, listeners and target id association. The problem with the approach you mentioned is that it does not allow me to loop and associate all the resources simultaneously or atleast i am missing something. I was able to create an input variable for the alb name and get the rest of the resources associated with the alb. and loop thorugh multiple albs using powershell.

            – Jay Pat
            Nov 30 '18 at 6:20











          • What do you mean by "looping using powershell"? Do you just generate your code by looping or you make a loop of terraformy apply ?

            – AlexK
            Nov 30 '18 at 8:44

















          thanks for the help . we have a unique situation where we create multiple albs ( 20 of them ) and associated resources such as target groups, listeners and target id association. The problem with the approach you mentioned is that it does not allow me to loop and associate all the resources simultaneously or atleast i am missing something. I was able to create an input variable for the alb name and get the rest of the resources associated with the alb. and loop thorugh multiple albs using powershell.

          – Jay Pat
          Nov 30 '18 at 6:20





          thanks for the help . we have a unique situation where we create multiple albs ( 20 of them ) and associated resources such as target groups, listeners and target id association. The problem with the approach you mentioned is that it does not allow me to loop and associate all the resources simultaneously or atleast i am missing something. I was able to create an input variable for the alb name and get the rest of the resources associated with the alb. and loop thorugh multiple albs using powershell.

          – Jay Pat
          Nov 30 '18 at 6:20













          What do you mean by "looping using powershell"? Do you just generate your code by looping or you make a loop of terraformy apply ?

          – AlexK
          Nov 30 '18 at 8:44





          What do you mean by "looping using powershell"? Do you just generate your code by looping or you make a loop of terraformy apply ?

          – AlexK
          Nov 30 '18 at 8:44




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53333888%2fcreating-mutilple-load-balancers-target-groups-and-listeners-in-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

          Bressuire

          Vorschmack

          Quarantine