github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_spot_fleet_request_test.go (about) 1 package aws 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "log" 7 "testing" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/service/ec2" 12 "github.com/hashicorp/terraform/helper/acctest" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/terraform" 15 ) 16 17 func TestAccAWSSpotFleetRequest_changePriceForcesNewRequest(t *testing.T) { 18 var before, after ec2.SpotFleetRequestConfig 19 rName := acctest.RandString(10) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: testAccAWSSpotFleetRequestConfig(rName), 28 Check: resource.ComposeAggregateTestCheckFunc( 29 testAccCheckAWSSpotFleetRequestExists( 30 "aws_spot_fleet_request.foo", &before), 31 resource.TestCheckResourceAttr( 32 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 33 resource.TestCheckResourceAttr( 34 "aws_spot_fleet_request.foo", "spot_price", "0.005"), 35 resource.TestCheckResourceAttr( 36 "aws_spot_fleet_request.foo", "launch_specification.#", "1"), 37 ), 38 }, 39 resource.TestStep{ 40 Config: testAccAWSSpotFleetRequestConfigChangeSpotBidPrice(rName), 41 Check: resource.ComposeAggregateTestCheckFunc( 42 testAccCheckAWSSpotFleetRequestExists( 43 "aws_spot_fleet_request.foo", &after), 44 resource.TestCheckResourceAttr( 45 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 46 resource.TestCheckResourceAttr( 47 "aws_spot_fleet_request.foo", "launch_specification.#", "1"), 48 resource.TestCheckResourceAttr( 49 "aws_spot_fleet_request.foo", "spot_price", "0.01"), 50 testAccCheckAWSSpotFleetRequestConfigRecreated(t, &before, &after), 51 ), 52 }, 53 }, 54 }) 55 } 56 57 func TestAccAWSSpotFleetRequest_lowestPriceAzOrSubnetInRegion(t *testing.T) { 58 var sfr ec2.SpotFleetRequestConfig 59 rName := acctest.RandString(10) 60 61 resource.Test(t, resource.TestCase{ 62 PreCheck: func() { testAccPreCheck(t) }, 63 Providers: testAccProviders, 64 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 65 Steps: []resource.TestStep{ 66 resource.TestStep{ 67 Config: testAccAWSSpotFleetRequestConfig(rName), 68 Check: resource.ComposeAggregateTestCheckFunc( 69 testAccCheckAWSSpotFleetRequestExists( 70 "aws_spot_fleet_request.foo", &sfr), 71 resource.TestCheckResourceAttr( 72 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 73 resource.TestCheckResourceAttr( 74 "aws_spot_fleet_request.foo", "launch_specification.#", "1"), 75 ), 76 }, 77 }, 78 }) 79 } 80 81 func TestAccAWSSpotFleetRequest_lowestPriceAzInGivenList(t *testing.T) { 82 var sfr ec2.SpotFleetRequestConfig 83 rName := acctest.RandString(10) 84 85 resource.Test(t, resource.TestCase{ 86 PreCheck: func() { testAccPreCheck(t) }, 87 Providers: testAccProviders, 88 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 89 Steps: []resource.TestStep{ 90 resource.TestStep{ 91 Config: testAccAWSSpotFleetRequestConfigWithAzs(rName), 92 Check: resource.ComposeAggregateTestCheckFunc( 93 testAccCheckAWSSpotFleetRequestExists( 94 "aws_spot_fleet_request.foo", &sfr), 95 resource.TestCheckResourceAttr( 96 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 97 resource.TestCheckResourceAttr( 98 "aws_spot_fleet_request.foo", "launch_specification.#", "2"), 99 resource.TestCheckResourceAttr( 100 "aws_spot_fleet_request.foo", "launch_specification.1590006269.availability_zone", "us-west-2a"), 101 resource.TestCheckResourceAttr( 102 "aws_spot_fleet_request.foo", "launch_specification.3809475891.availability_zone", "us-west-2b"), 103 ), 104 }, 105 }, 106 }) 107 } 108 109 func TestAccAWSSpotFleetRequest_lowestPriceSubnetInGivenList(t *testing.T) { 110 var sfr ec2.SpotFleetRequestConfig 111 rName := acctest.RandString(10) 112 113 resource.Test(t, resource.TestCase{ 114 PreCheck: func() { testAccPreCheck(t) }, 115 Providers: testAccProviders, 116 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 117 Steps: []resource.TestStep{ 118 resource.TestStep{ 119 Config: testAccAWSSpotFleetRequestConfigWithSubnet(rName), 120 Check: resource.ComposeAggregateTestCheckFunc( 121 testAccCheckAWSSpotFleetRequestExists( 122 "aws_spot_fleet_request.foo", &sfr), 123 resource.TestCheckResourceAttr( 124 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 125 resource.TestCheckResourceAttr( 126 "aws_spot_fleet_request.foo", "launch_specification.#", "2"), 127 ), 128 }, 129 }, 130 }) 131 } 132 133 func TestAccAWSSpotFleetRequest_multipleInstanceTypesInSameAz(t *testing.T) { 134 var sfr ec2.SpotFleetRequestConfig 135 rName := acctest.RandString(10) 136 137 resource.Test(t, resource.TestCase{ 138 PreCheck: func() { testAccPreCheck(t) }, 139 Providers: testAccProviders, 140 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 141 Steps: []resource.TestStep{ 142 resource.TestStep{ 143 Config: testAccAWSSpotFleetRequestConfigMultipleInstanceTypesinSameAz(rName), 144 Check: resource.ComposeAggregateTestCheckFunc( 145 testAccCheckAWSSpotFleetRequestExists( 146 "aws_spot_fleet_request.foo", &sfr), 147 resource.TestCheckResourceAttr( 148 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 149 resource.TestCheckResourceAttr( 150 "aws_spot_fleet_request.foo", "launch_specification.#", "2"), 151 resource.TestCheckResourceAttr( 152 "aws_spot_fleet_request.foo", "launch_specification.1590006269.instance_type", "m1.small"), 153 resource.TestCheckResourceAttr( 154 "aws_spot_fleet_request.foo", "launch_specification.1590006269.availability_zone", "us-west-2a"), 155 resource.TestCheckResourceAttr( 156 "aws_spot_fleet_request.foo", "launch_specification.3079734941.instance_type", "m3.large"), 157 resource.TestCheckResourceAttr( 158 "aws_spot_fleet_request.foo", "launch_specification.3079734941.availability_zone", "us-west-2a"), 159 ), 160 }, 161 }, 162 }) 163 } 164 165 func TestAccAWSSpotFleetRequest_multipleInstanceTypesInSameSubnet(t *testing.T) { 166 var sfr ec2.SpotFleetRequestConfig 167 rName := acctest.RandString(10) 168 169 resource.Test(t, resource.TestCase{ 170 PreCheck: func() { testAccPreCheck(t) }, 171 Providers: testAccProviders, 172 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 173 Steps: []resource.TestStep{ 174 resource.TestStep{ 175 Config: testAccAWSSpotFleetRequestConfigMultipleInstanceTypesinSameSubnet(rName), 176 Check: resource.ComposeAggregateTestCheckFunc( 177 testAccCheckAWSSpotFleetRequestExists( 178 "aws_spot_fleet_request.foo", &sfr), 179 resource.TestCheckResourceAttr( 180 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 181 resource.TestCheckResourceAttr( 182 "aws_spot_fleet_request.foo", "launch_specification.#", "2"), 183 ), 184 }, 185 }, 186 }) 187 } 188 189 func TestAccAWSSpotFleetRequest_overriddingSpotPrice(t *testing.T) { 190 var sfr ec2.SpotFleetRequestConfig 191 rName := acctest.RandString(10) 192 193 resource.Test(t, resource.TestCase{ 194 PreCheck: func() { testAccPreCheck(t) }, 195 Providers: testAccProviders, 196 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 197 Steps: []resource.TestStep{ 198 resource.TestStep{ 199 Config: testAccAWSSpotFleetRequestConfigOverridingSpotPrice(rName), 200 Check: resource.ComposeAggregateTestCheckFunc( 201 testAccCheckAWSSpotFleetRequestExists( 202 "aws_spot_fleet_request.foo", &sfr), 203 resource.TestCheckResourceAttr( 204 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 205 resource.TestCheckResourceAttr( 206 "aws_spot_fleet_request.foo", "spot_price", "0.005"), 207 resource.TestCheckResourceAttr( 208 "aws_spot_fleet_request.foo", "launch_specification.#", "2"), 209 resource.TestCheckResourceAttr( 210 "aws_spot_fleet_request.foo", "launch_specification.522395050.spot_price", "0.01"), 211 resource.TestCheckResourceAttr( 212 "aws_spot_fleet_request.foo", "launch_specification.522395050.instance_type", "m3.large"), 213 resource.TestCheckResourceAttr( 214 "aws_spot_fleet_request.foo", "launch_specification.1590006269.spot_price", ""), //there will not be a value here since it's not overriding 215 resource.TestCheckResourceAttr( 216 "aws_spot_fleet_request.foo", "launch_specification.1590006269.instance_type", "m1.small"), 217 ), 218 }, 219 }, 220 }) 221 } 222 223 func TestAccAWSSpotFleetRequest_diversifiedAllocation(t *testing.T) { 224 var sfr ec2.SpotFleetRequestConfig 225 rName := acctest.RandString(10) 226 227 resource.Test(t, resource.TestCase{ 228 PreCheck: func() { testAccPreCheck(t) }, 229 Providers: testAccProviders, 230 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 231 Steps: []resource.TestStep{ 232 resource.TestStep{ 233 Config: testAccAWSSpotFleetRequestConfigDiversifiedAllocation(rName), 234 Check: resource.ComposeAggregateTestCheckFunc( 235 testAccCheckAWSSpotFleetRequestExists( 236 "aws_spot_fleet_request.foo", &sfr), 237 resource.TestCheckResourceAttr( 238 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 239 resource.TestCheckResourceAttr( 240 "aws_spot_fleet_request.foo", "launch_specification.#", "3"), 241 resource.TestCheckResourceAttr( 242 "aws_spot_fleet_request.foo", "allocation_strategy", "diversified"), 243 ), 244 }, 245 }, 246 }) 247 } 248 249 func TestAccAWSSpotFleetRequest_withWeightedCapacity(t *testing.T) { 250 var sfr ec2.SpotFleetRequestConfig 251 rName := acctest.RandString(10) 252 253 fulfillSleep := func() resource.TestCheckFunc { 254 // sleep so that EC2 can fuflill the request. We do this to guard against a 255 // regression and possible leak where we'll destroy the request and the 256 // associated IAM role before anything is actually provisioned and running, 257 // thus leaking when those newly started instances are attempted to be 258 // destroyed 259 // See https://github.com/hashicorp/terraform/pull/8938 260 return func(s *terraform.State) error { 261 log.Printf("[DEBUG] Test: Sleep to allow EC2 to actually begin fulfilling TestAccAWSSpotFleetRequest_withWeightedCapacity request") 262 time.Sleep(1 * time.Minute) 263 return nil 264 } 265 } 266 267 resource.Test(t, resource.TestCase{ 268 PreCheck: func() { testAccPreCheck(t) }, 269 Providers: testAccProviders, 270 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 271 Steps: []resource.TestStep{ 272 resource.TestStep{ 273 Config: testAccAWSSpotFleetRequestConfigWithWeightedCapacity(rName), 274 Check: resource.ComposeAggregateTestCheckFunc( 275 fulfillSleep(), 276 testAccCheckAWSSpotFleetRequestExists( 277 "aws_spot_fleet_request.foo", &sfr), 278 resource.TestCheckResourceAttr( 279 "aws_spot_fleet_request.foo", "spot_request_state", "active"), 280 resource.TestCheckResourceAttr( 281 "aws_spot_fleet_request.foo", "launch_specification.#", "2"), 282 resource.TestCheckResourceAttr( 283 "aws_spot_fleet_request.foo", "launch_specification.2325690000.weighted_capacity", "3"), 284 resource.TestCheckResourceAttr( 285 "aws_spot_fleet_request.foo", "launch_specification.2325690000.instance_type", "r3.large"), 286 resource.TestCheckResourceAttr( 287 "aws_spot_fleet_request.foo", "launch_specification.3079734941.weighted_capacity", "6"), 288 resource.TestCheckResourceAttr( 289 "aws_spot_fleet_request.foo", "launch_specification.3079734941.instance_type", "m3.large"), 290 ), 291 }, 292 }, 293 }) 294 } 295 296 func TestAccAWSSpotFleetRequest_withEBSDisk(t *testing.T) { 297 var config ec2.SpotFleetRequestConfig 298 rName := acctest.RandString(10) 299 300 resource.Test(t, resource.TestCase{ 301 PreCheck: func() { testAccPreCheck(t) }, 302 Providers: testAccProviders, 303 CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy, 304 Steps: []resource.TestStep{ 305 resource.TestStep{ 306 Config: testAccAWSSpotFleetRequestEBSConfig(rName), 307 Check: resource.ComposeAggregateTestCheckFunc( 308 testAccCheckAWSSpotFleetRequestExists( 309 "aws_spot_fleet_request.foo", &config), 310 testAccCheckAWSSpotFleetRequest_EBSAttributes( 311 &config), 312 ), 313 }, 314 }, 315 }) 316 } 317 318 func TestAccAWSSpotFleetRequest_CannotUseEmptyKeyName(t *testing.T) { 319 _, errors := validateSpotFleetRequestKeyName("", "key_name") 320 if len(errors) == 0 { 321 t.Fatalf("Expected the key name to trigger a validation error") 322 } 323 } 324 325 func testAccCheckAWSSpotFleetRequestConfigRecreated(t *testing.T, 326 before, after *ec2.SpotFleetRequestConfig) resource.TestCheckFunc { 327 return func(s *terraform.State) error { 328 if before.SpotFleetRequestId == after.SpotFleetRequestId { 329 t.Fatalf("Expected change of Spot Fleet Request IDs, but both were %v", before.SpotFleetRequestId) 330 } 331 return nil 332 } 333 } 334 335 func testAccCheckAWSSpotFleetRequestExists( 336 n string, sfr *ec2.SpotFleetRequestConfig) resource.TestCheckFunc { 337 return func(s *terraform.State) error { 338 rs, ok := s.RootModule().Resources[n] 339 if !ok { 340 return fmt.Errorf("Not found: %s", n) 341 } 342 343 if rs.Primary.ID == "" { 344 return fmt.Errorf("No Spot fleet request with that id exists") 345 } 346 347 conn := testAccProvider.Meta().(*AWSClient).ec2conn 348 349 params := &ec2.DescribeSpotFleetRequestsInput{ 350 SpotFleetRequestIds: []*string{&rs.Primary.ID}, 351 } 352 resp, err := conn.DescribeSpotFleetRequests(params) 353 354 if err != nil { 355 return err 356 } 357 358 if v := len(resp.SpotFleetRequestConfigs); v != 1 { 359 return fmt.Errorf("Expected 1 request returned, got %d", v) 360 } 361 362 *sfr = *resp.SpotFleetRequestConfigs[0] 363 364 return nil 365 } 366 } 367 368 func testAccCheckAWSSpotFleetRequest_LaunchSpecAttributes( 369 sfr *ec2.SpotFleetRequestConfig) resource.TestCheckFunc { 370 return func(s *terraform.State) error { 371 if len(sfr.SpotFleetRequestConfig.LaunchSpecifications) == 0 { 372 return fmt.Errorf("Missing launch specification") 373 } 374 375 spec := *sfr.SpotFleetRequestConfig.LaunchSpecifications[0] 376 377 if *spec.InstanceType != "m1.small" { 378 return fmt.Errorf("Unexpected launch specification instance type: %s", *spec.InstanceType) 379 } 380 381 if *spec.ImageId != "ami-d06a90b0" { 382 return fmt.Errorf("Unexpected launch specification image id: %s", *spec.ImageId) 383 } 384 385 if *spec.SpotPrice != "0.01" { 386 return fmt.Errorf("Unexpected launch specification spot price: %s", *spec.SpotPrice) 387 } 388 389 if *spec.WeightedCapacity != 2 { 390 return fmt.Errorf("Unexpected launch specification weighted capacity: %f", *spec.WeightedCapacity) 391 } 392 393 if *spec.UserData != base64.StdEncoding.EncodeToString([]byte("hello-world")) { 394 return fmt.Errorf("Unexpected launch specification user data: %s", *spec.UserData) 395 } 396 397 return nil 398 } 399 } 400 401 func testAccCheckAWSSpotFleetRequest_EBSAttributes( 402 sfr *ec2.SpotFleetRequestConfig) resource.TestCheckFunc { 403 return func(s *terraform.State) error { 404 if len(sfr.SpotFleetRequestConfig.LaunchSpecifications) == 0 { 405 return fmt.Errorf("Missing launch specification") 406 } 407 408 spec := *sfr.SpotFleetRequestConfig.LaunchSpecifications[0] 409 410 ebs := spec.BlockDeviceMappings 411 if len(ebs) < 2 { 412 return fmt.Errorf("Expected %d block device mappings, got %d", 2, len(ebs)) 413 } 414 415 if *ebs[0].DeviceName != "/dev/xvda" { 416 return fmt.Errorf("Expected device 0's name to be %s, got %s", "/dev/xvda", *ebs[0].DeviceName) 417 } 418 if *ebs[1].DeviceName != "/dev/xvdcz" { 419 return fmt.Errorf("Expected device 1's name to be %s, got %s", "/dev/xvdcz", *ebs[1].DeviceName) 420 } 421 422 return nil 423 } 424 } 425 426 func testAccCheckAWSSpotFleetRequestDestroy(s *terraform.State) error { 427 conn := testAccProvider.Meta().(*AWSClient).ec2conn 428 429 for _, rs := range s.RootModule().Resources { 430 if rs.Type != "aws_spot_fleet_request" { 431 continue 432 } 433 434 _, err := conn.CancelSpotFleetRequests(&ec2.CancelSpotFleetRequestsInput{ 435 SpotFleetRequestIds: []*string{aws.String(rs.Primary.ID)}, 436 TerminateInstances: aws.Bool(true), 437 }) 438 439 if err != nil { 440 return fmt.Errorf("Error cancelling spot request (%s): %s", rs.Primary.ID, err) 441 } 442 } 443 444 return nil 445 } 446 447 func testAccAWSSpotFleetRequestConfig(rName string) string { 448 return fmt.Sprintf(` 449 resource "aws_key_pair" "debugging" { 450 key_name = "tmp-key-%s" 451 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 452 } 453 454 resource "aws_iam_policy_attachment" "test-attach" { 455 name = "test-attachment" 456 roles = ["${aws_iam_role.test-role.name}"] 457 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 458 } 459 460 resource "aws_iam_role" "test-role" { 461 name = "test-role-%s" 462 assume_role_policy = <<EOF 463 { 464 "Version": "2012-10-17", 465 "Statement": [ 466 { 467 "Sid": "", 468 "Effect": "Allow", 469 "Principal": { 470 "Service": [ 471 "spotfleet.amazonaws.com", 472 "ec2.amazonaws.com" 473 ] 474 }, 475 "Action": "sts:AssumeRole" 476 } 477 ] 478 } 479 EOF 480 } 481 482 resource "aws_spot_fleet_request" "foo" { 483 iam_fleet_role = "${aws_iam_role.test-role.arn}" 484 spot_price = "0.005" 485 target_capacity = 2 486 valid_until = "2019-11-04T20:44:20Z" 487 terminate_instances_with_expiration = true 488 launch_specification { 489 instance_type = "m1.small" 490 ami = "ami-d06a90b0" 491 key_name = "${aws_key_pair.debugging.key_name}" 492 } 493 depends_on = ["aws_iam_policy_attachment.test-attach"] 494 } 495 `, rName, rName) 496 } 497 498 func testAccAWSSpotFleetRequestConfigChangeSpotBidPrice(rName string) string { 499 return fmt.Sprintf(` 500 resource "aws_key_pair" "debugging" { 501 key_name = "tmp-key-%s" 502 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 503 } 504 505 resource "aws_iam_policy_attachment" "test-attach" { 506 name = "test-attachment" 507 roles = ["${aws_iam_role.test-role.name}"] 508 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 509 } 510 511 resource "aws_iam_role" "test-role" { 512 name = "test-role-%s" 513 assume_role_policy = <<EOF 514 { 515 "Version": "2012-10-17", 516 "Statement": [ 517 { 518 "Sid": "", 519 "Effect": "Allow", 520 "Principal": { 521 "Service": [ 522 "spotfleet.amazonaws.com", 523 "ec2.amazonaws.com" 524 ] 525 }, 526 "Action": "sts:AssumeRole" 527 } 528 ] 529 } 530 EOF 531 } 532 533 resource "aws_spot_fleet_request" "foo" { 534 iam_fleet_role = "${aws_iam_role.test-role.arn}" 535 spot_price = "0.01" 536 target_capacity = 2 537 valid_until = "2019-11-04T20:44:20Z" 538 terminate_instances_with_expiration = true 539 launch_specification { 540 instance_type = "m1.small" 541 ami = "ami-d06a90b0" 542 key_name = "${aws_key_pair.debugging.key_name}" 543 } 544 depends_on = ["aws_iam_policy_attachment.test-attach"] 545 } 546 `, rName, rName) 547 } 548 549 func testAccAWSSpotFleetRequestConfigWithAzs(rName string) string { 550 return fmt.Sprintf(` 551 resource "aws_key_pair" "debugging" { 552 key_name = "tmp-key-%s" 553 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 554 } 555 556 resource "aws_iam_policy_attachment" "test-attach" { 557 name = "test-attachment" 558 roles = ["${aws_iam_role.test-role.name}"] 559 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 560 } 561 562 resource "aws_iam_role" "test-role" { 563 name = "test-role-%s" 564 assume_role_policy = <<EOF 565 { 566 "Version": "2012-10-17", 567 "Statement": [ 568 { 569 "Sid": "", 570 "Effect": "Allow", 571 "Principal": { 572 "Service": [ 573 "spotfleet.amazonaws.com", 574 "ec2.amazonaws.com" 575 ] 576 }, 577 "Action": "sts:AssumeRole" 578 } 579 ] 580 } 581 EOF 582 } 583 584 resource "aws_spot_fleet_request" "foo" { 585 iam_fleet_role = "${aws_iam_role.test-role.arn}" 586 spot_price = "0.005" 587 target_capacity = 2 588 valid_until = "2019-11-04T20:44:20Z" 589 terminate_instances_with_expiration = true 590 launch_specification { 591 instance_type = "m1.small" 592 ami = "ami-d06a90b0" 593 key_name = "${aws_key_pair.debugging.key_name}" 594 availability_zone = "us-west-2a" 595 } 596 launch_specification { 597 instance_type = "m1.small" 598 ami = "ami-d06a90b0" 599 key_name = "${aws_key_pair.debugging.key_name}" 600 availability_zone = "us-west-2b" 601 } 602 depends_on = ["aws_iam_policy_attachment.test-attach"] 603 } 604 `, rName, rName) 605 } 606 607 func testAccAWSSpotFleetRequestConfigWithSubnet(rName string) string { 608 return fmt.Sprintf(` 609 resource "aws_key_pair" "debugging" { 610 key_name = "tmp-key-%s" 611 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 612 } 613 614 resource "aws_iam_policy_attachment" "test-attach" { 615 name = "test-attachment" 616 roles = ["${aws_iam_role.test-role.name}"] 617 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 618 } 619 620 resource "aws_iam_role" "test-role" { 621 name = "test-role-%s" 622 assume_role_policy = <<EOF 623 { 624 "Version": "2012-10-17", 625 "Statement": [ 626 { 627 "Sid": "", 628 "Effect": "Allow", 629 "Principal": { 630 "Service": [ 631 "spotfleet.amazonaws.com", 632 "ec2.amazonaws.com" 633 ] 634 }, 635 "Action": "sts:AssumeRole" 636 } 637 ] 638 } 639 EOF 640 } 641 642 resource "aws_vpc" "foo" { 643 cidr_block = "10.1.0.0/16" 644 } 645 646 resource "aws_subnet" "foo" { 647 cidr_block = "10.1.1.0/24" 648 vpc_id = "${aws_vpc.foo.id}" 649 availability_zone = "us-west-2a" 650 } 651 652 resource "aws_subnet" "bar" { 653 cidr_block = "10.1.20.0/24" 654 vpc_id = "${aws_vpc.foo.id}" 655 availability_zone = "us-west-2b" 656 } 657 658 resource "aws_spot_fleet_request" "foo" { 659 iam_fleet_role = "${aws_iam_role.test-role.arn}" 660 spot_price = "0.005" 661 target_capacity = 4 662 valid_until = "2019-11-04T20:44:20Z" 663 terminate_instances_with_expiration = true 664 launch_specification { 665 instance_type = "m3.large" 666 ami = "ami-d0f506b0" 667 key_name = "${aws_key_pair.debugging.key_name}" 668 subnet_id = "${aws_subnet.foo.id}" 669 } 670 launch_specification { 671 instance_type = "m3.large" 672 ami = "ami-d0f506b0" 673 key_name = "${aws_key_pair.debugging.key_name}" 674 subnet_id = "${aws_subnet.bar.id}" 675 } 676 depends_on = ["aws_iam_policy_attachment.test-attach"] 677 } 678 `, rName, rName) 679 } 680 681 func testAccAWSSpotFleetRequestConfigMultipleInstanceTypesinSameAz(rName string) string { 682 return fmt.Sprintf(` 683 resource "aws_key_pair" "debugging" { 684 key_name = "tmp-key-%s" 685 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 686 } 687 688 resource "aws_iam_policy_attachment" "test-attach" { 689 name = "test-attachment" 690 roles = ["${aws_iam_role.test-role.name}"] 691 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 692 } 693 694 resource "aws_iam_role" "test-role" { 695 name = "test-role-%s" 696 assume_role_policy = <<EOF 697 { 698 "Version": "2012-10-17", 699 "Statement": [ 700 { 701 "Sid": "", 702 "Effect": "Allow", 703 "Principal": { 704 "Service": [ 705 "spotfleet.amazonaws.com", 706 "ec2.amazonaws.com" 707 ] 708 }, 709 "Action": "sts:AssumeRole" 710 } 711 ] 712 } 713 EOF 714 } 715 716 resource "aws_spot_fleet_request" "foo" { 717 iam_fleet_role = "${aws_iam_role.test-role.arn}" 718 spot_price = "0.005" 719 target_capacity = 2 720 valid_until = "2019-11-04T20:44:20Z" 721 terminate_instances_with_expiration = true 722 launch_specification { 723 instance_type = "m1.small" 724 ami = "ami-d06a90b0" 725 key_name = "${aws_key_pair.debugging.key_name}" 726 availability_zone = "us-west-2a" 727 } 728 launch_specification { 729 instance_type = "m3.large" 730 ami = "ami-d06a90b0" 731 key_name = "${aws_key_pair.debugging.key_name}" 732 availability_zone = "us-west-2a" 733 } 734 depends_on = ["aws_iam_policy_attachment.test-attach"] 735 } 736 `, rName, rName) 737 } 738 739 func testAccAWSSpotFleetRequestConfigMultipleInstanceTypesinSameSubnet(rName string) string { 740 return fmt.Sprintf(` 741 resource "aws_key_pair" "debugging" { 742 key_name = "tmp-key-%s" 743 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 744 } 745 746 resource "aws_iam_policy_attachment" "test-attach" { 747 name = "test-attachment" 748 roles = ["${aws_iam_role.test-role.name}"] 749 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 750 } 751 752 resource "aws_iam_role" "test-role" { 753 name = "test-role-%s" 754 assume_role_policy = <<EOF 755 { 756 "Version": "2012-10-17", 757 "Statement": [ 758 { 759 "Sid": "", 760 "Effect": "Allow", 761 "Principal": { 762 "Service": [ 763 "spotfleet.amazonaws.com", 764 "ec2.amazonaws.com" 765 ] 766 }, 767 "Action": "sts:AssumeRole" 768 } 769 ] 770 } 771 EOF 772 } 773 774 resource "aws_vpc" "foo" { 775 cidr_block = "10.1.0.0/16" 776 } 777 778 resource "aws_subnet" "foo" { 779 cidr_block = "10.1.1.0/24" 780 vpc_id = "${aws_vpc.foo.id}" 781 availability_zone = "us-west-2a" 782 } 783 784 resource "aws_spot_fleet_request" "foo" { 785 iam_fleet_role = "${aws_iam_role.test-role.arn}" 786 spot_price = "0.005" 787 target_capacity = 4 788 valid_until = "2019-11-04T20:44:20Z" 789 terminate_instances_with_expiration = true 790 launch_specification { 791 instance_type = "m3.large" 792 ami = "ami-d0f506b0" 793 key_name = "${aws_key_pair.debugging.key_name}" 794 subnet_id = "${aws_subnet.foo.id}" 795 } 796 launch_specification { 797 instance_type = "r3.large" 798 ami = "ami-d0f506b0" 799 key_name = "${aws_key_pair.debugging.key_name}" 800 subnet_id = "${aws_subnet.foo.id}" 801 } 802 depends_on = ["aws_iam_policy_attachment.test-attach"] 803 } 804 `, rName, rName) 805 } 806 807 func testAccAWSSpotFleetRequestConfigOverridingSpotPrice(rName string) string { 808 return fmt.Sprintf(` 809 resource "aws_key_pair" "debugging" { 810 key_name = "tmp-key-%s" 811 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 812 } 813 814 resource "aws_iam_policy_attachment" "test-attach" { 815 name = "test-attachment" 816 roles = ["${aws_iam_role.test-role.name}"] 817 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 818 } 819 820 resource "aws_iam_role" "test-role" { 821 name = "test-role-%s" 822 assume_role_policy = <<EOF 823 { 824 "Version": "2012-10-17", 825 "Statement": [ 826 { 827 "Sid": "", 828 "Effect": "Allow", 829 "Principal": { 830 "Service": [ 831 "spotfleet.amazonaws.com", 832 "ec2.amazonaws.com" 833 ] 834 }, 835 "Action": "sts:AssumeRole" 836 } 837 ] 838 } 839 EOF 840 } 841 842 resource "aws_spot_fleet_request" "foo" { 843 iam_fleet_role = "${aws_iam_role.test-role.arn}" 844 spot_price = "0.005" 845 target_capacity = 2 846 valid_until = "2019-11-04T20:44:20Z" 847 terminate_instances_with_expiration = true 848 launch_specification { 849 instance_type = "m1.small" 850 ami = "ami-d06a90b0" 851 key_name = "${aws_key_pair.debugging.key_name}" 852 availability_zone = "us-west-2a" 853 } 854 launch_specification { 855 instance_type = "m3.large" 856 ami = "ami-d06a90b0" 857 key_name = "${aws_key_pair.debugging.key_name}" 858 availability_zone = "us-west-2a" 859 spot_price = "0.01" 860 } 861 depends_on = ["aws_iam_policy_attachment.test-attach"] 862 } 863 `, rName, rName) 864 } 865 866 func testAccAWSSpotFleetRequestConfigDiversifiedAllocation(rName string) string { 867 return fmt.Sprintf(` 868 resource "aws_key_pair" "debugging" { 869 key_name = "tmp-key-%s" 870 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 871 } 872 873 resource "aws_iam_policy_attachment" "test-attach" { 874 name = "test-attachment" 875 roles = ["${aws_iam_role.test-role.name}"] 876 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 877 } 878 879 resource "aws_iam_role" "test-role" { 880 name = "test-role-%s" 881 assume_role_policy = <<EOF 882 { 883 "Version": "2012-10-17", 884 "Statement": [ 885 { 886 "Sid": "", 887 "Effect": "Allow", 888 "Principal": { 889 "Service": [ 890 "spotfleet.amazonaws.com", 891 "ec2.amazonaws.com" 892 ] 893 }, 894 "Action": "sts:AssumeRole" 895 } 896 ] 897 } 898 EOF 899 } 900 901 resource "aws_spot_fleet_request" "foo" { 902 iam_fleet_role = "${aws_iam_role.test-role.arn}" 903 spot_price = "0.7" 904 target_capacity = 30 905 valid_until = "2019-11-04T20:44:20Z" 906 allocation_strategy = "diversified" 907 terminate_instances_with_expiration = true 908 launch_specification { 909 instance_type = "m1.small" 910 ami = "ami-d06a90b0" 911 key_name = "${aws_key_pair.debugging.key_name}" 912 availability_zone = "us-west-2a" 913 } 914 launch_specification { 915 instance_type = "m3.large" 916 ami = "ami-d06a90b0" 917 key_name = "${aws_key_pair.debugging.key_name}" 918 availability_zone = "us-west-2a" 919 } 920 launch_specification { 921 instance_type = "r3.large" 922 ami = "ami-d06a90b0" 923 key_name = "${aws_key_pair.debugging.key_name}" 924 availability_zone = "us-west-2a" 925 } 926 depends_on = ["aws_iam_policy_attachment.test-attach"] 927 } 928 `, rName, rName) 929 } 930 931 func testAccAWSSpotFleetRequestConfigWithWeightedCapacity(rName string) string { 932 return fmt.Sprintf(` 933 resource "aws_key_pair" "debugging" { 934 key_name = "tmp-key-%s" 935 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 936 } 937 938 resource "aws_iam_policy_attachment" "test-attach" { 939 name = "test-attachment" 940 roles = ["${aws_iam_role.test-role.name}"] 941 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 942 } 943 944 resource "aws_iam_role" "test-role" { 945 name = "test-role-%s" 946 assume_role_policy = <<EOF 947 { 948 "Version": "2012-10-17", 949 "Statement": [ 950 { 951 "Sid": "", 952 "Effect": "Allow", 953 "Principal": { 954 "Service": [ 955 "spotfleet.amazonaws.com", 956 "ec2.amazonaws.com" 957 ] 958 }, 959 "Action": "sts:AssumeRole" 960 } 961 ] 962 } 963 EOF 964 } 965 966 resource "aws_spot_fleet_request" "foo" { 967 iam_fleet_role = "${aws_iam_role.test-role.arn}" 968 spot_price = "0.7" 969 target_capacity = 10 970 valid_until = "2019-11-04T20:44:20Z" 971 terminate_instances_with_expiration = true 972 launch_specification { 973 instance_type = "m3.large" 974 ami = "ami-d06a90b0" 975 key_name = "${aws_key_pair.debugging.key_name}" 976 availability_zone = "us-west-2a" 977 weighted_capacity = "6" 978 } 979 launch_specification { 980 instance_type = "r3.large" 981 ami = "ami-d06a90b0" 982 key_name = "${aws_key_pair.debugging.key_name}" 983 availability_zone = "us-west-2a" 984 weighted_capacity = "3" 985 } 986 depends_on = ["aws_iam_policy_attachment.test-attach"] 987 } 988 `, rName, rName) 989 } 990 991 func testAccAWSSpotFleetRequestEBSConfig(rName string) string { 992 return fmt.Sprintf(` 993 resource "aws_iam_policy_attachment" "test-attach" { 994 name = "test-attachment" 995 roles = ["${aws_iam_role.test-role.name}"] 996 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 997 } 998 999 resource "aws_iam_role" "test-role" { 1000 name = "test-role-%s" 1001 assume_role_policy = <<EOF 1002 { 1003 "Version": "2012-10-17", 1004 "Statement": [ 1005 { 1006 "Sid": "", 1007 "Effect": "Allow", 1008 "Principal": { 1009 "Service": [ 1010 "spotfleet.amazonaws.com", 1011 "ec2.amazonaws.com" 1012 ] 1013 }, 1014 "Action": "sts:AssumeRole" 1015 } 1016 ] 1017 } 1018 EOF 1019 } 1020 1021 resource "aws_spot_fleet_request" "foo" { 1022 iam_fleet_role = "${aws_iam_role.test-role.arn}" 1023 spot_price = "0.005" 1024 target_capacity = 1 1025 valid_until = "2019-11-04T20:44:20Z" 1026 terminate_instances_with_expiration = true 1027 launch_specification { 1028 instance_type = "m1.small" 1029 ami = "ami-d06a90b0" 1030 1031 ebs_block_device { 1032 device_name = "/dev/xvda" 1033 volume_type = "gp2" 1034 volume_size = "8" 1035 } 1036 1037 ebs_block_device { 1038 device_name = "/dev/xvdcz" 1039 volume_type = "gp2" 1040 volume_size = "100" 1041 } 1042 } 1043 depends_on = ["aws_iam_policy_attachment.test-attach"] 1044 } 1045 `, rName) 1046 }