Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_lambda_event_source_mapping: Fix creation wait for proper state update #14765

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions aws/resource_aws_lambda_event_source_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

const (
LambdaEventSourceMappingCreating = "Creating"
LambdaEventSourceMappingEnabling = "Enabling"
LambdaEventSourceMappingUpdating = "Updating"
LambdaEventSourceMappingDisabling = "Disabling"
LambdaEventSourceMappingEnabled = "Enabled"
LambdaEventSourceMappingDisabled = "Disabled"
)

func resourceAwsLambdaEventSourceMapping() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLambdaEventSourceMappingCreate,
Expand Down Expand Up @@ -257,6 +266,11 @@ func resourceAwsLambdaEventSourceMappingCreate(d *schema.ResourceData, meta inte
// No error
d.Set("uuid", eventSourceMappingConfiguration.UUID)
d.SetId(*eventSourceMappingConfiguration.UUID)

if err := waitForLambdaEventSourceMapping(conn, *eventSourceMappingConfiguration.UUID); err != nil {
return err
}

return resourceAwsLambdaEventSourceMappingRead(d, meta)
}

Expand Down Expand Up @@ -303,12 +317,12 @@ func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interf
state := aws.StringValue(eventSourceMappingConfiguration.State)

switch state {
case "Enabled", "Enabling":
case LambdaEventSourceMappingEnabled:
d.Set("enabled", true)
case "Disabled", "Disabling":
case LambdaEventSourceMappingDisabled:
d.Set("enabled", false)
default:
log.Printf("[DEBUG] Lambda event source mapping is neither enabled nor disabled but %s", *eventSourceMappingConfiguration.State)
return fmt.Errorf("state is neither enabled nor disabled but %s", *eventSourceMappingConfiguration.State)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to prevent unexpected issues for now, likely going to keep this as a warning log.

}

return nil
Expand Down Expand Up @@ -406,5 +420,35 @@ func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta inte
return fmt.Errorf("Error updating Lambda event source mapping: %s", err)
}

if err := waitForLambdaEventSourceMapping(conn, d.Id()); err != nil {
return err
}

return resourceAwsLambdaEventSourceMappingRead(d, meta)
}

func waitForLambdaEventSourceMapping(conn *lambda.Lambda, id string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{LambdaEventSourceMappingCreating, LambdaEventSourceMappingEnabling, LambdaEventSourceMappingUpdating, LambdaEventSourceMappingDisabling},
Target: []string{LambdaEventSourceMappingEnabled, LambdaEventSourceMappingDisabled},
Refresh: func() (interface{}, string, error) {
params := &lambda.GetEventSourceMappingInput{
UUID: aws.String(id),
}

res, err := conn.GetEventSourceMapping(params)
if err != nil {
return nil, "", err
}

return res, aws.StringValue(res.State), err
},
Timeout: 10 * time.Minute,
Delay: 5 * time.Second,
}

log.Printf("[DEBUG] Waiting for LambdaEventSourceMapping state update: %s", id)
_, err := stateConf.WaitForState()

return err
}