Skip to content

Commit

Permalink
added launch_group option to aws_spot_instance_request. (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrison-Miller authored and stack72 committed Jul 12, 2017
1 parent 370efb3 commit 423e66d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions aws/resource_aws_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func resourceAwsSpotInstanceRequest() *schema.Resource {
Optional: true,
Default: false,
}
s["launch_group"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
}
s["spot_bid_status"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -117,6 +122,10 @@ func resourceAwsSpotInstanceRequestCreate(d *schema.ResourceData, meta interface
spotOpts.BlockDurationMinutes = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("launch_group"); ok {
spotOpts.LaunchGroup = aws.String(v.(string))
}

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

Expand Down Expand Up @@ -217,6 +226,7 @@ func resourceAwsSpotInstanceRequestRead(d *schema.ResourceData, meta interface{}
}

d.Set("spot_request_state", request.State)
d.Set("launch_group", request.LaunchGroup)
d.Set("block_duration_minutes", request.BlockDurationMinutes)
d.Set("tags", tagsToMap(request.Tags))

Expand Down
56 changes: 56 additions & 0 deletions aws/resource_aws_spot_instance_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ func TestAccAWSSpotInstanceRequest_basic(t *testing.T) {
})
}

func TestAccAWSSpotInstanceRequest_withLaunchGroup(t *testing.T) {
var sir ec2.SpotInstanceRequest
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotInstanceRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotInstanceRequestConfig_withLaunchGroup(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSpotInstanceRequestExists(
"aws_spot_instance_request.foo", &sir),
testAccCheckAWSSpotInstanceRequestAttributes(&sir),
testCheckKeyPair(fmt.Sprintf("tmp-key-%d", rInt), &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", "launch_group", "terraform-test-group"),
),
},
},
})
}

func TestAccAWSSpotInstanceRequest_withBlockDuration(t *testing.T) {
var sir ec2.SpotInstanceRequest
rInt := acctest.RandInt()
Expand Down Expand Up @@ -356,6 +384,34 @@ func testAccAWSSpotInstanceRequestConfig(rInt int) string {
}`, rInt)
}

func testAccAWSSpotInstanceRequestConfig_withLaunchGroup(rInt int) string {
return fmt.Sprintf(`
resource "aws_key_pair" "debugging" {
key_name = "tmp-key-%d"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
}
resource "aws_spot_instance_request" "foo" {
ami = "ami-4fccb37f"
instance_type = "m1.small"
key_name = "${aws_key_pair.debugging.key_name}"
// base price is $0.044 hourly, so bidding above that should theoretically
// always fulfill
spot_price = "0.05"
// we wait for fulfillment because we want to inspect the launched instance
// and verify termination behavior
wait_for_fulfillment = true
launch_group = "terraform-test-group"
tags {
Name = "terraform-test"
}
}`, rInt)
}

func testAccAWSSpotInstanceRequestConfig_withBlockDuration(rInt int) string {
return fmt.Sprintf(`
resource "aws_key_pair" "debugging" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/spot_instance_request.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Spot Instance Requests support all the same arguments as
* `spot_type` - (Optional; Default: "persistent") If set to "one-time", after
the instance is terminated, the spot request will be closed. Also, Terraform
can't manage one-time spot requests, just launch them.
* `launch_group` - (Optional) A launch group is a group of spot instances that launch together and terminate together.
If left empty instances are launched and terminated individually.
* `block_duration_minutes` - (Optional) The required duration for the Spot instances, in minutes. This value must be a multiple of 60 (60, 120, 180, 240, 300, or 360).
The duration period starts as soon as your Spot instance receives its instance ID. At the end of the duration period, Amazon EC2 marks the Spot instance for termination and provides a Spot instance termination notice, which gives the instance a two-minute warning before it terminates.
Note that you can't specify an Availability Zone group or a launch group if you specify a duration.
Expand Down

0 comments on commit 423e66d

Please sign in to comment.