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

Pass key_name and subnetId to spot instance request - resolves issue #2919 #2954

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
2 changes: 2 additions & 0 deletions builtin/providers/aws/resource_aws_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ func resourceAwsSpotInstanceRequestCreate(d *schema.ResourceData, meta interface
IAMInstanceProfile: instanceOpts.IAMInstanceProfile,
ImageID: instanceOpts.ImageID,
InstanceType: instanceOpts.InstanceType,
KeyName: instanceOpts.KeyName,
Placement: instanceOpts.SpotPlacement,
SecurityGroupIDs: instanceOpts.SecurityGroupIDs,
SecurityGroups: instanceOpts.SecurityGroups,
SubnetID: instanceOpts.SubnetID,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SubnetId:

UserData: instanceOpts.UserData64,
},
}
Expand Down
93 changes: 93 additions & 0 deletions builtin/providers/aws/resource_aws_spot_instance_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccAWSSpotInstanceRequest_basic(t *testing.T) {
testAccCheckAWSSpotInstanceRequestExists(
"aws_spot_instance_request.foo", &sir),
testAccCheckAWSSpotInstanceRequestAttributes(&sir),
testCheckKeyPair("tmp-key", &sir),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo", "spot_bid_status", "fulfilled"),
resource.TestCheckResourceAttr(
Expand All @@ -35,6 +36,45 @@ func TestAccAWSSpotInstanceRequest_basic(t *testing.T) {
})
}

func TestAccAWSSpotInstanceRequest_vpc(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new acceptance test TestAccAWSSpotInstanceRequest_vpc that validates starting a spot instance in a VPC works

var sir ec2.SpotInstanceRequest

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotInstanceRequestDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSpotInstanceRequestConfigVPC,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSpotInstanceRequestExists(
"aws_spot_instance_request.foo_VPC", &sir),
testAccCheckAWSSpotInstanceRequestAttributes(&sir),
testCheckKeyPair("tmp-key", &sir),
testAccCheckAWSSpotInstanceRequestAttributesVPC(&sir),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo_VPC", "spot_bid_status", "fulfilled"),
resource.TestCheckResourceAttr(
"aws_spot_instance_request.foo_VPC", "spot_request_state", "active"),
),
},
},
})
}

func testCheckKeyPair(keyName string, sir *ec2.SpotInstanceRequest) resource.TestCheckFunc {
return func(*terraform.State) error {
if sir.LaunchSpecification.KeyName == nil {
return fmt.Errorf("No Key Pair found, expected(%s)", keyName)
}
if sir.LaunchSpecification.KeyName != nil && *sir.LaunchSpecification.KeyName != keyName {
return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, *sir.LaunchSpecification.KeyName)
}

return nil
}
}

func testAccCheckAWSSpotInstanceRequestDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

Expand Down Expand Up @@ -138,10 +178,26 @@ func testAccCheckAWSSpotInstanceRequestAttributes(
}
}

func testAccCheckAWSSpotInstanceRequestAttributesVPC(
sir *ec2.SpotInstanceRequest) resource.TestCheckFunc {
return func(s *terraform.State) error {
if sir.LaunchSpecification.SubnetID == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LaunchSpecification.SubnetId

return fmt.Errorf("SubnetID was not passed, but should have been for this instance to belong to a VPC")
}
return nil
}
}

const testAccAWSSpotInstanceRequestConfig = `
resource "aws_key_pair" "debugging" {
key_name = "tmp-key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}

resource "aws_spot_instance_request" "foo" {
ami = "ami-4fccb37f"
instance_type = "m1.small"
key_name = "${aws_key_pair.debugging.key_name}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the default spot test to validate that passing a key_name works; since it doesn't seem to make sense to start an instance without one.


// base price is $0.044 hourly, so bidding above that should theoretically
// always fulfill
Expand All @@ -156,3 +212,40 @@ resource "aws_spot_instance_request" "foo" {
}
}
`

const testAccAWSSpotInstanceRequestConfigVPC = `
resource "aws_vpc" "foo_VPC" {
cidr_block = "10.1.0.0/16"
}

resource "aws_subnet" "foo_VPC" {
cidr_block = "10.1.1.0/24"
vpc_id = "${aws_vpc.foo_VPC.id}"
}

resource "aws_key_pair" "debugging" {
key_name = "tmp-key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}

resource "aws_spot_instance_request" "foo_VPC" {
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"

// VPC settings
subnet_id = "${aws_subnet.foo_VPC.id}"

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

tags {
Name = "terraform-test-VPC"
}
}
`