From 3d335e48ffe301b826245dee1242997cbfba6869 Mon Sep 17 00:00:00 2001 From: Matt Dainty Date: Tue, 7 Mar 2017 14:20:01 +0000 Subject: [PATCH] Check instance is running before trying to attach (#12459) This covers the scenario of an instance created by a spot request. Using Terraform we only know the spot request is fulfilled but the instance can still be pending which causes the attachment to fail. --- .../aws/resource_aws_volume_attachment.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/builtin/providers/aws/resource_aws_volume_attachment.go b/builtin/providers/aws/resource_aws_volume_attachment.go index b469417d4..9aed74a42 100644 --- a/builtin/providers/aws/resource_aws_volume_attachment.go +++ b/builtin/providers/aws/resource_aws_volume_attachment.go @@ -77,6 +77,25 @@ func resourceAwsVolumeAttachmentCreate(d *schema.ResourceData, meta interface{}) vols, err := conn.DescribeVolumes(request) if (err != nil) || (len(vols.Volumes) == 0) { + // This handles the situation where the instance is created by + // a spot request and whilst the request has been fulfilled the + // instance is not running yet + stateConf := &resource.StateChangeConf{ + Pending: []string{"pending"}, + Target: []string{"running"}, + Refresh: InstanceStateRefreshFunc(conn, iID), + Timeout: 10 * time.Minute, + Delay: 10 * time.Second, + MinTimeout: 3 * time.Second, + } + + _, err = stateConf.WaitForState() + if err != nil { + return fmt.Errorf( + "Error waiting for instance (%s) to become ready: %s", + iID, err) + } + // not attached opts := &ec2.AttachVolumeInput{ Device: aws.String(name),