provider/aws: Refresh `aws_lambda_event_source_mapping` from state when (#8378)

NotFound

Fixes #8375

When a Lambda or an associated Event Source Mapping has been removed via
the AWS Console, Terraform throws an error similar to the following:

```
Error refreshing state: 1 error(s) occurred:

* aws_lambda_event_source_mapping.dmp_enrichment_event_source_mapping:
* ResourceNotFoundException: The resource you requested does not exist.
    status code: 404, request id: a17c641d-6868-11e6-accf-3d0ea71934fa

```

the resource should be refreshed from the state when this happens so
that subsequent plans show it needs to be recreated

```
% make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSLambdaEventSourceMapping_'
==> Checking that code complies with gofmt requirements...
/Users/stacko/Code/go/bin/stringer
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/08/22 16:15:54 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v
-run=TestAccAWSLambdaEventSourceMapping_ -timeout 120m
=== RUN   TestAccAWSLambdaEventSourceMapping_basic
--- PASS: TestAccAWSLambdaEventSourceMapping_basic (120.81s)
=== RUN   TestAccAWSLambdaEventSourceMapping_disappears
--- PASS: TestAccAWSLambdaEventSourceMapping_disappears (104.08s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/aws    224.914s
```
This commit is contained in:
Paul Stack 2016-08-22 17:05:48 +01:00 committed by GitHub
parent 50a18b00df
commit 9d1ef78fc1
2 changed files with 62 additions and 0 deletions

View File

@ -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
}

View File

@ -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