延迟在AWS中创建启动配置

时间:2018-08-20 13:47:31

标签: terraform

使用Terraform,我定义了以下启动配置和自动缩放组资源:

resource "aws_launch_configuration" "lc_name" {
  name = "lc_name"
  image_id = "ami-035d01348bb6e6070"
  instance_type = "m3.large"
  security_groups = ["sg-61a0b51b"]
}

####################
# Autoscaling group
####################
resource "aws_autoscaling_group" "as_group_name" {

  name = "as_group_name"
  launch_configuration = "lc_name"
  vpc_zone_identifier = ["subnet-be1088f7","subnet-fa8d6fa1"]
  min_size = "1"
  max_size = "1"
  desired_capacity = "1"
  load_balancers = ["${aws_elb.elb_name.name}"]
  health_check_type = "EC2"
}

当我运行Terraform Apply时,我得到:

Error: Error applying plan:
1 error(s) occurred:

aws_autoscaling_group.as_group_name: 1 error(s) occurred:
aws_autoscaling_group.as_group_name: Error creating AutoScaling Group: ValidationError: Launch configuration name not found - A launch configuration with the name: lc_name does not exist
status code: 400, request id: b09191d3-a47c-11e8-8198-198283743bc9

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

如果再次运行应用,一切都会顺利进行,这强烈暗示着识别新启动配置的自动缩放比例组代码中存在延迟。有没有办法调整此延迟?

更新:

根据建议,我添加了一个依赖项:

resource "aws_launch_configuration" "myLaunchConfig" {
  name = "myLaunchConfig"
  image_id = "ami-01c068891b0d9411a"
  instance_type = "m3.large"
  security_groups = ["sg-61a0b51b"]
}

resource "aws_autoscaling_group" "myAutoScalingGroup" {

  name = "myAutoScalingGroup"
  launch_configuration = "myLaunchConfig"
  depends_on = ["myLaunchConfig"]
  vpc_zone_identifier = ["subnet-be1088f7","subnet-fa8d6fa1"]
  min_size = "1"
  max_size = "1"
  desired_capacity = "1"
  load_balancers = ["${aws_elb.myLoadBalancer.name}"]
  health_check_type = "EC2"
}

由于相同的原因仍然出现错误,尽管看起来有些不同:

Error: aws_autoscaling_group.myAutoScalingGroup: resource depends on non-existent resource 'myLaunchConfig'

1 个答案:

答案 0 :(得分:2)

据Terraform所知,自动缩放组与启动配置之间没有关系,因此它将尝试并行创建它们,从而导致您观察到的竞争状况在下一个{{1} }。

有了Terraform,您有了two different ways of ordering a dependency chain between resources

您可以使用显式的depends_on语法来强制资源等待,直到创建另一个资源为止。

在您的情况下,这类似于:

apply

或者,通常最好是在可能的情况下,如果您从一个资源内插一个值,那么它将自动等待创建该资源后再创建第二个资源。

在您的情况下,您将使用以下内容:

resource "aws_launch_configuration" "lc_name" {
  name            = "lc_name"
  image_id        = "ami-035d01348bb6e6070"
  instance_type   = "m3.large"
  security_groups = ["sg-61a0b51b"]
}

####################
# Autoscaling group
####################
resource "aws_autoscaling_group" "as_group_name" {
  name                 = "as_group_name"
  launch_configuration = "lc_name"
  vpc_zone_identifier  = ["subnet-be1088f7", "subnet-fa8d6fa1"]
  min_size             = "1"
  max_size             = "1"
  desired_capacity     = "1"
  load_balancers       = ["${aws_elb.elb_name.name}"]
  health_check_type    = "EC2"
  depends_on           = ["aws_launch_configuration.lc_name"]
}

如果您不确定Terraform的操作顺序,那么您可能需要看看terraform graph command

相关问题