Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 在terraform中创建多个负载平衡器、目标组和侦听器_Amazon Web Services_Powershell_Terraform - Fatal编程技术网

Amazon web services 在terraform中创建多个负载平衡器、目标组和侦听器

Amazon web services 在terraform中创建多个负载平衡器、目标组和侦听器,amazon-web-services,powershell,terraform,Amazon Web Services,Powershell,Terraform,我正在尝试创建一个负载平衡器,目标组连接到ALB,侦听器和实例连接到目标组 我已经让它为一个负载平衡器工作,但我无法让它为多个ALB工作,而不必复制代码 我还尝试在命令行中传递输入变量,并在powershell中添加脚本,但每次创建资源时,状态文件都会用下一个资源名重写 是否有一种方法可以使用新资源附加到现有状态文件,或者有一种方法可以使用所有其他关联资源创建多个ALB,而无需重复 下面是代码: variable` "name" {} variable "environment" { de

我正在尝试创建一个负载平衡器,目标组连接到ALB,侦听器和实例连接到目标组

  • 我已经让它为一个负载平衡器工作,但我无法让它为多个ALB工作,而不必复制代码

  • 我还尝试在命令行中传递输入变量,并在powershell中添加脚本,但每次创建资源时,状态文件都会用下一个资源名重写

  • 是否有一种方法可以使用新资源附加到现有状态文件,或者有一种方法可以使用所有其他关联资源创建多个ALB,而无需重复

    下面是代码:

    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
    }
    

    首先,让我解释一下您的ALB被覆盖的原因:

    Terraform是一种声明性的,即它使环境与文件中的外观完全一致。因此,如果您使用名称ALB1和一些配置创建ALB,请运行Terraform,然后将文件中的名称更改为ALB2,调用Terraform apply,Terraform将删除第一个ALB(因为您需要新资源来重命名ALB)并创建一个新的ALB

    使用地形模块可以轻松实现您想要的功能。您可以执行以下操作:

  • 将所有信息以及变量(可能需要更多变量)导出到模块中。模块只是一个文件夹,您可以使用它,例如,Main.tfvars.tfoutput.tf
  • 然后,从另一个Terraform文件中,您将使用您想要的每个负载平衡器的适当值调用您的模块几次
  • 查看有关模块的更多信息


    另外,如果您发现自己陷入了困境,请发表评论,我们会解决它。

    谢谢您的帮助。我们有一个独特的情况,我们创建多个ALB(其中20个)和相关资源,如目标组、侦听器和目标id关联。您提到的方法的问题是,它不允许我同时循环和关联所有资源,或者至少我遗漏了一些东西。我能够为alb名称创建一个输入变量,并获取与alb关联的其余资源。并使用powershell循环多个ALB。你说的“使用powershell循环”是什么意思?您是通过循环生成代码还是应用terraformy的循环?