Skip to content

Commit 0b09440

Browse files
committed
fix Lambda state issue by waiting for an Active state. Closes #833
not quite ideal, but hopefully this is enough to mitigate the issue people are seeing for now, ideally this is more configurable. I cant reproduce the issue myself, so please let me know if it doesnt fix the issue, seems like it should be ok
1 parent ffd660e commit 0b09440

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

platform/lambda/lambda.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,9 @@ func (p *Platform) updateFunction(c *lambda.Lambda, a *apigateway.APIGateway, up
621621

622622
// update function config
623623
log.Debug("updating function")
624+
if err := p.isPending(c); err != nil {
625+
return "", err
626+
}
624627
_, err = c.UpdateFunctionConfiguration(&lambda.UpdateFunctionConfigurationInput{
625628
FunctionName: &p.config.Name,
626629
Handler: &p.handler,
@@ -638,6 +641,9 @@ func (p *Platform) updateFunction(c *lambda.Lambda, a *apigateway.APIGateway, up
638641

639642
// update function code
640643
log.Debug("updating function code")
644+
if err := p.isPending(c); err != nil {
645+
return "", err
646+
}
641647
res, err := c.UpdateFunctionCode(&lambda.UpdateFunctionCodeInput{
642648
FunctionName: &p.config.Name,
643649
Publish: aws.Bool(true),
@@ -664,6 +670,38 @@ func (p *Platform) updateFunction(c *lambda.Lambda, a *apigateway.APIGateway, up
664670
return *res.Version, nil
665671
}
666672

673+
// isPending implementation.
674+
func (p *Platform) isPending(c *lambda.Lambda) error {
675+
var attempt int
676+
maxAttempts := 30 // TODO: ideally max attempts is configurable
677+
wait := time.Second * 5 // TODO: ideally some backoff
678+
679+
retry:
680+
attempt++
681+
682+
log.Debugf("checking if function is pending (attempt %d of %d)", attempt, maxAttempts)
683+
conf, err := c.GetFunctionConfiguration(&lambda.GetFunctionConfigurationInput{
684+
FunctionName: &p.config.Name,
685+
})
686+
if err != nil {
687+
return errors.Wrapf(err, "getting function config")
688+
}
689+
690+
if *conf.State == "Active" && *conf.LastUpdateStatus != "InProgress" {
691+
log.Debugf("function is in state %q / %q", *conf.State, *conf.LastUpdateStatus)
692+
return nil
693+
}
694+
695+
if attempt >= maxAttempts {
696+
log.Debugf("max attempts exceeded")
697+
return errors.Errorf("function is stuck in the state %q / %q", *conf.State, *conf.LastUpdateStatus)
698+
}
699+
700+
log.Debugf("function is in state %q / %q, trying again in %s", *conf.State, *conf.LastUpdateStatus, wait)
701+
time.Sleep(wait)
702+
goto retry
703+
}
704+
667705
// vpc returns the vpc configuration or nil.
668706
func (p *Platform) vpc() *lambda.VpcConfig {
669707
v := p.config.Lambda.VPC

0 commit comments

Comments
 (0)