diff --git a/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go b/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go index 03072307f..6cae85851 100644 --- a/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go +++ b/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go @@ -134,6 +134,12 @@ func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interf eventSourceMappingConfiguration, err := conn.GetEventSourceMapping(params) if err != nil { + if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "ResourceNotFoundException" { + log.Printf("[DEBUG] Lambda event source mapping (%s) not found", d.Id()) + d.SetId("") + + return nil + } return err } diff --git a/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go b/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go index 59fe5b56e..9ad5dc9d7 100644 --- a/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go +++ b/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go @@ -5,8 +5,10 @@ import ( "regexp" "strconv" "testing" + "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/lambda" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" @@ -48,6 +50,60 @@ func TestAccAWSLambdaEventSourceMapping_basic(t *testing.T) { }) } +func TestAccAWSLambdaEventSourceMapping_disappears(t *testing.T) { + var conf lambda.EventSourceMappingConfiguration + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSLambdaEventSourceMappingConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), + testAccCheckAWSLambdaEventSourceMappingDisappears(&conf), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckAWSLambdaEventSourceMappingDisappears(conf *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).lambdaconn + + params := &lambda.DeleteEventSourceMappingInput{ + UUID: conf.UUID, + } + + _, err := conn.DeleteEventSourceMapping(params) + if err != nil { + if err != nil { + return err + } + } + + return resource.Retry(10*time.Minute, func() *resource.RetryError { + params := &lambda.GetEventSourceMappingInput{ + UUID: conf.UUID, + } + _, err := conn.GetEventSourceMapping(params) + if err != nil { + cgw, ok := err.(awserr.Error) + if ok && cgw.Code() == "ResourceNotFoundException" { + return nil + } + return resource.NonRetryableError( + fmt.Errorf("Error retrieving Lambda Event Source Mapping: %s", err)) + } + return resource.RetryableError(fmt.Errorf( + "Waiting for Lambda Event Source Mapping: %v", conf.UUID)) + }) + } +} + func testAccCheckLambdaEventSourceMappingDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).lambdaconn