Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_spot_instance_request: Fix being able to stop requests #1986

Merged
merged 2 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions aws/resource_aws_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsSpotInstanceRequest() *schema.Resource {
Expand Down Expand Up @@ -81,8 +82,13 @@ func resourceAwsSpotInstanceRequest() *schema.Resource {
s["instance_interruption_behaviour"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "terminate",
Default: ec2.InstanceInterruptionBehaviorTerminate,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
ec2.InstanceInterruptionBehaviorTerminate,
ec2.InstanceInterruptionBehaviorStop,
ec2.InstanceInterruptionBehaviorHibernate,
}, false),
}
return s
}(),
Expand Down Expand Up @@ -115,7 +121,6 @@ func resourceAwsSpotInstanceRequestCreate(d *schema.ResourceData, meta interface
ImageId: instanceOpts.ImageID,
InstanceType: instanceOpts.InstanceType,
KeyName: instanceOpts.KeyName,
Placement: instanceOpts.SpotPlacement,
SecurityGroupIds: instanceOpts.SecurityGroupIDs,
SecurityGroups: instanceOpts.SecurityGroups,
SubnetId: instanceOpts.SubnetID,
Expand All @@ -132,6 +137,11 @@ func resourceAwsSpotInstanceRequestCreate(d *schema.ResourceData, meta interface
spotOpts.LaunchGroup = aws.String(v.(string))
}

// Placement GroupName can only be specified when instanceInterruptionBehavior is not set or set to 'terminate'
if v, exists := d.GetOkExists("instance_interruption_behaviour"); v.(string) == ec2.InstanceInterruptionBehaviorTerminate || !exists {
spotOpts.LaunchSpecification.Placement = instanceOpts.SpotPlacement
}

// Make the spot instance request
log.Printf("[DEBUG] Requesting spot bid opts: %s", spotOpts)

Expand Down
68 changes: 68 additions & 0 deletions aws/resource_aws_spot_instance_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,56 @@ func testAccCheckAWSSpotInstanceRequestAttributesVPC(
}
}

func TestAccAWSSpotInstanceRequestInterruptStop(t *testing.T) {
var sir ec2.SpotInstanceRequest

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotInstanceRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotInstanceRequestInterruptConfig("stop"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSpotInstanceRequestExists(
"aws_spot_instance_request.foo", &sir),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo", "spot_bid_status", "fulfilled"),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo", "spot_request_state", "active"),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo", "instance_interruption_behaviour", "stop"),
),
},
},
})
}

func TestAccAWSSpotInstanceRequestInterruptHibernate(t *testing.T) {
var sir ec2.SpotInstanceRequest

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotInstanceRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotInstanceRequestInterruptConfig("hibernate"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSpotInstanceRequestExists(
"aws_spot_instance_request.foo", &sir),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo", "spot_bid_status", "fulfilled"),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo", "spot_request_state", "active"),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo", "instance_interruption_behaviour", "hibernate"),
),
},
},
})
}

func testAccAWSSpotInstanceRequestConfig(rInt int) string {
return fmt.Sprintf(`
resource "aws_key_pair" "debugging" {
Expand Down Expand Up @@ -589,3 +639,21 @@ func testAccAWSSpotInstanceRequestConfig_getPasswordData(rInt int) string {
}
`, rInt)
}

func testAccAWSSpotInstanceRequestInterruptConfig(interruption_behavior string) string {
return fmt.Sprintf(`
resource "aws_spot_instance_request" "foo" {
ami = "ami-19e92861"
instance_type = "c5.large"

// base price is $0.067 hourly, so bidding above that should theoretically
// always fulfill
spot_price = "0.07"

// we wait for fulfillment because we want to inspect the launched instance
// and verify termination behavior
wait_for_fulfillment = true

instance_interruption_behaviour = "%s"
}`, interruption_behavior)
}