diff --git a/aws/resource_aws_spot_fleet_request.go b/aws/resource_aws_spot_fleet_request.go index a3b5cabe689..e2ef0036dd0 100644 --- a/aws/resource_aws_spot_fleet_request.go +++ b/aws/resource_aws_spot_fleet_request.go @@ -883,7 +883,7 @@ func resourceAwsSpotFleetRequestCreate(d *schema.ResourceData, meta interface{}) if v, ok := d.GetOk("allocation_strategy"); ok { spotFleetConfig.AllocationStrategy = aws.String(v.(string)) } else { - spotFleetConfig.AllocationStrategy = aws.String("lowestPrice") + spotFleetConfig.AllocationStrategy = aws.String(ec2.AllocationStrategyLowestPrice) } if v, ok := d.GetOk("instance_pools_to_use_count"); ok && v.(int) != 1 { @@ -1479,11 +1479,10 @@ func resourceAwsSpotFleetRequestUpdate(d *schema.ResourceData, meta interface{}) updateFlag := false if d.HasChange("target_capacity") { - if val, ok := d.GetOk("target_capacity"); ok { + if val, ok := d.GetOkExists("target_capacity"); ok { req.TargetCapacity = aws.Int64(int64(val.(int))) + updateFlag = true } - - updateFlag = true } if d.HasChange("excess_capacity_termination_policy") { @@ -1495,9 +1494,25 @@ func resourceAwsSpotFleetRequestUpdate(d *schema.ResourceData, meta interface{}) } if updateFlag { + log.Printf("[DEBUG] Modifying Spot Fleet Request: %#v", req) if _, err := conn.ModifySpotFleetRequest(req); err != nil { return fmt.Errorf("error updating spot request (%s): %s", d.Id(), err) } + + log.Println("[INFO] Waiting for Spot Fleet Request to be modified") + stateConf := &resource.StateChangeConf{ + Pending: []string{ec2.BatchStateModifying}, + Target: []string{ec2.BatchStateActive}, + Refresh: resourceAwsSpotFleetRequestStateRefreshFunc(d, meta), + Timeout: 10 * time.Minute, + MinTimeout: 10 * time.Second, + Delay: 30 * time.Second, + } + + _, err := stateConf.WaitForState() + if err != nil { + return err + } } if d.HasChange("tags") { diff --git a/aws/resource_aws_spot_fleet_request_test.go b/aws/resource_aws_spot_fleet_request_test.go index 1a76e11c8fe..1c4757356ea 100644 --- a/aws/resource_aws_spot_fleet_request_test.go +++ b/aws/resource_aws_spot_fleet_request_test.go @@ -1072,6 +1072,48 @@ func TestAccAWSSpotFleetRequest_WithTargetGroups(t *testing.T) { }) } +func TestAccAWSSpotFleetRequest_zero_capacity(t *testing.T) { + var sfr ec2.SpotFleetRequestConfig + rName := acctest.RandomWithPrefix("tf-acc-test") + validUntil := time.Now().UTC().Add(24 * time.Hour).Format(time.RFC3339) + resourceName := "aws_spot_fleet_request.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2SpotFleetRequest(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSpotFleetRequestZeroCapacityConfig(rName, validUntil), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "target_capacity", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"wait_for_fulfillment"}, + }, + { + Config: testAccAWSSpotFleetRequestConfig(rName, validUntil), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "target_capacity", "2"), + ), + }, + { + Config: testAccAWSSpotFleetRequestZeroCapacityConfig(rName, validUntil), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSSpotFleetRequestExists(resourceName, &sfr), + resource.TestCheckResourceAttr(resourceName, "target_capacity", "0"), + ), + }, + }, + }) +} + func TestAccAWSSpotFleetRequest_WithInstanceStoreAmi(t *testing.T) { t.Skip("Test fails due to test harness constraints") rName := acctest.RandomWithPrefix("tf-acc-test") @@ -2308,3 +2350,23 @@ resource "aws_spot_fleet_request" "test" { } `, rName, validUntil) } + +func testAccAWSSpotFleetRequestZeroCapacityConfig(rName string, validUntil string) string { + return testAccAWSSpotFleetRequestConfigBase(rName) + fmt.Sprintf(` +resource "aws_spot_fleet_request" "test" { + iam_fleet_role = "${aws_iam_role.test.arn}" + spot_price = "0.05" + target_capacity = 0 + valid_until = %[1]q + terminate_instances_with_expiration = true + instance_interruption_behaviour = "stop" + wait_for_fulfillment = true + launch_specification { + instance_type = "${data.aws_ec2_instance_type_offering.available.instance_type}" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + key_name = "${aws_key_pair.test.key_name}" + } + depends_on = ["aws_iam_policy_attachment.test"] +} +`, validUntil) +}