From cf3d234f5c56cf17bd25671eea5d1a28a67788ed Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Tue, 28 Feb 2017 22:08:22 +0000 Subject: [PATCH] provider/aws: Refresh aws_autoscaling_schedule from state when autoscaling_group (#12312) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit not found Fixes: #12279 When manually deleting an autoscaling_group from the console, a terraform plan would look as follows: ``` % terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. aws_launch_configuration.foobar: Refreshing state... (ID: test-0096cf26c7eebdc9fcb5bd1837) aws_autoscaling_group.foobar: Refreshing state... (ID: test) aws_autoscaling_schedule.foobar: Refreshing state... (ID: foobar) Error refreshing state: 1 error(s) occurred: * aws_autoscaling_schedule.foobar: aws_autoscaling_schedule.foobar: Error retrieving Autoscaling Scheduled Actions: ValidationError: Group test not found status code: 400, request id: 093e9ed5-fe01-11e6-b990-1f64334b3a10 ``` After this patch: ``` % terraform plan ✹ ✭ [WARN] /Users/stacko/Code/go/bin/terraform-provider-aws overrides an internal plugin for aws-provider. If you did not expect to see this message you will need to remove the old plugin. See https://www.terraform.io/docs/internals/internal-plugins.html Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. aws_launch_configuration.foobar: Refreshing state... (ID: test-0096cf26c7eebdc9fcb5bd1837) aws_autoscaling_group.foobar: Refreshing state... (ID: test) aws_autoscaling_schedule.foobar: Refreshing state... (ID: foobar) The Terraform execution plan has been generated and is shown below. Resources are shown in alphabetical order for quick scanning. Green resources will be created (or destroyed and then created if an existing resource exists), yellow resources are being changed in-place, and red resources will be destroyed. Cyan entries are data sources to be read. Note: You didn't specify an "-out" parameter to save this plan, so when "apply" is called, Terraform can't guarantee this is what will execute. + aws_autoscaling_group.foobar arn: "" availability_zones.#: "1" availability_zones.2487133097: "us-west-2a" default_cooldown: "" desired_capacity: "" force_delete: "true" health_check_grace_period: "300" health_check_type: "ELB" launch_configuration: "test-0096cf26c7eebdc9fcb5bd1837" load_balancers.#: "" max_size: "1" metrics_granularity: "1Minute" min_size: "1" name: "test" protect_from_scale_in: "false" tag.#: "1" tag.157008572.key: "Foo" tag.157008572.propagate_at_launch: "true" tag.157008572.value: "foo-bar" termination_policies.#: "1" termination_policies.0: "OldestInstance" vpc_zone_identifier.#: "" wait_for_capacity_timeout: "10m" + aws_autoscaling_schedule.foobar arn: "" autoscaling_group_name: "test" desired_capacity: "0" end_time: "2017-12-12T06:00:00Z" max_size: "1" min_size: "0" recurrence: "" scheduled_action_name: "foobar" start_time: "2017-12-11T18:00:00Z" Plan: 2 to add, 0 to change, 0 to destroy. ``` --- .../providers/aws/resource_aws_autoscaling_schedule.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/builtin/providers/aws/resource_aws_autoscaling_schedule.go b/builtin/providers/aws/resource_aws_autoscaling_schedule.go index 5a6a89291..5cfa1c729 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_schedule.go +++ b/builtin/providers/aws/resource_aws_autoscaling_schedule.go @@ -6,6 +6,7 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/autoscaling" "github.com/hashicorp/terraform/helper/schema" ) @@ -170,6 +171,13 @@ func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interfac log.Printf("[INFO] Describing Autoscaling Scheduled Action: %+v", params) actions, err := autoscalingconn.DescribeScheduledActions(params) if err != nil { + //A ValidationError here can mean that either the Schedule is missing OR the Autoscaling Group is missing + if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "ValidationError" { + log.Printf("[WARNING] %s not found, removing from state", d.Id()) + d.SetId("") + + return nil, nil, false + } return nil, fmt.Errorf("Error retrieving Autoscaling Scheduled Actions: %s", err), false }