diff --git a/aws/resource_aws_spot_instance_request.go b/aws/resource_aws_spot_instance_request.go index 805662a1581..4c64bc448e7 100644 --- a/aws/resource_aws_spot_instance_request.go +++ b/aws/resource_aws_spot_instance_request.go @@ -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, @@ -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) @@ -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)) diff --git a/aws/resource_aws_spot_instance_request_test.go b/aws/resource_aws_spot_instance_request_test.go index 21a099216af..c52611ef120 100644 --- a/aws/resource_aws_spot_instance_request_test.go +++ b/aws/resource_aws_spot_instance_request_test.go @@ -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() @@ -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" { diff --git a/website/docs/r/spot_instance_request.html.markdown b/website/docs/r/spot_instance_request.html.markdown index 52cbb847fa8..777e2ee9012 100644 --- a/website/docs/r/spot_instance_request.html.markdown +++ b/website/docs/r/spot_instance_request.html.markdown @@ -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.