Delay in creation of launch config in AWS

2019-08-20 03:27发布

问题:

Using Terraform, I have the following launch config and autoscale group resources defined:

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"
}

When I run terraform apply, I get:

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.

If I run apply again, all goes well, strongly implying that there is a delay in the create autoscale group code recognizing a new launch configuration. Is there a way to adjust to this delay?

Update:

Per suggestion, I added a dependency:

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"
}

Still getting an error for the same reason, though it looks a bit different:

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

回答1:

As far as Terraform can tell there is no relationship between your autoscaling group and your launch configuration so it is going to try to create these in parallel, leading you to the observed race condition that corrects itself on the next apply.

With Terraform you have two different ways of ordering a dependency chain between resources.

You can use the explicit depends_on syntax to force a resource to wait until another resource is created before it, in turn, is created.

In your case this would be something like:

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"]
}

Or, and this is generally preferable where possible, if you interpolate a value from one resource then it will automatically wait until that resource is created before creating the second resource.

In your case you would then use something like this:

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 = "${aws_launch_configuration.lc_name.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"
}

If you are ever unsure as to the order of things that Terraform will operate on then you might want to take a look at the terraform graph command.



标签: terraform