diff --git a/internal/service/ec2/ec2_instance_test.go b/internal/service/ec2/ec2_instance_test.go index cf3d952ad998..2c75b02a9402 100644 --- a/internal/service/ec2/ec2_instance_test.go +++ b/internal/service/ec2/ec2_instance_test.go @@ -5551,6 +5551,46 @@ func defaultSubnetCount(ctx context.Context, t *testing.T) int { return len(subnets) } +func TestAccEC2Instance_basicWithSpot(t *testing.T) { + ctx := acctest.Context(t) + var v ec2.Instance + resourceName := "aws_instance.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + // No subnet_id specified requires default VPC with default subnets. + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckInstanceDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccInstanceConfig_basicWithSpot(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists(ctx, resourceName, &v), + resource.TestCheckResourceAttrSet(resourceName, "spot_instance_request_id"), + resource.TestCheckResourceAttr(resourceName, "instance_lifecycle", "spot"), + resource.TestCheckResourceAttr(resourceName, "instance_market_options.#", "1"), + resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.%", "2"), + resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.market_type", "spot"), + resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.#", "1"), + resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.0.%", "4"), + resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.0.instance_interruption_behavior", "terminate"), + resource.TestCheckResourceAttrSet(resourceName, "instance_market_options.0.spot_options.0.max_price"), + resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.0.spot_instance_type", "one-time"), + resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.0.valid_until", ""), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"user_data_replace_on_change"}, + }, + }, + }) +} + // testAccLatestAmazonLinuxPVEBSAMIConfig returns the configuration for a data source that // describes the latest Amazon Linux AMI using PV virtualization and an EBS root device. // The data source is named 'amzn-ami-minimal-pv-ebs'. @@ -8849,3 +8889,19 @@ resource "aws_instance" "test" { } `, rName)) } + +func testAccInstanceConfig_basicWithSpot(rName string) string { + return acctest.ConfigCompose( + acctest.ConfigLatestAmazonLinux2HVMEBSARM64AMI(), + acctest.AvailableEC2InstanceTypeForRegion("t4g.nano"), + fmt.Sprintf(` +resource "aws_instance" "test" { + ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id + instance_market_options {} + instance_type = data.aws_ec2_instance_type_offering.available.instance_type + tags = { + Name = %[1]q + } +} +`, rName)) +}