github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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_CannotUseEmptyKeyName(t *testing.T) {
   297  	_, errors := validateSpotFleetRequestKeyName("", "key_name")
   298  	if len(errors) == 0 {
   299  		t.Fatalf("Expected the key name to trigger a validation error")
   300  	}
   301  }
   302  
   303  func testAccCheckAWSSpotFleetRequestConfigRecreated(t *testing.T,
   304  	before, after *ec2.SpotFleetRequestConfig) resource.TestCheckFunc {
   305  	return func(s *terraform.State) error {
   306  		if before.SpotFleetRequestId == after.SpotFleetRequestId {
   307  			t.Fatalf("Expected change of Spot Fleet Request IDs, but both were %v", before.SpotFleetRequestId)
   308  		}
   309  		return nil
   310  	}
   311  }
   312  
   313  func testAccCheckAWSSpotFleetRequestExists(
   314  	n string, sfr *ec2.SpotFleetRequestConfig) resource.TestCheckFunc {
   315  	return func(s *terraform.State) error {
   316  		rs, ok := s.RootModule().Resources[n]
   317  		if !ok {
   318  			return fmt.Errorf("Not found: %s", n)
   319  		}
   320  
   321  		if rs.Primary.ID == "" {
   322  			return fmt.Errorf("No Spot fleet request with that id exists")
   323  		}
   324  
   325  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   326  
   327  		params := &ec2.DescribeSpotFleetRequestsInput{
   328  			SpotFleetRequestIds: []*string{&rs.Primary.ID},
   329  		}
   330  		resp, err := conn.DescribeSpotFleetRequests(params)
   331  
   332  		if err != nil {
   333  			return err
   334  		}
   335  
   336  		if v := len(resp.SpotFleetRequestConfigs); v != 1 {
   337  			return fmt.Errorf("Expected 1 request returned, got %d", v)
   338  		}
   339  
   340  		*sfr = *resp.SpotFleetRequestConfigs[0]
   341  
   342  		return nil
   343  	}
   344  }
   345  
   346  func testAccCheckAWSSpotFleetRequest_LaunchSpecAttributes(
   347  	sfr *ec2.SpotFleetRequestConfig) resource.TestCheckFunc {
   348  	return func(s *terraform.State) error {
   349  		if len(sfr.SpotFleetRequestConfig.LaunchSpecifications) == 0 {
   350  			return fmt.Errorf("Missing launch specification")
   351  		}
   352  
   353  		spec := *sfr.SpotFleetRequestConfig.LaunchSpecifications[0]
   354  
   355  		if *spec.InstanceType != "m1.small" {
   356  			return fmt.Errorf("Unexpected launch specification instance type: %s", *spec.InstanceType)
   357  		}
   358  
   359  		if *spec.ImageId != "ami-d06a90b0" {
   360  			return fmt.Errorf("Unexpected launch specification image id: %s", *spec.ImageId)
   361  		}
   362  
   363  		if *spec.SpotPrice != "0.01" {
   364  			return fmt.Errorf("Unexpected launch specification spot price: %s", *spec.SpotPrice)
   365  		}
   366  
   367  		if *spec.WeightedCapacity != 2 {
   368  			return fmt.Errorf("Unexpected launch specification weighted capacity: %f", *spec.WeightedCapacity)
   369  		}
   370  
   371  		if *spec.UserData != base64.StdEncoding.EncodeToString([]byte("hello-world")) {
   372  			return fmt.Errorf("Unexpected launch specification user data: %s", *spec.UserData)
   373  		}
   374  
   375  		return nil
   376  	}
   377  }
   378  
   379  func testAccCheckAWSSpotFleetRequestDestroy(s *terraform.State) error {
   380  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   381  
   382  	for _, rs := range s.RootModule().Resources {
   383  		if rs.Type != "aws_spot_fleet_request" {
   384  			continue
   385  		}
   386  
   387  		_, err := conn.CancelSpotFleetRequests(&ec2.CancelSpotFleetRequestsInput{
   388  			SpotFleetRequestIds: []*string{aws.String(rs.Primary.ID)},
   389  			TerminateInstances:  aws.Bool(true),
   390  		})
   391  
   392  		if err != nil {
   393  			return fmt.Errorf("Error cancelling spot request (%s): %s", rs.Primary.ID, err)
   394  		}
   395  	}
   396  
   397  	return nil
   398  }
   399  
   400  func testAccAWSSpotFleetRequestConfig(rName string) string {
   401  	return fmt.Sprintf(`
   402  resource "aws_key_pair" "debugging" {
   403  	key_name = "tmp-key-%s"
   404  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   405  }
   406  
   407  resource "aws_iam_policy_attachment" "test-attach" {
   408      name = "test-attachment"
   409      roles = ["${aws_iam_role.test-role.name}"]
   410      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   411  }
   412  
   413  resource "aws_iam_role" "test-role" {
   414      name = "test-role-%s"
   415      assume_role_policy = <<EOF
   416  {
   417    "Version": "2012-10-17",
   418    "Statement": [
   419      {
   420        "Sid": "",
   421        "Effect": "Allow",
   422        "Principal": {
   423          "Service": [
   424            "spotfleet.amazonaws.com",
   425            "ec2.amazonaws.com"
   426          ]
   427        },
   428        "Action": "sts:AssumeRole"
   429      }
   430    ]
   431  }
   432  EOF
   433  }
   434  
   435  resource "aws_spot_fleet_request" "foo" {
   436      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   437      spot_price = "0.005"
   438      target_capacity = 2
   439      valid_until = "2019-11-04T20:44:20Z"
   440      terminate_instances_with_expiration = true
   441      launch_specification {
   442          instance_type = "m1.small"
   443          ami = "ami-d06a90b0"
   444          key_name = "${aws_key_pair.debugging.key_name}"
   445      }
   446      depends_on = ["aws_iam_policy_attachment.test-attach"]
   447  }
   448  `, rName, rName)
   449  }
   450  
   451  func testAccAWSSpotFleetRequestConfigChangeSpotBidPrice(rName string) string {
   452  	return fmt.Sprintf(`
   453  resource "aws_key_pair" "debugging" {
   454  	key_name = "tmp-key-%s"
   455  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   456  }
   457  
   458  resource "aws_iam_policy_attachment" "test-attach" {
   459      name = "test-attachment"
   460      roles = ["${aws_iam_role.test-role.name}"]
   461      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   462  }
   463  
   464  resource "aws_iam_role" "test-role" {
   465      name = "test-role-%s"
   466      assume_role_policy = <<EOF
   467  {
   468    "Version": "2012-10-17",
   469    "Statement": [
   470      {
   471        "Sid": "",
   472        "Effect": "Allow",
   473        "Principal": {
   474          "Service": [
   475            "spotfleet.amazonaws.com",
   476            "ec2.amazonaws.com"
   477          ]
   478        },
   479        "Action": "sts:AssumeRole"
   480      }
   481    ]
   482  }
   483  EOF
   484  }
   485  
   486  resource "aws_spot_fleet_request" "foo" {
   487      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   488      spot_price = "0.01"
   489      target_capacity = 2
   490      valid_until = "2019-11-04T20:44:20Z"
   491      terminate_instances_with_expiration = true
   492      launch_specification {
   493          instance_type = "m1.small"
   494          ami = "ami-d06a90b0"
   495          key_name = "${aws_key_pair.debugging.key_name}"
   496      }
   497      depends_on = ["aws_iam_policy_attachment.test-attach"]
   498  }
   499  `, rName, rName)
   500  }
   501  
   502  func testAccAWSSpotFleetRequestConfigWithAzs(rName string) string {
   503  	return fmt.Sprintf(`
   504  resource "aws_key_pair" "debugging" {
   505  	key_name = "tmp-key-%s"
   506  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   507  }
   508  
   509  resource "aws_iam_policy_attachment" "test-attach" {
   510      name = "test-attachment"
   511      roles = ["${aws_iam_role.test-role.name}"]
   512      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   513  }
   514  
   515  resource "aws_iam_role" "test-role" {
   516      name = "test-role-%s"
   517      assume_role_policy = <<EOF
   518  {
   519    "Version": "2012-10-17",
   520    "Statement": [
   521      {
   522        "Sid": "",
   523        "Effect": "Allow",
   524        "Principal": {
   525          "Service": [
   526            "spotfleet.amazonaws.com",
   527            "ec2.amazonaws.com"
   528          ]
   529        },
   530        "Action": "sts:AssumeRole"
   531      }
   532    ]
   533  }
   534  EOF
   535  }
   536  
   537  resource "aws_spot_fleet_request" "foo" {
   538      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   539      spot_price = "0.005"
   540      target_capacity = 2
   541      valid_until = "2019-11-04T20:44:20Z"
   542      terminate_instances_with_expiration = true
   543      launch_specification {
   544          instance_type = "m1.small"
   545          ami = "ami-d06a90b0"
   546          key_name = "${aws_key_pair.debugging.key_name}"
   547  	availability_zone = "us-west-2a"
   548      }
   549      launch_specification {
   550          instance_type = "m1.small"
   551          ami = "ami-d06a90b0"
   552          key_name = "${aws_key_pair.debugging.key_name}"
   553  	availability_zone = "us-west-2b"
   554      }
   555      depends_on = ["aws_iam_policy_attachment.test-attach"]
   556  }
   557  `, rName, rName)
   558  }
   559  
   560  func testAccAWSSpotFleetRequestConfigWithSubnet(rName string) string {
   561  	return fmt.Sprintf(`
   562  resource "aws_key_pair" "debugging" {
   563  	key_name = "tmp-key-%s"
   564  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   565  }
   566  
   567  resource "aws_iam_policy_attachment" "test-attach" {
   568      name = "test-attachment"
   569      roles = ["${aws_iam_role.test-role.name}"]
   570      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   571  }
   572  
   573  resource "aws_iam_role" "test-role" {
   574      name = "test-role-%s"
   575      assume_role_policy = <<EOF
   576  {
   577    "Version": "2012-10-17",
   578    "Statement": [
   579      {
   580        "Sid": "",
   581        "Effect": "Allow",
   582        "Principal": {
   583          "Service": [
   584            "spotfleet.amazonaws.com",
   585            "ec2.amazonaws.com"
   586          ]
   587        },
   588        "Action": "sts:AssumeRole"
   589      }
   590    ]
   591  }
   592  EOF
   593  }
   594  
   595  resource "aws_vpc" "foo" {
   596      cidr_block = "10.1.0.0/16"
   597  }
   598  
   599  resource "aws_subnet" "foo" {
   600      cidr_block = "10.1.1.0/24"
   601      vpc_id = "${aws_vpc.foo.id}"
   602      availability_zone = "us-west-2a"
   603  }
   604  
   605  resource "aws_subnet" "bar" {
   606      cidr_block = "10.1.20.0/24"
   607      vpc_id = "${aws_vpc.foo.id}"
   608      availability_zone = "us-west-2b"
   609  }
   610  
   611  resource "aws_spot_fleet_request" "foo" {
   612      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   613      spot_price = "0.005"
   614      target_capacity = 4
   615      valid_until = "2019-11-04T20:44:20Z"
   616      terminate_instances_with_expiration = true
   617      launch_specification {
   618          instance_type = "m3.large"
   619          ami = "ami-d0f506b0"
   620          key_name = "${aws_key_pair.debugging.key_name}"
   621  	subnet_id = "${aws_subnet.foo.id}"
   622      }
   623      launch_specification {
   624          instance_type = "m3.large"
   625          ami = "ami-d0f506b0"
   626          key_name = "${aws_key_pair.debugging.key_name}"
   627  	subnet_id = "${aws_subnet.bar.id}"
   628      }
   629      depends_on = ["aws_iam_policy_attachment.test-attach"]
   630  }
   631  `, rName, rName)
   632  }
   633  
   634  func testAccAWSSpotFleetRequestConfigMultipleInstanceTypesinSameAz(rName string) string {
   635  	return fmt.Sprintf(`
   636  resource "aws_key_pair" "debugging" {
   637  	key_name = "tmp-key-%s"
   638  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   639  }
   640  
   641  resource "aws_iam_policy_attachment" "test-attach" {
   642      name = "test-attachment"
   643      roles = ["${aws_iam_role.test-role.name}"]
   644      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   645  }
   646  
   647  resource "aws_iam_role" "test-role" {
   648      name = "test-role-%s"
   649      assume_role_policy = <<EOF
   650  {
   651    "Version": "2012-10-17",
   652    "Statement": [
   653      {
   654        "Sid": "",
   655        "Effect": "Allow",
   656        "Principal": {
   657          "Service": [
   658            "spotfleet.amazonaws.com",
   659            "ec2.amazonaws.com"
   660          ]
   661        },
   662        "Action": "sts:AssumeRole"
   663      }
   664    ]
   665  }
   666  EOF
   667  }
   668  
   669  resource "aws_spot_fleet_request" "foo" {
   670      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   671      spot_price = "0.005"
   672      target_capacity = 2
   673      valid_until = "2019-11-04T20:44:20Z"
   674      terminate_instances_with_expiration = true
   675      launch_specification {
   676          instance_type = "m1.small"
   677          ami = "ami-d06a90b0"
   678          key_name = "${aws_key_pair.debugging.key_name}"
   679          availability_zone = "us-west-2a"
   680      }
   681      launch_specification {
   682          instance_type = "m3.large"
   683          ami = "ami-d06a90b0"
   684          key_name = "${aws_key_pair.debugging.key_name}"
   685          availability_zone = "us-west-2a"
   686      }
   687      depends_on = ["aws_iam_policy_attachment.test-attach"]
   688  }
   689  `, rName, rName)
   690  }
   691  
   692  func testAccAWSSpotFleetRequestConfigMultipleInstanceTypesinSameSubnet(rName string) string {
   693  	return fmt.Sprintf(`
   694  resource "aws_key_pair" "debugging" {
   695  	key_name = "tmp-key-%s"
   696  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   697  }
   698  
   699  resource "aws_iam_policy_attachment" "test-attach" {
   700      name = "test-attachment"
   701      roles = ["${aws_iam_role.test-role.name}"]
   702      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   703  }
   704  
   705  resource "aws_iam_role" "test-role" {
   706      name = "test-role-%s"
   707      assume_role_policy = <<EOF
   708  {
   709    "Version": "2012-10-17",
   710    "Statement": [
   711      {
   712        "Sid": "",
   713        "Effect": "Allow",
   714        "Principal": {
   715          "Service": [
   716            "spotfleet.amazonaws.com",
   717            "ec2.amazonaws.com"
   718          ]
   719        },
   720        "Action": "sts:AssumeRole"
   721      }
   722    ]
   723  }
   724  EOF
   725  }
   726  
   727  resource "aws_vpc" "foo" {
   728      cidr_block = "10.1.0.0/16"
   729  }
   730  
   731  resource "aws_subnet" "foo" {
   732      cidr_block = "10.1.1.0/24"
   733      vpc_id = "${aws_vpc.foo.id}"
   734      availability_zone = "us-west-2a"
   735  }
   736  
   737  resource "aws_spot_fleet_request" "foo" {
   738      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   739      spot_price = "0.005"
   740      target_capacity = 4
   741      valid_until = "2019-11-04T20:44:20Z"
   742      terminate_instances_with_expiration = true
   743      launch_specification {
   744          instance_type = "m3.large"
   745          ami = "ami-d0f506b0"
   746          key_name = "${aws_key_pair.debugging.key_name}"
   747  	subnet_id = "${aws_subnet.foo.id}"
   748      }
   749      launch_specification {
   750          instance_type = "r3.large"
   751          ami = "ami-d0f506b0"
   752          key_name = "${aws_key_pair.debugging.key_name}"
   753  	subnet_id = "${aws_subnet.foo.id}"
   754      }
   755      depends_on = ["aws_iam_policy_attachment.test-attach"]
   756  }
   757  `, rName, rName)
   758  }
   759  
   760  func testAccAWSSpotFleetRequestConfigOverridingSpotPrice(rName string) string {
   761  	return fmt.Sprintf(`
   762  resource "aws_key_pair" "debugging" {
   763  	key_name = "tmp-key-%s"
   764  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   765  }
   766  
   767  resource "aws_iam_policy_attachment" "test-attach" {
   768      name = "test-attachment"
   769      roles = ["${aws_iam_role.test-role.name}"]
   770      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   771  }
   772  
   773  resource "aws_iam_role" "test-role" {
   774      name = "test-role-%s"
   775      assume_role_policy = <<EOF
   776  {
   777    "Version": "2012-10-17",
   778    "Statement": [
   779      {
   780        "Sid": "",
   781        "Effect": "Allow",
   782        "Principal": {
   783          "Service": [
   784            "spotfleet.amazonaws.com",
   785            "ec2.amazonaws.com"
   786          ]
   787        },
   788        "Action": "sts:AssumeRole"
   789      }
   790    ]
   791  }
   792  EOF
   793  }
   794  
   795  resource "aws_spot_fleet_request" "foo" {
   796      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   797      spot_price = "0.005"
   798      target_capacity = 2
   799      valid_until = "2019-11-04T20:44:20Z"
   800      terminate_instances_with_expiration = true
   801      launch_specification {
   802          instance_type = "m1.small"
   803          ami = "ami-d06a90b0"
   804          key_name = "${aws_key_pair.debugging.key_name}"
   805          availability_zone = "us-west-2a"
   806      }
   807      launch_specification {
   808          instance_type = "m3.large"
   809          ami = "ami-d06a90b0"
   810          key_name = "${aws_key_pair.debugging.key_name}"
   811          availability_zone = "us-west-2a"
   812          spot_price = "0.01"
   813      }
   814      depends_on = ["aws_iam_policy_attachment.test-attach"]
   815  }
   816  `, rName, rName)
   817  }
   818  
   819  func testAccAWSSpotFleetRequestConfigDiversifiedAllocation(rName string) string {
   820  	return fmt.Sprintf(`
   821  resource "aws_key_pair" "debugging" {
   822  	key_name = "tmp-key-%s"
   823  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   824  }
   825  
   826  resource "aws_iam_policy_attachment" "test-attach" {
   827      name = "test-attachment"
   828      roles = ["${aws_iam_role.test-role.name}"]
   829      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   830  }
   831  
   832  resource "aws_iam_role" "test-role" {
   833      name = "test-role-%s"
   834      assume_role_policy = <<EOF
   835  {
   836    "Version": "2012-10-17",
   837    "Statement": [
   838      {
   839        "Sid": "",
   840        "Effect": "Allow",
   841        "Principal": {
   842          "Service": [
   843            "spotfleet.amazonaws.com",
   844            "ec2.amazonaws.com"
   845          ]
   846        },
   847        "Action": "sts:AssumeRole"
   848      }
   849    ]
   850  }
   851  EOF
   852  }
   853  
   854  resource "aws_spot_fleet_request" "foo" {
   855      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   856      spot_price = "0.7"
   857      target_capacity = 30
   858      valid_until = "2019-11-04T20:44:20Z"
   859      allocation_strategy = "diversified"
   860      terminate_instances_with_expiration = true
   861      launch_specification {
   862          instance_type = "m1.small"
   863          ami = "ami-d06a90b0"
   864          key_name = "${aws_key_pair.debugging.key_name}"
   865          availability_zone = "us-west-2a"
   866      }
   867      launch_specification {
   868          instance_type = "m3.large"
   869          ami = "ami-d06a90b0"
   870          key_name = "${aws_key_pair.debugging.key_name}"
   871          availability_zone = "us-west-2a"
   872      }
   873      launch_specification {
   874          instance_type = "r3.large"
   875          ami = "ami-d06a90b0"
   876          key_name = "${aws_key_pair.debugging.key_name}"
   877          availability_zone = "us-west-2a"
   878      }
   879      depends_on = ["aws_iam_policy_attachment.test-attach"]
   880  }
   881  `, rName, rName)
   882  }
   883  
   884  func testAccAWSSpotFleetRequestConfigWithWeightedCapacity(rName string) string {
   885  	return fmt.Sprintf(`
   886  resource "aws_key_pair" "debugging" {
   887  	key_name = "tmp-key-%s"
   888  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   889  }
   890  
   891  resource "aws_iam_policy_attachment" "test-attach" {
   892      name = "test-attachment"
   893      roles = ["${aws_iam_role.test-role.name}"]
   894      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
   895  }
   896  
   897  resource "aws_iam_role" "test-role" {
   898      name = "test-role-%s"
   899      assume_role_policy = <<EOF
   900  {
   901    "Version": "2012-10-17",
   902    "Statement": [
   903      {
   904        "Sid": "",
   905        "Effect": "Allow",
   906        "Principal": {
   907          "Service": [
   908            "spotfleet.amazonaws.com",
   909            "ec2.amazonaws.com"
   910          ]
   911        },
   912        "Action": "sts:AssumeRole"
   913      }
   914    ]
   915  }
   916  EOF
   917  }
   918  
   919  resource "aws_spot_fleet_request" "foo" {
   920      iam_fleet_role = "${aws_iam_role.test-role.arn}"
   921      spot_price = "0.7"
   922      target_capacity = 10
   923      valid_until = "2019-11-04T20:44:20Z"
   924      terminate_instances_with_expiration = true
   925      launch_specification {
   926          instance_type = "m3.large"
   927          ami = "ami-d06a90b0"
   928          key_name = "${aws_key_pair.debugging.key_name}"
   929          availability_zone = "us-west-2a"
   930          weighted_capacity = "6"
   931      }
   932      launch_specification {
   933          instance_type = "r3.large"
   934          ami = "ami-d06a90b0"
   935          key_name = "${aws_key_pair.debugging.key_name}"
   936          availability_zone = "us-west-2a"
   937          weighted_capacity = "3"
   938      }
   939      depends_on = ["aws_iam_policy_attachment.test-attach"]
   940  }
   941  `, rName, rName)
   942  }