diff --git a/aws/resource_aws_ec2_transit_gateway_route.go b/aws/resource_aws_ec2_transit_gateway_route.go index 020925d5ba0..cd00adcb90e 100644 --- a/aws/resource_aws_ec2_transit_gateway_route.go +++ b/aws/resource_aws_ec2_transit_gateway_route.go @@ -124,7 +124,7 @@ func resourceAwsEc2TransitGatewayRouteRead(d *schema.ResourceData, meta interfac d.Set("destination_cidr_block", transitGatewayRoute.DestinationCidrBlock) d.Set("transit_gateway_attachment_id", "") - if len(transitGatewayRoute.TransitGatewayAttachments) == 0 || transitGatewayRoute.TransitGatewayAttachments[0] != nil { + if len(transitGatewayRoute.TransitGatewayAttachments) > 0 && transitGatewayRoute.TransitGatewayAttachments[0] != nil { d.Set("transit_gateway_attachment_id", transitGatewayRoute.TransitGatewayAttachments[0].TransitGatewayAttachmentId) } diff --git a/aws/resource_aws_ec2_transit_gateway_route_test.go b/aws/resource_aws_ec2_transit_gateway_route_test.go index bcca6e93480..1cd90166532 100644 --- a/aws/resource_aws_ec2_transit_gateway_route_test.go +++ b/aws/resource_aws_ec2_transit_gateway_route_test.go @@ -62,6 +62,33 @@ func TestAccAWSEc2TransitGatewayRoute_disappears(t *testing.T) { }) } +func TestAccAWSEc2TransitGatewayRoute_disappears_TransitGatewayAttachment(t *testing.T) { + var transitGateway1 ec2.TransitGateway + var transitGatewayRoute1 ec2.TransitGatewayRoute + var transitGatewayVpcAttachment1 ec2.TransitGatewayVpcAttachment + resourceName := "aws_ec2_transit_gateway_route.test" + transitGatewayVpcAttachmentResourceName := "aws_ec2_transit_gateway_vpc_attachment.test" + transitGatewayResourceName := "aws_ec2_transit_gateway.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2TransitGateway(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEc2TransitGatewayRouteDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSEc2TransitGatewayRouteConfigDestinationCidrBlock(), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEc2TransitGatewayExists(transitGatewayResourceName, &transitGateway1), + testAccCheckAWSEc2TransitGatewayRouteExists(resourceName, &transitGatewayRoute1), + testAccCheckAWSEc2TransitGatewayVpcAttachmentExists(transitGatewayVpcAttachmentResourceName, &transitGatewayVpcAttachment1), + testAccCheckAWSEc2TransitGatewayVpcAttachmentDisappears(&transitGatewayVpcAttachment1), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckAWSEc2TransitGatewayRouteExists(resourceName string, transitGatewayRoute *ec2.TransitGatewayRoute) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName]